If you only want to modify your last commit message, it is very simple. Just run
$ git commit --amend
That will drop you into your text exitor and let you change the last commit message.
Now let's assume that you want to modify either multiple commit messages,
or a commit message several commits back. In this case, you need to use
a tool called 'interactive rebase'. You can run this with the -i
option to git rebase
. You will need to supply how far back you
want to rewrite commits by telling it which commit to go back to.
If you want to change the last 3 commit messages, or any of the commit
messages up to that point, supply 'HEAD~3' to the git rebase -i
command.
$ git rebase -i HEAD~3
Running this command will give you a list of commits in your text editor that looks something like this:
pick dd56df4 so the reference updates pick 36c7dba updated ticgit gem pick 7482e0d updated the gemspec to hopefully work better # Rebase b429ad8..7482e0d onto b429ad8 # # Commands: # p, pick = use commit # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. #
Change the word 'pick' to the work 'edit' for each of the commits you want to change the message for. For example, let's say we want to change the third commit message only, we would change the file to look like this
pick dd56df4 so the reference updates
pick 36c7dba updated ticgit gem
edit 7482e0d updated the gemspec to hopefully work better
When you save and exit the editor, it will rewind you back to that last commit in that list and drop you on the command line with the following message:
$ git rebase -i HEAD~3 Stopped at 7482e0d... updated the gemspec to hopefully work better You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue
These instructions tell you exactly what to do. Type
$ git commit --amend
Change the commit message and exit the editor. Then run
$ git rebase --continue
You can repeat those steps for each commit you changed to edit
.
Each time it will stop and let you amend the commit and continue when you're
done. In this case, we had no other 'edit's so it will simply rebase the rest
of the commits and we're done!