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.

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

Be the first to comment on "Git revert specific commits from the command line"

Leave a comment

Your email address will not be published.


%d bloggers like this: