How to amend a git commit

3 ways to undo a git commit

There are three commonly used approaches a developer can take to undo their last Git commit and put a new commit in its place:

  • A git revert followed by a new commit.
  • A git reset followed by a new commit.
  • A simple git commit amend command.

Of those three, the option to amend a git commit is by far the easiest.

How to amend a git commit

To undo and remove the last commit in your history with the git amend command follow, these steps:

  1. Add, update or remove files as required to fix your local workspace.
  2. Stage your updated files with the git add –all command.
  3. Perform a git commit –amend command to undo the previous commit.

Git commit remove vs git amend

When you amend a Git commit, this removes the old commit from your branch’s history, and a brand new commit with the updated state of your workspace is put in its place.

The old commit becomes orphaned in your local workspace. The new, amended commit replaces the previous commit at the tip of the currently selected branch.

As such, it is more accurate to say the git commit amend command amends the Git commit history as opposed to amending the last commit itself. When you do an amend, you remove a Git commit from the branch history, rather than update or change the existing one.

Git commit amend example

Here is a simple git commit amend example that undoes the commit that added a file named alpha.txt to the repository.

The amended Git commit in this example not only removes the alpha.txt file from the commit history, but it also adds a new file named bravo.txt.

$ touch alpha.txt
$ git add alpha.txt
$ git commit -m "undo this git commit!"
$ rm alpha.txt
$ touch bravo.txt
$ git add --all
$ git commit --amend "undo the previous git commit"

Benefits of the git commit amend command

By far, the easiest way to undo a previous commit is with the git commit amend command.

Both the reset and revert approaches require the user to issue an extra command to undo the previous Git commit. With the git commit amend command, you simply edit files and perform a single commit as you normally would, with the only change being the addition of the –amend flag.

The git commit amend option is simple and easy to use.

Git commit amend vs git reset

One drawback to the git commit amend command is that it only works on the most recent commit.

Compare this to the git reset command that allows you to undo any number of commits in your local history.

Furthermore, a hard Git reset has the power to reset all of the files in your local workspace back to the way they were when a previous commit occurred. The git commit amend does not include this functionality.

One problem with both the git commit amend and git reset commands is that they create orphan commits in your local repository.

This is why you should only use the git reset or git commit amend commands to undo local commits that have not been pulled from a team repository or pushed to a remote repo. If you amend or reset shared commits, this will corrupt the shared branch history for every member on the team.

amend git commit message

The git commit amend command is often used to quickly update or change a Git commit message.

Git commit amend vs git revert

When you undo a Git commit with a reset instead of an amend, the system preserves the previous commit in your history and creates a new commit that represents the undone commit. As a result, the git revert command is a much safer option when manipulating a shared Git commit history.

However, the fact that a revert preserves the Git commit you are trying to undo is also one of its drawbacks. Any errors or mistakes you attempt to hide remain in your local repository and become part of the public Git commit history as soon as you push to GitHub, GitLab or BitBucket.

When you undo a Git commit with an amend, the old orphaned commit is never shared externally. Even locally it is not visible with a git log command. Only a git reflog reveals its existence.

There are many ways to undo a previous commit, but the git commit amend command is far and away the easiest, especially if the commit to be undone has never been pushed to or pulled from a shared repository.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close