Skip to content

Instantly share code, notes, and snippets.

@ateucher
Last active March 18, 2016 22:28
Show Gist options
  • Save ateucher/e0e24d8f3424da2688ce to your computer and use it in GitHub Desktop.
Save ateucher/e0e24d8f3424da2688ce to your computer and use it in GitHub Desktop.

Rolling back a bad commit

So, you've committed some changes to your repository that you want to roll back. This will tell you how to roll that back. It assumes that you want to pretend that the offending commit(s) never happened. Note that this workflow is the simplest solution, and works well for us because we are a small team that communicates well, but it is generally considered bad practice because it rewrites history and can mess up collaborators.

  1. Find the commit that you want to roll back to, and copy the SHA (the unique alphanumeric string that identifies the commit).

  2. In git bash, type:

    git reset --hard theSHA
    

    This rolls back your local copy of the repository to the commit specified by the SHA. If you have not pushed the bad commit(s) to a remote (e.g., GitHub), you can stop here.

  3. If you have pushed to a remote, you will need to 'force push' your reversion to the remote, because now it looks like you local repo is behind the origin, and a regular push will be rejected:

    git push -f origin branchname
    
  4. Let your collaborators know that you've done this, because you have now rewritten history, and it will affect them if they had previously pulled down the commits you've just reverted. They won't be able to do a regular pull, they will have to reset their local copy to mirror the origin: First make sure you are on the proper branch:

    git checkout branchname
    

    Then fetch the remote and hard reset your branch to the state of the branch that was just force-pushed:

    git fetch --all
    git reset --hard origin/branchname
    

Resources:

http://willi.am/blog/2014/08/12/the-dark-side-of-the-force-push/

http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit

http://stackoverflow.com/questions/9813816/git-pull-after-forced-update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment