How to use Git revert In GIT

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>

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