Open In App

Undo a Commit in Git

Last Updated : 04 Oct, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

In Git, sometimes you may need to undo a commit due to errors or changes in your code. Git offers several commands to help you undo or revert commits depending on your needs and whether the commit has been pushed to a remote repository.

Methods to Undo a Commit in Git

1. Using Git reset

Step 1: First check all your commits

#git log

Output: commits are just examples or sample commits

commit 2: second commit  
commit 1: First commit

Perception drawn are as follows:

  • commit 2 is the commit you want to undo, the current head is here
  • commit1 is the first commit where you want to go after undo

Step 2: To restore everything or undo all the changes we have to reset the commit.

#git reset --soft HEAD^      
#git reset --hard HEAD^  

Note: 

  • soft is used if you want to keep your changes
  • hard is used if you don't want to keep your changes

Step 3: To check your commit is reset or not

#git log

Output:

commit 1: First commit   
//undo the second commit, now head is at first or previous commit

 One can clearly see last commit (i.e. second commit) is removed.

2. Using Git revert

Now if we have already made your commit public then you will have to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).

Step 1: Revert your changes 

#git revert HEAD  

We are now ready for your new commit in order to restore the file that we accidentally have remove with the below command as follows:

#git commit -m 

Step 2: Now check your all commits to see the list of commits

#git log

Output:

commit 3: restoring the file that we accidentally remove  
commit 2: removing a file we don't need
commit 1: Needed file

Now we can revert your last commit.

Also do note that we use the command below specified to undo the last commits in git where the head is a pointer pointing to the last commit in our branch 

git reset HEAD~<no-of-commits>

3. Using git checkout

The git checkout command can be used to create a new branch from a specific commit. This method is helpful when you want to keep the original branch unchanged.

Steps to create a new branch from a specific commit:

1. Identify the commit hash you want to create a branch from.

git log

2. Use the 'git checkout' command to create a new branch.

git checkout -b <new-branch-name> <commit-hash>

Example:

git checkout -b new-feature a1b2c3d

Suggested Quiz
5 Questions

Which command is used to undo the last commit while keeping the changes in the working directory?

  • A

    git reset --hard HEAD^

  • B

    git reset --soft HEAD^

  • C

    git revert HEAD

  • D

    git checkout -b branch HEAD

Explanation:

git reset --soft HEAD^ removes the latest commit but keeps the changes staged, allowing you to recommit or modify.

Which command should be used when you want to undo a public commit without altering commit history?

  • A

    git reset --hard HEAD^

  • B

    git checkout -b newbranch

  • C

    git revert HEAD

  • D

    git reset HEAD~2

Explanation:

git revert HEAD creates a new commit that reverses the previous one, recommended for public/shared repositories to avoid rewriting history.

What is the difference between git reset --soft and git reset --hard?

  • A

    Soft deletes commits; hard keeps them

  • B

    Soft keeps changes; hard removes them from working directory

  • C

    Hard keeps changes staged; soft unstages them

  • D

    No difference

Explanation:
  • --softkeeps all changes unstaged/staged
  • --harddeletes commits and working directory changes permanently

Which command can be used to undo multiple recent commits?

  • A

    git revert HEAD~3

  • B

    git reset HEAD~<no-of-commits>

  • C

    git checkout HEAD^

  • D

    git merge --abort

Explanation:

git reset HEAD~<number> resets the branch back by defined number of commits.

If you want to explore an older commit without modifying the current branch, what command helps create a new branch from that commit?

  • A

    git reset --soft HEAD^

  • B

    git revert HEAD

  • C

    git checkout -b <new-branch-name> <commit-hash>

  • D

    git clean -fd

Explanation:

git checkout -b newBranch <commit-hash> creates a new branch starting at a specific commit, keeping original branch unchanged.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Explore