If it's your last commit, just amend the commit:
git commit --amend -o -m "New commit message"
(Using the -o
(--only
) flag to make sure you change only the commit message)
If it's a buried commit, use the awesome interactive rebase:
git rebase -i @~9 # Show the last 9 commits in a text editor
Find the commit you want, change pick
to r
(reword
), and save and close the file. Done!
Miniature Vim tutorial (or, how to rebase with only 8 keystrokes 3j
cw
r
EscZZ
):
vimtutor
if you have timeh
j
k
l
correspond to movement keys ????3j
moves down three linesi
to enter insert mode — text you type will appear in the filec
to exit insert mode and return to "normal" modeu
to undor
to redodd
, dw
, dl
to delete a line, word, or letter, respectivelycc
, cw
, cl
to change a line, word, or letter, respectively (same as dd
i
)yy
, yw
, yl
to copy ("yank") a line, word, or letter, respectivelyp
or P
to paste after, or before current position, respectively:w
Enter to save (write) a file:q!
Enter to quit without saving:wq
Enter or ZZ
to save and quitIf you edit text a lot, then switch to the Dvorak keyboard layout, learn to touch-type, and learn Vim. Is it worth the effort? Yes.
ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
$ git reset @~3 # Go back three commits
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~3
2c52489 HEAD@{1}: commit: more changes
4a5246d HEAD@{2}: commit: make important changes
e8571e4 HEAD@{3}: commit: make some changes
... earlier commits ...
$ git reset 2c52489
... and you're back where you started
* Watch out for options like --hard
and --force
though — they can discard data.
* Also, don't rewrite history on any branches you're collaborating on.