Undoing a Commit in Git

By | May 7, 2023

If you have committed the wrong files accidentally to Git. However you did not push the commit to the server yet. Then there are various ways that you can undo those commits from the local repository. We will dicuss four different methods you can use to undo a commit in this article.

Scenario Setup

Let’s assume you have the following commit history, where `C` represents your current `HEAD` commit and `(F)` denotes the state of your files.

   (F)
A-B-C
    ↑
  master

Option 1: Using `git reset –hard`

If you want to completely discard commit `C` and any uncommitted changes, you can use the following command:

git reset --hard HEAD~1

Result will be:

 (F)
A-B
  ↑
master

With the `–hard` option, the `HEAD` pointer moves back one commit to `B`, and your files are reset to the state at commit `B`.

Option 2: Using git reset

If commit `C` is not a disaster but needs some adjustments before committing again, you can undo the commit while keeping your changes for further editing. Starting from the previous scenario:

   (F)
A-B-C
    ↑
  master

You can use the following command, omitting the `–hard` option:

git reset HEAD~1

Result will be:

   (F)
A-B-C
  ↑
master

In both cases, the `HEAD` pointer moves back one commit (`B`), but your files remain as they were in commit `C`. The changes you had checked into commit `C` are preserved, allowing you to continue working on them.

Option 3: Using `git reset –soft`

If you prefer a lighter approach, you can undo the commit while preserving both your files and the changes in the index. This can be achieved with the following command:

git reset --soft HEAD~1

This command keeps your files and index untouched. When you run `git status`, you’ll notice that the same files remain in the index as before. If you proceed to commit immediately after this command, you would essentially redo the same commit you just undone.

Option 4: Recovering a Commit

In case you mistakenly destroyed a commit using `git reset –hard` and later realize you need it back, there is still a way to recover it. Follow these steps:

  1. Run the command `git reflog` to view a list of partially referenced commit SHAs (hashes) that you have modified.
  2. Locate the commit you destroyed in the list and copy its SHA.
  3. Execute the command:
    git checkout -b someNewBranchName shaYouDestroyed
    

By doing this, you resurrect the commit by creating a new branch `someNewBranchName` pointing to the commit you had previously destroyed.

Keep in mind that Git retains commits for a considerable period (usually 90 days) even if they appear to be destroyed. Therefore, you can usually recover commits that were accidentally removed.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.