Git revert command

Git revert command-Example

 git revert command will undo the changes for a particular commit and unlike git reset command it will not delete the commit details.

Prerequisite:- A local repo where you are running the below commands.

Step1 :-> Create a file called package.json and commit the changes into your local repo.

              touch package.json

              git add package.json

              git commit -m "package.json file is added"

Step2 :-> Modify package.json file and commit it again

              echo "{ course: devops, batch: freshers }" >> package.json

              git add package.json

             git commit -m "package.json file is modified"

Step3:- Verify the git logs

            git log --oneline

     In my case the commits looks like below

753e97d (HEAD -> master) package.json file is modified

0c94fe2 package.json file is added


Step4: - Revert back the changes related to the modification of the package.json file.

              git revert 753e97d

Step5:- Check git log entry, the commit should not be deleted and there must be a new commit that is related to revert changes.package.json file should be empty.

             git log --oneline

             cat package.json

Below are my git log details.

466a416 (HEAD -> master) Revert "package.json file is modified"

753e97d package.json file is modified

0c94fe2 package.json file is added


Step6:- Bring back the changes of package.json file.

           git revert 466a416

            git log --oneline

           cat package.json 

Comments