Undoing Changes
Undoing Changes
Discard unstaged changes to a file
$ git restore <file>
Reverts the file in your working tree to match the last commit. Uncommitted edits are lost.
Unstage a file without losing changes
$ git restore --staged <file>
Removes the file from the index but keeps your edits in the working tree.
Undo the last commit but keep changes staged
$ git reset --soft HEAD~1
The commit is removed but your changes stay in the index, ready to recommit.
Undo the last commit and unstage changes
$ git reset HEAD~1
The default (--mixed). Changes go back to the working tree as
unstaged edits.
Undo the last commit and discard all changes
$ git reset --hard HEAD~1
The commit and all changes are gone. Recover with git reflog if
needed.
Revert a commit without rewriting history
$ git revert <hash>
Creates a new commit that undoes the changes. Safe to use on shared branches.
Recover a lost commit
$ git reflog # find the hash
$ git branch recovered <hash> # or: git reset --hard <hash>
Works as long as the reflog entry has not expired (default: 30 days).