How to With Git: Undo Commit

How to With Git: Undo Commit

Sometimes you might commit changes to your Git repo, but then realize you made a mistake. Now you want to undo those commits to get your code working again.

In this guide, we’ll look at the git reset command for local and remote commits to a repository.

Difference Between a git reset and git revert

It’s important to understand the difference between resetting vs reverting commits when using Git.

  • The git reset command undoes commits by removing previous commits from the repository and resetting the Git HEAD to an earlier commit. Some Git history might be lost by doing this, depending on what option is used.
  • The git revert command undoes commits by creating a new commit that represents an earlier state of the repository. No Git history will be lost by doing this.

Already pushed changes and someone else pulled those changes? You should not use git reset to undo changes, you should use git revert as described in our Git rollback commit guide instead.

Using the git reset Command to Undo a Commit

For the following examples, assume our local Git tree looks like this:

The X represents our local, uncommitted change to our Git HEAD, indicated by C.

To undo the commit named “C “due to a mistake, we can run a git reset command.

Depending on file tracking needs, you’ll want to use the git reset command with different flags.

Case 1: Use git reset and Remove Files From the Staging Area

We can use the git reflog command to see our Git history.

We can use git ls-files to see the currently staged files for our project:

To keep any tracked files since commit C, but remove them from the Git index, you can run:

You can use the Git shorthand of HEAD~1 to remove the commit one commit before HEAD.

If you used HEAD~5, this would remove the commit that is five commits before HEAD.

Changes to the Git index, also called the “staging area,” will be lost, so the Git tree would now be:

Which we can confirm with these commands:

When just using git reset with no flags, we don’t remove any files; they are just un-staged:

You do end up removing the log entry for the commit you reset, however:

Case 2: Use git reset –soft and Preserve Changes to Files

If you use the –soft flag, any changes to tracked files and the Git index are preserved:

Any files that were staged remain so, and no files are physically removed:

Using the –soft flag, you still end up removing the log entry for the commit you reset.

Case 2: Use git reset –hard and Remove All Changes to Files

If you use the –hard flag, any changes to tracked files and the Git index are lost:

All files after the commit you reset to are un-staged and physically removed:

You again remove the Git log entry for the commit that you reset.

Try to Use git revert When Commits Are Already Pushed

As stated above, if you’ve already pushed your changes and someone else pulled in those changes, you should not use git reset to undo changes, use git revert instead.

However, if you really want to, you can apply the same steps as in the previous section to delete some previous Git commits.

After that, you can do a git push -f to use the force option. Again, this is not recommended, because it can create serious conflicts between the various repository states of other Git users.

Questions or Comments? Ask Jacob!

Ask a question and Jacob will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.