Command Line

There are a few times when you need to amend your commits in git.

git commit —amend

git commit --amend will allow the modification of the last commit message.

This is the current log in our example.

$ git log --oneline
d09ef8b4 (HEAD -> refactor/655-some-refactoring, origin/refactor/655-some-refactoring) Did some stuff
dd3d303e Some feature spec should now use the new refactored code
cc5ec6b4 Something filter is added to the location search

Let’s change the commit message from Did some stuff to Added tests and removed unneeded method

$ git commit --amend
$ Did some stuff
 
 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.

Modify your messages to reflect your updated message.

Added tests and removed unneeded method
 
 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.

Save.

$ git commit --amend
[refactor/655-some-refactoring a380ce8c] Added tests and removed unneeded method
 Date: Sun Aug 19 19:11:14 2017 +0800
 3 files changed, 7 insertions(+), 19 deletions(-)
git push origin refactor/655-some-refactoring --force

You might want to check with your teammates because you will be overwriting the branch history.