During the past few years of using git, I’ve come across several commands which have become pivotal in my workflow. One of them is git commit --amend.

Amending a commit

How many times have you inadvertently forgotten to include a file in your last commit? I know my Github can attest to my forgetfulness. We will first start out by creating a file named help.md

1
2
3
# Help
Nothing can stop the man with the right mental attitude from achieving his goal; nothing on
earth can help the man with the wrong mental attitude.
1
2
git add help.md
git commit -m "Add help file"

It seems we forgot to include some text in the help file. To fix it we will append the information to the end of the file using the echo command.

1
echo 'You can also contact me at foo@bar.com' >> hello.md

You can now add it back to the list of tracked files and amend your commit using the following

1
2
git add help.md
git commit --amend -m "Add contact information to help file"

Congratulations, you have successfully amended a commit!

References