Quick Tip: Git Undo Commands
CodeWithCaen

CodeWithCaen @codewithcaen

About: Background in freelance frontend, currently a fullstack Laravel/PHP engineer with a strong focus on backend and cybersecurity. Created Laravel-based static site generator HydePHP.

Location:
Sweden
Joined:
Apr 22, 2022

Quick Tip: Git Undo Commands

Publish Date: Oct 17 '24
1 0

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.

Comments 0 total

    Add comment