clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Git: amending commits

Filed under: Source code management— Tagged with: git

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

If, mistakenly or in purpose, an amend was done after pushing to remote, Git gives an error "remote is behind", when pushing (or something along those lines). Then you need to force push the local changes, note that all the changes done to the remote origin by another dev will be lost. But if it’s your branch and you know who you work with, it’s most likely not a problem.

$ git push -f origin master

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

$ git config --global core.editor nano

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!