Skip to content

Instantly share code, notes, and snippets.

@Swivelgames
Last active August 28, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swivelgames/73432913e04e823563aaf8eaad010771 to your computer and use it in GitHub Desktop.
Save Swivelgames/73432913e04e823563aaf8eaad010771 to your computer and use it in GitHub Desktop.
Simple revert all commits between HEAD and COMMIT_X in git

Grab the commit you want to revert back to

$ git log

You should see a less'd list of your log in descending chronological order (top of the list is your most recent commit).

  • Find the commit you want to revert back to.
  • Copy the hash (hexadecimal string, like: "b4533eeac95f970d131452b08db9094e8e07a260")
  • Press "q" to quit

GRACEFUL REVERT

Do this to keep your changes in the history, but switch back to a previous commit; Good in case you have merged into another branch or someone else has a copy of your branch.

me@local:mygitrepo$ git diff HEAD..$HASH_YOU_COPIED > diff.patch
me@local:mygitrepo$ git apply diff.patch
me@local:mygitrepo$ rm diff.patch
me@local:mygitrepo$ git commit -a -m "Reverted back to HASH_YOU_COPIED"

Check to make sure that everything is back to the way that it was before.

Then:

me@local:mygitrepo$ git push

FORCEFUL REVERT

WARNING Only do this when your code hasn't been merged into develop yet!

me@local:mygitrepo$ git reset --hard $HASH_YOU_COPIED

Check to make sure that everything is back the way that it was before.

Then:

me@local:mygitrepo$ git push --force

This will force the remote to the hash that you reset your tree to

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