How to Undo a Git Commit: Revert, Reset and Amend Explained

5
(1)

Scenario:
Make
: Git / GitHub
Platform: Windows, Linux and macOS
Mode: CLI [Command Line Interface] and GitHub Desktop
Description: In this article, we will discuss how to undo a Git commit the right way. You will learn when to use git revert, git reset (soft, mixed and hard) and git commit –amend, how to undo a commit that has already been pushed to GitHub, how to move a commit made on the wrong branch, and how to recover if something goes wrong.

Quick Answer: Which Command Should You Use?

  • Undo the last commit but keep your changes (not pushed yet) – git reset –soft HEAD~1
  • Undo the last commit and delete its changes (not pushed yet) – git reset –hard HEAD~1
  • Fix the last commit message or add a forgotten file – git commit –amend
  • Undo a commit that is already pushed to GitHub – git revert <commit-id>

The most important question is whether the commit has been pushed. If it is only on your machine, you can safely rewrite history with reset or amend. If others may already have pulled it, use revert, which undoes the commit without rewriting history.

Before You Start: Find the Commit

Open a terminal in your repository and list the recent commits. Each commit has a short ID that you will use in the commands below:

edledge-git:~/github$ git log --oneline -5
e435475 (HEAD -> edledge) Update DNS zone file
a1b2c3d Add new records
9f8e7d6 Initial zone configuration

Here, e435475 is the latest commit, and HEAD points to it. HEAD~1 means “one commit before HEAD”. If Git is not installed yet, see Install Git on Windows or Install Git on Linux.

Method 1: Undo the Last Commit and Keep the Changes

Use this when you committed too early, or want to split or redo the commit. Your file changes stay in place:

edledge-git:~/github$ git reset --soft HEAD~1

With –soft, the changes remain staged, ready to commit again. If you want them unstaged instead, use the default mixed mode:

edledge-git:~/github$ git reset HEAD~1

Method 2: Undo the Last Commit and Discard the Changes

Use this only when you want to throw the commit and its changes away completely:

edledge-git:~/github$ git reset --hard HEAD~1
HEAD is now at a1b2c3d Add new records

Important Note: git reset –hard also deletes any uncommitted changes in your working folder. Run git status first and make sure there is nothing you want to keep.

Method 3: Fix the Last Commit with Amend

If the only problem is a wrong commit message, replace it:

edledge-git:~/github$ git commit --amend -m "Update DNS zone file for edledge.com"

If you forgot to include a file, stage it and add it to the last commit without changing the message:

edledge-git:~/github$ git add zones/edledge.com.yaml
edledge-git:~/github$ git commit --amend --no-edit

Amend rewrites the last commit, so use it only on commits you have not pushed.

Method 4: Undo a Commit Already Pushed to GitHub

Once a commit is on GitHub, the safe way to undo it is git revert. It creates a new commit that reverses the changes of the chosen commit, so the history stays intact for everyone else:

edledge-git:~/github$ git revert e435475 --no-edit
[edledge 6e16d16] Revert "Update DNS zone file"
 1 file changed, 3 insertions(+), 131 deletions(-)
edledge-git:~/github$ git push

Without –no-edit, Git opens an editor so you can change the revert message. You can revert any commit in the history, not just the latest one.

On a branch that only you use, you could instead reset and force-push, which removes the commit from GitHub entirely:

edledge-git:~/github$ git reset --hard HEAD~1
edledge-git:~/github$ git push --force-with-lease

Important Note: Never force-push to a shared branch such as main. It rewrites history for everyone who has already pulled it. Use git revert instead.

Committed to the Wrong Branch?

If you made a commit on the current branch but it belongs on another one, and you have not pushed it yet, move it.

If the correct branch does not exist yet, create it at the current commit, then remove the commit from the current branch:

edledge-git:~/github$ git branch new-feature
edledge-git:~/github$ git reset --hard HEAD~1
edledge-git:~/github$ git switch new-feature

If the correct branch already exists, copy the commit there with cherry-pick, then remove it from the wrong branch:

edledge-git:~/github$ git switch correct-branch
edledge-git:~/github$ git cherry-pick e435475
edledge-git:~/github$ git switch wrong-branch
edledge-git:~/github$ git reset --hard HEAD~1

Undo a Commit in GitHub Desktop or on GitHub.com

  • GitHub Desktop – for the latest commit that has not been pushed, click Undo at the bottom of the left panel. For any other commit, open the History tab, right-click the commit and choose Revert Changes in Commit, then push.
  • GitHub.com – for a merged pull request, open the pull request and click Revert. GitHub creates a new pull request that undoes the merge, which you can review and merge.

Made a Mistake? Recover with Reflog

Even after git reset –hard, Git usually keeps a record of where your branch pointed before. Use reflog to find the lost commit and restore it:

edledge-git:~/github$ git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
e435475 HEAD@{1}: commit: Update DNS zone file
edledge-git:~/github$ git reset --hard e435475

Frequently Asked Questions

What is the difference between git revert and git reset?

git revert adds a new commit that cancels out an earlier one, so history is preserved and it is safe for pushed commits. git reset moves the branch back to an earlier commit, removing the later commits from history, so it should only be used on commits that have not been pushed.

How do I undo the last three commits?

If they are not pushed, run git reset –soft HEAD~3 to keep the changes, or git reset –hard HEAD~3 to discard them. If they are pushed, run git revert –no-edit HEAD~3..HEAD, which creates one revert commit for each of the three.

Can I undo a git revert?

Yes. A revert is just another commit, so you can revert the revert commit using its commit ID.

Disclaimer: This article is for educational purposes only and is not professional financial, medical, or legal advice. It may contain affiliate links, which earn us a small commission at no extra cost to you. See our Disclaimer and Affiliate Disclosure.

Article ID: EA00146

Worth your time? Give it a rating

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×