I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.
How can I change the commit message/files? The commit has not been pushed yet.
I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.
How can I change the commit message/files? The commit has not been pushed yet.
If it's your last commit, just amend the commit:
(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:
Find the commit you want, change
pick
tor
(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 3 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 asdd
i
)yy
,yw
,yl
to copy ("yank") a line, word, or letter, respectivelyp
orP
to paste after, or before current position, respectively:w
Enter to save (write) a file:q!
Enter to quit without saving:wq
Enter orZZ
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:
* 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.
I have added the alias of
reci
,recm
forrecommit (amend)
it, now I can do it withgit recm
orgit recm -m
.I realised that I had pushed a commit with a typo in it. In order to undo, I did the following:
Warning: force pushing your changes will overwrite the remote branch with your local one. Make sure that you aren't going to be overwriting anything that you want to keep. Also be cautious about force pushing an amended (rewritten) commit if anyone else shares the branch with you, because they'll need to rewrite their own history if they have the old copy of the commit that you've just rewritten.