To undo changes in Git, you can use the following commands, depending on what you want to undo:
Method 1: Undo uncommitted changes in the working directory:
git checkout -- <file>
Method 2: Unstage a staged file:
git reset <file>
Method 3: Undo the last commit but keep changes:
git reset --soft HEAD~1
Method 4: Undo the last commit and discard changes:
git reset --hard HEAD~1
Bonus: Reset to the remote tracking branch
git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)
Choose the command based on your requirement. Always be cautious with --hard
as it irreversibly deletes changes.
Top comments (0)