Git: ammending commits

How to amend commits. Tip: don't amend remote commits.

Amending commits

If you forgot a small change from a commit, you probably don’t want to give that puny little thing its own commit, but amend in to the previous commit where it belongs:

$ git commit --amend

After that, Git pops out the last commit message into a text editor, for me that editor was Vim, for you it could be something different. The message can be edited there if needed, then just save and exit.

About Vim: press esc to enter the command mode, then type :qw (quit and write) and hit enter. More on exiting Vim here.

Do not amend remote commits

This is what I learned the hard way, if you’ve already pushed the changes to GitHub, then don’t amend the commits anymore.

If, mistakenly or in purpose, an amend was done to a remote commit, Git gives an error “remote is behind”, when pushing (or something along those lines). Then you can force push the local changes, note that all the changes to remote origin will be lost.

$ git push -f origin master

ps. the default editor can be changed, for instance to nano:

$ git config --global core.editor nano

Club-Mate, the beverage →