Resetting Branch to a specific pushed commit

Sometimes, you just push a bad commit into a branch of a git repository and later we realise the mistake. The first question that comes to your mid is “How to reset branch to a specific pushed commit?.

This is what my latest commits looks like on a git repo “myWork” on the branch deploy.

Lets say, I want this deploy repo to have code committed till “sprint 6” and remove the code pushed after in the commits, Bad commit 1 and Bad commit 2

To achieve this, git provides an option to reset the branch to the commit you want.

Table of Contents

Git Reset

Make sure you are on the branch where the commit is. In my case it is deploy and the remote is called origin.

Then use git reset –hard <commit-hash> to set the current branch HEAD to the commit you want.

git reset --hard 62202a4
git push --force origin deploy

When we push the change to remote when need to use force. This is how the commits looks on my myWork origin now.

Click here for more technology blogs

Leave a Reply

Your email address will not be published. Required fields are marked *