Undoing a Commit in Git

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 07 May, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What are the advantages of the Internet?

    Here in this article we will discuss about the various ways through which the net is headed up in the world. Employment Opportunities: Employment opportunities will get open…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read
  • Understanding Stock Markets: A Beginner’s Guide to NSE

    Introduction If you are a beginner in the stock markets, then let us first understand its basics. The stock market is also known as the share market. It…

    4 min read
  • Why choose python for your Android app development

    Do you know how frequently you check your smartphone in a day? No? Well, according to various reports, the average number is an astonishing 100+ times a day.…

    6 min read
  • Writing First C++ Program – Hello World Example

    C++ is Object-Oriented Programming (OOP) language. C++ is easy to understand. You can learn C++ by following these steps: Writing your program in a text editor. Then saving…

    3 min read
  • Setting up C++ Development Environment

    C++ is programming language. It is object-oriented, and generic programming paradigms. C++ is compatible with multiple platforms such as Windows, Linux, Unix, and Mac. Using Online IDE Before…

    7 min read