Git How to Add Changes to Last Commit (git commit – -amend)

To change the most recent commit in Git, use the git commit –amend command. In other words, stage your new changes as you would stage any other changes using git add. Then run git commit –amend to attach the staged changes to the previous commit.

$ git add <file-name>
$ git commit --amend

Notice that if your most recent commit already exists in the remote repo, you need to do a forced push:

git push --force-with-lease

This guide teaches you how to add more changes to your most recent commit. It also teaches you how to modify the most recent commit that already lives on the remote repository. You’ll also learn how to change the commit message of the most recent commit. The theory is backed up with great examples.

Let’s jump into it!

git commit –amend: Change the Last Commit

Sometimes you might find yourself wondering how to change the contents of the most recent commit or how to modify the message of it.

Because this is such a common necessity, Git has a really neat solution to it.

Changing the most recent commit happens with the git commit –amend command. The –amend option tells Git to rewrite the last commit.

Before running git commit –amend, however, you need to stage the new changes with the git add command unless you’re just looking to change the message of the most recent commit.

In the latter case, you don’t need to add anything or make a change. Instead, you can just call the git commit –amend straight away.

Anyway, here’s how you can add the most recent changes to the most recent commit:

$ git add <file-name>
$ git commit --amend

Amending the commit opens up an interactive git commit message editor where you can change the commit message of the most recent commit. This is why the popular answer to “How to change git commit message” is by using the git commit –amend command.

Notice that if you’re only working on a local repository, amending a commit doesn’t cause problems.

But if your commit already lives in the remote repo, there’s going to be a contradiction between your local repo and remote repo.

This is because when you amend a commit, you’re essentially rewriting history, that is, the last commit. This is because you’re changing what the most recent local commit initially was. On the remote repo, the most recent commit is still what it used to be. When this happens, the local repo and the remote repo differ from one another which prevents pushing the changes to the remote. To overcome this, you need to do a forced push.

The safest way to do a forced push is by using the –force-with-lease flag to prevent your forced push from overwriting other developers’ code.

$ git push --force-with-lease

Let’s see some concrete examples of changing the contents of the most recent commits.

Example 1. Add Changes to the Most Recent Commit

Let’s have a look at a concrete and simple example to support understanding.

Let’s say you have a Git project with a text file where you’ve added the following line:

Besides, you’ve committed that change and it shows up as the most recent commit when checking the git log:

It’s only now you realize that the sentence added to the file doesn’t end with a dot. Let’s change it.

Now you need to add and commit this tiny change, don’t you?

But this is not optimal as the change you’ve made is so small and so heavily related to the previous commit.

In this case, it’s a much better idea to amend this change to the previous commit instead of creating a new one. To do this, let’s stage the changes and call git commit –amend to add the staged changes to the previous commit.

$ git add .
$ git commit --amend

Running git commit with the –amend option opens up an editor where you can also change the commit message of the previous commit if you wish.

Once you close the editor, the commit will be amended to the previous commit instead of creating a new one. Pretty neat, isn’t it?

If you haven’t pushed the change to remote yet, you can do it with the regular git push command. BUT if you have already pushed the earlier version of the commit to the remote, you’re going to see an error. The next example demonstrates this.

Example 2. Pushing an –amend Commit

Let’s take a look at another concrete example that demonstrates the issue with pushing an amended commit to remote.

Say this is the commit history of our example repo and that all of these commits are pushed to the remote:

Now, let’s change the most recent commit. To keep it short, let’s only change the commit message. You can do this with the git commit –amend command.

$ git commit --amend

This opens up the commit message editing view. I’ll change the commit message from “Add a sentence to the text file” to “Add an example sentence to test.txt“.

After changing the message, here’s what the local commit history looks like:

Take a look at the last commit (on the top). It now has a different message and the hash has also changed.

This means we just rewrote the commit history.

  • The remote repository thinks that the last commit was 0bcde9d and it says “Add a sentence to the text file
  • But the local repository claims the most recent commit is 417950b and says “Add an example sentence to test.txt

This is obviously a contradiction as the remote and local repositories don’t agree about the history. If you try to push the amended commit to the remote, it fails.

This error happens because the last commit in the remote is not the same as the last commit you have locally.

To fix this, all you need to do is a forced push. In other words, you want to push your changes to the remote no matter what it says.

git push --force-with-lease

The –force-with-lease option makes a safer force push. It ensures you don’t accidentally overwrite someone else’s work when doing a force push.

Now you’ve successfully changed the most recent commit and pushed it to the remote repo as well!

Summary

Today you learned how the change the most recent commit using the git commit –amend option.

If you’re changing a local commit, running this command is all you need.

But if you’re changing a commit that already exists on the remote, then you need to do a safe force push to the origin.

This is because changing the contents of a commit is considered changing the history of the project. And when you change history, your local and remote repositories will disagree as to what was the most recent commit. A force push solves the issue.

To safely push to the origin, do a git push –force-with-lease. This ensures no commits are overwritten by accident.

Thanks for reading. Happy coding!

Read Also

Scroll to Top