When working with version control tools like Git, it's not uncommon to make a typo or be unhappy with the message we've attached to a commit. Fortunately enough, there are two ways to modify the message of an unpushed commit in Git, depending on whether you want to change the message of the most recent commit or an older one.
It's as simple as running the command below, although it's important not to have any staged changes before running it to avoid unwanted results, as the --amend option also allows you to apply staged changes.
git commit --amend -m "New message"
The -m flag specifies the new commit message, replacing the old commit message with the one you specify inside the quotes. In the example above, this would be New message.
As an extra, if you just wanted to amend the most recent commit by applying your current staged changes without changing the commit message, you would run this:
git commit --amend --no-edit
Note that the -m flag and the quoted message have been replaced by the --no-edit flag, which tells Git to keep the existing commit message.
Before delving deeper into what to do next, here is a basic guide to use Vim to continue with the process:
The arrow keys work fine in Vim, but it's more productive to use h (←), j (↓), k (↑), l (→) in order to avoid moving your hand back and forth between the home row and the arrow keys.
i to insert text before the cursor -- enters Insert mode.
I to insert text at the beginning of the line -- enters Insert mode.
Esc or Ctrl+c to exit Insert mode and return to Normal mode.
u to undo the last change.
Ctrl+r to redo the last undone change.
yaw to yank (copy) the word under the cursor, including surrounding spaces.
yiw to yank (copy) the word under the cursor without surrounding spaces.
p to paste after current position.
P to paste before current position.
:wEnter to save (write) a file.
:qEnter to quit Vim.
:q!Enter to quit Vim without saving.
:wqEnter or ZZ to save and quit Vim.
Go to the line pick 2fbb070 Create test helper to find missing localized strings and switch to Insert mode to replace pick with reword, like this:
reword 2fbb070 Create test helper to find missing localized strings
Exit the Insert mode by pressing Esc.
Enter :wqEnter or ZZ to save and quit Vim.
After that we will be prompted with as many editors as we have changed pick to reword. In this case, we will only be prompted with one editor, as we only want to change one commit message. Note that you can change as many commit messages as you like at once by applying the step 3 to multiple commits.
Modify the commit message and enter :wqEnter or ZZ when you finish.
If for some reason you change your mind in the midst of rebasing and don't want to edit your commit messages anymore, you can exit the Vim editor(s) with :q!Enter and then run git rebase --abort to stop the rebase in progress and return the repository to the state it was in before the rebase started.
If you've already done the rebase, and afterwards realize that you want to undo the changes you made to one or more commit messages, you can run git reflog. It consists of a log of where your branches have been as maintained by Git. You can use it to find the state of your branch before the rebase and revert back to it.
After running it, look for the entry that corresponds to the state before you started the rebase. It should look something like this:
gde8542 (HEAD -> master) HEAD@{0}: rebase -i (finish): returning to refs/heads/masterora9512 HEAD@{1}: rebase -i (start): checkout HEAD~4
Once you identify the correct commit hash (let's say it's ora9512), you can reset your branch to that commit by running git reset --hard ora9512. This will reset your branch to the state it was in before the rebase, effectively undoing all changes made during the rebase. Be cautious with git reset --hard though, as it will discard all changes in the working directory and index.