DEV Community

CodeWithCaen
CodeWithCaen

Posted on • Edited on • Originally published at tips.desilva.se

Quick Tip: Git Undo Commands

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>
Enter fullscreen mode Exit fullscreen mode

Method 2: Unstage a staged file:

git reset <file>
Enter fullscreen mode Exit fullscreen mode

Method 3: Undo the last commit but keep changes:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Method 4: Undo the last commit and discard changes:

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Bonus: Reset to the remote tracking branch

git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)
Enter fullscreen mode Exit fullscreen mode

Choose the command based on your requirement. Always be cautious with --hard as it irreversibly deletes changes.

Top comments (0)