On this question there are a lot of answers, but none of them explains in super detail how to change older commit messages using Vim. I was stuck trying to do this myself, so here I'll write down in detail how I did this especially for people who have no experience in Vim!
I wanted to change my five latest commits that I already pushed to the server. This is quite 'dangerous' because if someone else already pulled from this, you can mess things up by changing the commit messages. However, when you’re working on your own little branch and are sure no one pulled it you can change it like this:
Let's say you want to change your five latest commits, and then you type this in the terminal:
git rebase -i HEAD~5
*Where 5 is the number of commit messages you want to change (so if you want to change the 10th to last commit, you type in 10).
This command will get you into Vim there you can ‘edit’ your commit history. You’ll see your last five commits at the top like this:
pick <commit hash> commit message
Instead of pick
you need to write reword
. You can do this in Vim by typing in i
. That makes you go in to insert mode. (You see that you’re in insert mode by the word INSERT at the bottom.) For the commits you want to change, type in reword
instead of pick
.
Then you need to save and quit this screen. You do that by first going in to ‘command-mode’ by pressing the Escbutton (you can check that you’re in command-mode if the word INSERT at the bottom has disappeared). Then you can type in a command by typing :
. The command to save and quit is wq
. So if you type in :wq
you’re on the right track.
Then Vim will go over every commit message you want to reword, and here you can actually change the commit messages. You’ll do this by going into insert mode, changing the commit message, going into the command-mode, and save and quit. Do this five times and you’re out of Vim!
Then, if you already pushed your wrong commits, you need to git push --force
to overwrite them. Remember that git push --force
is quite a dangerous thing to do, so make sure that no one pulled from the server since you pushed your wrong commits!
Now you have changed your commit messages!
(As you see, I'm not that experienced in Vim, so if I used the wrong 'lingo' to explain what's happening, feel free to correct me!)