Skip to content
Home » Git revert specific commits from the command line

Git revert specific commits from the command line

git revert

There can be cases where you want to revert a branch or commits in git but due to many other commits on the top of it, Github does not show revert button for branch/commits or does not allow to revert due to other commits on the top of it.
(The Github User Interface where it is quick and easy to revert anything from revert button)

So in that case, specific commits/branch can be reverted from the command line to solve this problem.

Below are the steps to revert a commit:

  1. Check out the branch from where you want to revert the commit and rebase the branch. Here I am taking a master branch.
    git checkout master
    git pull –rebase origin master
  2. Create a new branch and switch to a new branch ‘revert_pull_request’
    git checkout -b revert_pull_request
  3. Revert the commit. need to specify -m 1 because it is a merge commit:
    git revert -m 1 6956d588g3
  4. Push your changes:
    git push origin revert_pull_request
  5. Now you can merge the new branch to the master branch to revert the changes done in a commit.

Leave a Reply

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

0 Shares
Tweet
Pin
Share
Share
Share