To undo changes in Git, you can use the following commands, depending on what you want to undo:
-
Undo uncommitted changes in the working directory:
1git checkout -- <file> -
Unstage a staged file:
1git reset <file> -
Undo the last commit but keep changes:
1git reset --soft HEAD~1 -
Undo the last commit and discard changes:
1git reset --hard HEAD~1
Choose the command based on your requirement. Always be cautious with --hard
as it irreversibly deletes changes.
Syntax highlighting by Torchlight.dev