Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@todgru
Created December 10, 2012 18:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/4252540 to your computer and use it in GitHub Desktop.
Save todgru/4252540 to your computer and use it in GitHub Desktop.
oops git reverse commit back out

#Oops, I merged untested changes to master!

You've made changes to master, commited and push up to your repo. Git is flexible enough that you can dig yourself out rather easily. Here are two of many options to get back to a safe place.

  • Option 1, Branch out and keep your changes.
  • Option 2, Reset the branch and lose changes.

The final step in both of these options is $ git push --force origin master.

##Option 1: Branch Out

Derived from Stack Overflow how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit

Checkout master

$ git checkout master

Rename master branch:

$ git branch -m my_mistake_master_branch

Checkout the <sha1> of the last good commit:

$ git checkout <sha1>

Create and checkout your new master branch:

$ git checkout -b master

Locally, master banch is back to your last known good. Now force push these changes up to the repo:

$ git push --force origin master

Finally, I would check your github.com// to make sure all is good.

##Option 2 Reset

Derived from: Stack Overflow how-to-delete-a-git-commit

Stop. Branch first, and then do this, because git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES

Assuming you just commited, this will reset your repo back one commit, the HEAD~1 means the commit before HEAD:

$ git reset --hard HEAD~1

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

$ git reset --hard <tag/branch/commit id>

If you already pushed your commits, you will need to do a force push of your local repo:

$ git push origin HEAD --force

or $ git push origin master --force

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