Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created November 17, 2013 09:50
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save stevenyap/7511407 to your computer and use it in GitHub Desktop.
Save stevenyap/7511407 to your computer and use it in GitHub Desktop.
Git reset and revert: Undoing changs

Difference between reset and revert

  • Reset rewinds history (files + commits) back to the previous commits
  • Revert rewinds your files back to the previous commits by adding a new commit to show this
  • You should use revert (especially if you have pushed) as it does not rewrite history

Git Reset

# If you are pulling, rebasing or your new code is a mess, and you want to return to the last committed point:
# Note that this does not delete newly created files
# git clean -f -d will remove newly created files and directories (BEWARE!)
git reset --hard

# reset to the last commit
# HEAD is the current commit, HEAD^ is the last commit
# HEAD~2 is the 3rd, HEAD~3 is the 4th and so on...
git reset --hard HEAD^

# reset to a particular commit
git reset --hard be47384a

Git Revert

# Revert the commit 0766c053
# Note that commit may not necessary be the last commit, it can be ANY commit
git revert 0766c053

# Revert the changes specified by the fourth last commit in HEAD and create a new commit with the reverted changes.
git revert HEAD~3

# Revert the changes done by commits from the fifth last commit in master (included) to the third last commit in master (included), but do not create any commit with the reverted changes. The revert only modifies the working tree and the index.
git revert -n master~5..master~2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment