How to use Git reset In GIT

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.

How to Undo a Commit in Git ?

In Git if there is any mistake made while making commits, we can undo it using the git reset and revert commands. Git helps developers to manage their projects efficiently. However, even the most experienced developers occasionally make mistakes or need to backtrack on their commits.

Below are the approaches to Undo a commit in Git:

Table of Content

  • Using Git reset
  • Using Git revert

Similar Reads

Using Git reset

Step 1: First check all your commits...

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)....