DEV Community

CodeWithCaen
CodeWithCaen

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

1

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)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more