This post is the first part of our Git 101 series that intends to display its useful tips and tricks. Today, we will shed some light on Git Amend feature and explore its handy use cases.
Imagine that you make your changes on a branch and commit them but right before pushing the commit to the remote origin, you realize that:
- Some files were not included in the commit.
- There is a typo in the commit message or maybe it could even benefit from a better message.
At this point, one might think of doing a git reset
or perhaps creating a new commit; however, there exists a more charming and cleaner solution.
Introducing Git Amend
The git commit --amend
command lets us tinker with the most recent commit instead of resorting to creating a whole new commit or conducting a git reset
by replacing the existing commit with the amended one.
Let’s see it in action:
First, we create three files named foo
, bar
and baz
in our branch, stage foo
and bar
then commit them:

Oops, what about baz
? Let’s reunite it with its companions and use the amend
feature to pretend we did not leave him out:

Interesting! We can see foo
and bar
are added again even though they were included in the initial commit. This is a manifestation of the fact that we are rewiring our Git history.
Also, you might be wondering about the --no-edit
switch at the end. Simply put, if we do not specify it, Git will show us a prompt, asking for a commit message, otherwise, the previous commit message will be used. In our case, Add very important files!!!
.
What if we wanted to provide a better commit message or fix some typos using the amend
command? Basically, there are two options available for us:
- Use
git commit --amend
and when you press enter, a prompt will pop up, asking for a new message. - Or use the
git commit --amend
-m "my message"
Looking at my initial commit message, I reckon using the exclamation marks was not really necessary. Let’s modify the message using the second approach:

Looks better.
But how do all these changes appear in the git log
output?

I would say, it is pretty much clean and no one knows that we mistakenly left out baz
or changed our grumpy commit message.
It is worthwhile to mention that the steps we talked about here are to be used before the changes are pushed to a remote repository. If you push the commits and then use the amend feature, you will have to forcefully push the commit and that will not be very pretty for others who already have your commit in their branch.
Summary
git commit --amend
provides a means for us to make changes to our most recent commit, be it adding or removing files, editing the existing files, or changing the commit message. Nonetheless, this command has to be used before pushing your commit to a remote repository.
We hope that you find this post useful and stay tuned for our next blog posts! You can also check our other posts here.