Git is powerful because it lets us experiment—try things, make mistakes, and undo them safely. But many beginners get confused when they see multiple ways to undo changes: restore, reset, and revert.
In this post, we’ll learn what each command does, when to use it, and simple examples to help us pick the right one every time.
1. Git restore — Undo Working Directory Changes
Think of git restore as the command that cleans up our pending changes—things we haven’t committed yet.
Use it when:
- We modified a file but don’t want the change anymore.
- We staged a file and want to unstage it.
Examples
Undo changes in a file (go back to last commit):
git restore file.txt
Unstage a file (move it back from staged to unstaged):
git restore --staged file.txt
Works on:
- Our working directory
- Staging area
Does NOT:
- Change commit history
2. Git reset — Move HEAD to a previous commit (Dangerous if using –hard)
git reset changes the commit history by moving the branch pointer (HEAD) to an earlier commit.
This affects our local history.
Use it when:
- We want to undo commits that only exist in our local machine.
- We want to rewrite or fix local history before pushing.
Three types of reset
1. soft reset (keeps changes staged)
git reset --soft <commit-id>
Good when we want to rewrite last commit but keep changes ready to recommit.
2. mixed reset (default — keeps changes but unstaged)
git reset <commit-id>
3. hard reset (WARNING: discards changes)
git reset --hard <commit-id>
This completely wipes our working directory to match the chosen commit.
Does:
- Moves branch pointer
- Can delete commits (locally)
Does NOT:
- Create a new commit
- Safe for local-only changes before pushing
3. Git revert — Safely Undo a Commit by Creating a New Commit
If the commit is already pushed to a shared repo, git revert is our safest option.
Instead of deleting old commits, it creates a new commit that reverses the effect of a previous commit.
Use it when:
- We want to undo changes in shared/public history.
- We must not break other people’s work.
Examples
Revert the last commit
git revert HEAD
Revert a specific commit
git revert <commit-id>
Does:
- Create a new commit that undoes earlier work
- Safe for public branches (like
main)
Does NOT:
- Change old commit history
Summary Table
| Task | Use This | Why |
|---|---|---|
| Undo changes in unstaged/staged files | git restore | Safe, works on working directory |
| Undo commits before pushing | git reset | Rewrites history locally |
| Undo commits already pushed | git revert | Safe for shared repos |
Which One Should You Use?
- If we haven’t committed yet → use restore
- If we have committed but not pushed → use reset
- If we have committed and pushed → use revert
Closing Thoughts
Undoing changes in Git doesn’t have to be scary.
Once we understand what each command controls—working directory, staging area, or commit history—we can confidently clean up mistakes without losing our work.