Git Basics: Adding more changes to your last commit

With this post you'll learn how to modify your last commit, adding (or removing) some changes. You can also follow this tutorial to learn how to edit your last commit message.

I assume you are working on a branch alone to avoid messing around with your teammates’ work. Remember: shared branches are usually very hard to maintain.


So you have a commit history like this one (older commits on top):

f7f3f6d create file_a class and methods
310154e improve SomeOtherClass code
a5f4a0d update changelog file

But you forgot to add just a single line on the changelog file you mentioned on the commit a5f4a0d :(

You could simply commit it and have a history like this:

f7f3f6d create file_a class and methods
310154e improve SomeOtherClass code
a5f4a0d update changelog file
a4gx124 add missing line to changelog

But that’s just not good for your history (it’s just a change on a change you already did). The best case scenario here is to have both changes in a single commit.

And you can achieve that with the simply amazing amend command!

Just remember: do not commit any of the changes you want to add to your last commit before doing these steps!

The Basic of the Amend Command

Just add the modified file(s):

$ (some_branch) git add changelog.md

And amend it:

$ (some_branch) git commit --amend

After executing the command, this file will show up (probably on vi/vim. check this cheatsheet if you don't know how to use it):

update changelog file
# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
#
# Author: John Doe <john@doe.com>
# Date: Thu Apr 21 22:40:30 2016 -0300
#
# On branch some_branch
# Your branch is up-to-date with ‘origin/some_branch’.
#
# Changes to be committed:
# modified: changelog.md

Here you can edit your commit message. Do as you wish and then save the file. If you don’t want to change the message, just save the file without changing it.

Now the changes you did on the last commit and after it will be in the same commit!

Amending a Commit Without Changing Its Message

If you don’t want to change your commit message, you can run the amend command with the no-edit flag, like this:

$ (some_branch) git commit --amend --no-edit

You’ll not be "redirected" a file to edit the commit message and that’s it!

Pushing an Amended Commit

If you haven’t pushed the last commit yet to your remote, a single push is enough. Otherwise you’ll have to push using the -f option, since you’ve rewritten the your commit history:

$ (some_branch) git push -f origin some_branch
Be warned: pushing with -f is a very dangerous thing. Proceed with caution and always be sure that you’re pushing to the right branch.
Remember: NEVER rewrite the commit history of public branches (like master). This will truly mess your teammates work.

Bonus 1: Just Editing a Commit Message

To just edit a commit message (without adding new changes to your last commit), just run the amend command without adding changes. Simple as that!

Bonus 2: Editing a Commit Without Opening a File

Of course there would be a command for that! Just run:

$ (some_branch) git commit --amend -m "Your new commit message"

And there you go!

If you wanna learn how to use Git with teams, you should check this other post ;)

Happy gitting!