When working with Git, you would possibly encounter a scenario the place it’s worthwhile to edit a commit message. There are a lot of causes you’d wish to make the change, akin to fixing a typo, eradicating delicate data, or including further data.
This information explains how you can change the message of the newest or older Git commits.
The git commit –amend command lets you change the newest commit message.
To alter the message of the newest commit that has not been pushed to the distant repository, commit it once more utilizing the –amend flag.
Navigate to the repository listing in your terminal.
Run the next command to amend (change) the message of the newest commit:
git commit –amend -m “New commit message.”
What the command does is overwriting the newest commit with the brand new one.
The -m possibility lets you write the brand new message on the command line with out opening an editor session.
Earlier than altering the commit message, you may also add different modifications you beforehand forgot:
git add .git commit –amend -m “New commit message.”
The amended (modified) commit is a brand new entity with a unique SHA-1. The earlier commit will now not exist within the present department.
Usually, it is best to keep away from amending a commit that’s already pushed as it might trigger points to individuals who based mostly their work on this commit. It’s a good suggestion to seek the advice of your fellow builders earlier than altering a pushed commit.
In the event you modified the message of essentially the most lately pushed commit, you would need to pressure push it.
Navigate to the repository.
Amend the message of the newest pushed commit:
git commit –amend -m “New commit message.”
Pressure push to replace the historical past of the distant repository:
git push –force branch-name
If it’s worthwhile to change the message of an older or a number of commits, you should utilize an interactive git rebase to alter a number of older commits.
The rebase command rewrites the commit historical past, and it’s strongly discouraged to rebase commits which might be already pushed to the distant Git repository .
Navigate to the repository containing the commit message you wish to change.
Kind git rebase -i HEAD~N, the place N is the variety of commits to carry out a rebase on. For instance, if you wish to change the 4th and the fifth newest commits you’d sort:
git rebase -i HEAD~5
The command will show the newest X commits in your default textual content editor :
choose 43f8707f9 repair: replace dependency json5 to ^2.1.1
choose cea1fb88a repair: replace dependency verdaccio to ^4.3.3
choose aa540c364 repair: replace dependency webpack-dev-server to ^3.8.2
choose c5e078656 chore: replace dependency flow-bin to ^0.109.0
choose 11ce0ab34 repair: Repair spelling.
# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 instructions)
Transfer to the strains of the commit message you wish to change and substitute choose with reword:
reword 43f8707f9 repair: replace dependency json5 to ^2.1.1
reword cea1fb88a repair: replace dependency verdaccio to ^4.3.3
choose aa540c364 repair: replace dependency webpack-dev-server to ^3.8.2
choose c5e078656 chore: replace dependency flow-bin to ^0.109.0
choose 11ce0ab34 repair: Repair spelling.
# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 instructions)
Save the modifications and shut the editor.
For every chosen commit, a brand new textual content editor window will open. Change the commit message, save the file, and shut the editor.
repair: replace dependency json5 to ^2.1.1
Pressure push the modifications to the distant repository:
git push –force branch-name
To alter the newest commit message, use the git commit –amend command. To alter an older or a number of commit messages, use git rebase -i HEAD~N.
Don’t amend pushed commits as it might doubtlessly trigger a variety of issues to your colleagues.
In the event you hit an issue or have suggestions, depart a remark beneath.
Leave a Reply
You must be logged in to post a comment.