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
):
- Run
vimtutor
if you have time
h
j
k
l
correspond to movement keys ←↓↑→
- All commands can be prefixed with a "range", e.g.
3j
moves down three lines
i
to enter insert mode — text you type will appear in the file
- Esc or Ctrl
c
to exit insert mode and return to "normal" mode
u
to undo
- Ctrl
r
to redo
dd
, dw
, dl
to delete a line, word, or letter, respectively
cc
, 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, respectively
p
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 quit
If 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.