How to revert git repository to previous commit?

Problem:

Sometimes, we commit code mistakenly in the branch and we want to remove the mistakenly committed code.

Solution:

We can use the git reset command to reset our changes to commit id up to which we want to revert our code changes using below three steps (git commands)-

git log –oneline

Use this command to get all the commits done so far along with commit id

git reset CommitId –hard

Use this command to reset your changes to the commit you want to keep. Please remember that all following commits after this commit will be lost. Commit Id you can get using above command i.e

git log –oneline

git push -f

Your changes will not reflect until and unless you execute this command. You need to provide this command after above command i.e git reset commitId –hard.

Example Screenshot-



Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply