Skip to content

Instantly share code, notes, and snippets.

@MeteSayan
Created August 26, 2023 21:21
Show Gist options
  • Save MeteSayan/c7c948bdc69a010b6e3db75f23dfc0ad to your computer and use it in GitHub Desktop.
Save MeteSayan/c7c948bdc69a010b6e3db75f23dfc0ad to your computer and use it in GitHub Desktop.
Undo Changes in Git

Undo Changes in Git

Undoing Local Changes That Have Not Been Committed

$ git status # List affected files
$ git checkout filename.html 
# File has now been reverted to previous commit

Undoing a Specific Commit (That Has Been Pushed)

$ git status # Make sure you have a clean working tree
$ git log --oneline # Find your commit's unique hash
$ git revert <hash> --no-edit
$ git push
# This will make a new commit and will revert the file(s) to their previous state as if it was never changed.

Undoing Your Last Commit (That Has Not Been Pushed)

$ git reset --soft HEAD~ # Revert last commit
OR
$ git reset --soft HEAD~2 # Revert last 2 commit (You can change number)

Undoing Local Changes That Have Been Committed (But Not Pushed)

$ git status # Make sure you have a clean working tree
$ git log --oneline # The one you want to revert back to
$ git reset <hash>
$ git reset --hard <hash>
# If you do git reset the commits will be removed, but the changes will appear as uncommitted, giving you access to the code. 
# If you dogit reset --hard the commits will be removed and the changes will disappear.
@aslimutlu
Copy link

This is very useful content. Thanks!

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