Git Undo Changes: Restore, Reset, Revert & Discard Changes
95 people viewed this
Git Undo Changes — Restore, Reset, Revert and Discard Changes
Making changes is part of every developer's daily workflow. But sometimes you modify the wrong file, stage something accidentally, create the wrong commit, or push a change that needs to be undone.
Git provides several commands for handling these situations, but they do different things.
The most important commands are:
git restore
git reset
git revert
git clean
Choosing the wrong command can cause you to lose local work or rewrite shared history.
In this article, we will understand how to undo changes in Git using practical PHP and Laravel examples.
1. First Understand the Three Git States
Before learning how to undo changes, you should understand where your changes currently exist.
Working Directory
↓
Staging Area
↓
Repository / Commit
For example:
Laravel file modified
↓
git add
↓
File staged
↓
git commit
↓
Commit created
The command you use to undo the change depends on which state the change is currently in.
2. The Four Common Undo Situations
| Situation | Common Command |
|---|---|
| Discard changes in a file | git restore file |
| Unstage a file | git restore --staged file |
| Undo a local commit | git reset |
| Undo a shared/pushed commit | git revert |
| Remove untracked files | git clean |
3. Before Undoing Anything — Check Your Status
Before running any destructive Git command, first inspect the repository.
git status
For example:
On branch feature/payment
Changes not staged for commit:
modified: app/Services/PaymentService.php
Changes to be committed:
modified: app/Models/Payment.php
Untracked files:
app/Services/TestPaymentService.php
Now you know exactly what state each file is in.
4. Use git diff Before Discarding Changes
If you are thinking about removing a change, first inspect it.
git diff
This shows unstaged changes.
For staged changes:
git diff --staged
This simple habit can prevent accidental loss of useful work.
5. Undo Changes in a File With git restore
Suppose you modified a Laravel controller:
app/Http/Controllers/PaymentController.php
But you don't want those local changes anymore.
Run:
git restore app/Http/Controllers/PaymentController.php
This restores the file to the version from the index, which normally corresponds to the currently checked-out commit when the file has not already been staged differently.
Important: this can discard your local modifications to that file.
6. Restore All Unstaged Changes
If you want to discard all unstaged changes in tracked files:
git restore .
This is powerful and potentially destructive.
Before running it, use:
git status
git diff
Make sure you really want to discard those changes.
7. Undo git add — Unstage a File
Suppose you modified:
app/Models/Order.php
Then you ran:
git add app/Models/Order.php
But you realize you don't want to commit it yet.
Use:
git restore --staged app/Models/Order.php
The file remains modified, but it is removed from the staging area.
Think of it like this:
Before:
Working Directory → Staging Area
After restore --staged:
Working Directory
↓
Modified but NOT staged
Your file changes are not deleted.
8. Unstage All Files
If you staged multiple files accidentally:
git add .
You can unstage them with:
git restore --staged .
The changes remain in your working directory. Only the staging state is changed.
9. Important Difference — Restore vs Restore --staged
| Command | What happens? |
|---|---|
git restore file.php |
Discards unstaged changes in the file |
git restore --staged file.php |
Unstages the file but keeps its changes |
Easy way to remember:
restore
↓
Discard working-directory changes
restore --staged
↓
Remove file from staging
10. Restore a File From a Specific Commit
You can also restore a file from another commit.
git restore --source=<commit> -- path/to/file.php
For example:
git restore --source=a82d921 -- app/Services/PaymentService.php
This takes the file content from the specified commit and places it into your working tree.
You can then inspect the change before committing it.
11. Undo the Last Commit With git reset
Suppose you created:
git commit -m "feat(payment): add payment service"
But you realize the commit should not have been created yet.
If the commit is only local, git reset can move your branch back.
There are three important modes:
git reset --soft
git reset --mixed
git reset --hard
12. git reset --soft
--soft moves HEAD to another commit but keeps the changes staged.
git reset --soft HEAD~1
Suppose you had:
A → B → C
After:
git reset --soft HEAD~1
the branch moves to B, but the changes introduced by C remain staged.
A → B
Changes from C → Staged
This is useful when you want to recreate the commit with a better message or adjust its contents.
13. git reset --mixed
--mixed moves HEAD and unstages the changes.
git reset --mixed HEAD~1
This is also the default mode:
git reset HEAD~1
After the reset:
A → B
Changes from C → Working Directory
↓
Unstaged
The changes remain in your files, but they are no longer staged.
14. git reset --hard
--hard moves HEAD and also updates the working tree and index to match the target commit.
git reset --hard HEAD~1
This can discard local changes.
For example:
A → B → C
After:
git reset --hard HEAD~1
you get:
A → B
The changes introduced by C are no longer present in the current working tree.
Use this command carefully.
15. Soft vs Mixed vs Hard
| Mode | HEAD | Staging | Working Files |
|---|---|---|---|
--soft |
Moves | Changes remain staged | Changes remain |
--mixed |
Moves | Changes become unstaged | Changes remain |
--hard |
Moves | Updated to target | Updated to target |
Easy memory trick:
soft → keep staged
mixed → keep changes, unstage them
hard → throw away local changes
16. Undo a Pushed Commit With git revert
Suppose a commit has already been pushed to a shared branch:
git push origin main
If you need to undo that change, do not normally rewrite the shared history just to remove it.
Instead, use:
git revert <commit-hash>
For example:
git revert a82d921
Git creates a new commit that reverses the changes introduced by the selected commit.
The history becomes:
A → B → C → D
↓
Revert C
The original commit remains in history, and a new commit records the reversal.
17. Reset vs Revert
This is one of the most common Git interview questions.
| Reset | Revert |
|---|---|
| Moves branch history | Creates a new commit |
| Commonly used for local commits | Commonly used for shared/pushed commits |
| Can rewrite history | Preserves existing history |
Can be destructive with --hard |
Normally safer for shared branches |
Remember:
reset → move the branch backward
revert → create a new commit that undoes a change
18. Undo the Last Commit But Keep the Changes
Suppose you accidentally committed too early:
git commit -m "feat(order): add order API"
You want to make some additional changes before creating the final commit.
Use:
git reset --soft HEAD~1
The commit disappears from the current branch history, but the changes remain staged.
You can then make adjustments and create a new commit.
19. Undo the Last Commit and Unstage the Changes
Use:
git reset HEAD~1
This is equivalent to:
git reset --mixed HEAD~1
The commit is removed from the current branch position, while the changes remain in your working directory as unstaged modifications.
20. Remove the Last Commit and Its Local Changes
If you are absolutely certain that you want to discard the commit and its associated local changes:
git reset --hard HEAD~1
This is one of the most dangerous common Git undo commands.
Before using it, consider checking:
git status
git log --oneline -5
git reflog
21. What Is git clean?
git clean removes untracked files from your working directory.
For example, you might have:
app/Services/TestService.php
which Git reports as untracked.
First preview what would be removed:
git clean -n
If you are certain, you can remove untracked files:
git clean -f
Always preview with -n before using -f.
22. Remove Untracked Directories
To remove untracked directories too:
git clean -fd
Preview first:
git clean -nd
This is another potentially destructive command.
23. What Does git clean NOT Do?
Git clean deals with untracked files. It does not undo modifications to tracked files.
For example:
Tracked modified file
↓
git restore
Untracked file
↓
git clean
24. Laravel Example — Accidentally Modified a Controller
Suppose you are working on:
app/Http/Controllers/OrderController.php
You make several changes but then decide you don't want them.
Check:
git status
git diff
If you want to discard them:
git restore app/Http/Controllers/OrderController.php
The controller returns to its previous tracked version.
25. Laravel Example — Accidentally Staged a File
You changed:
app/Services/PaymentService.php
Then:
git add app/Services/PaymentService.php
But you don't want to commit it yet.
Run:
git restore --staged app/Services/PaymentService.php
The code changes remain, but the file is unstaged.
26. Laravel Example — Wrong Local Commit
You created:
git commit -m "fix: payment issue"
But you realize the commit includes changes that should be separated.
If it has not been shared and you want to keep the changes:
git reset --soft HEAD~1
Now you can reorganize the files and create better commits.
27. Laravel Example — Bad Commit Already Pushed
Suppose this commit was already pushed to the team's shared branch:
a82d921 feat(payment): update payment calculation
Instead of moving shared history backward, create a reversal:
git revert a82d921
Then push the new revert commit:
git push origin main
The repository history remains traceable.
28. Undo Changes After git add
Suppose you have:
git add .
and now want to review everything again.
First:
git status
Then unstage:
git restore --staged .
Your modifications remain in the working directory.
Now you can selectively stage:
git add app/Services/PaymentService.php
git add tests/Feature/PaymentTest.php
29. Undo Only Part of a File
Sometimes a single file contains two different changes. You want to keep one and discard the other.
Instead of restoring the entire file, use interactive patching:
git add -p
This lets you select individual sections to stage.
For more advanced workflows, Git also provides interactive restore options.
git restore -p
This allows you to interactively choose which working-tree changes to discard.
Because these commands operate at the individual change/hunk level, review each prompt carefully.
30. Undo a Commit Using Its Hash
Suppose your history is:
a82d921 Add payment API
b91c441 Fix authentication
c72d123 Update documentation
You want to undo the payment API commit.
Use:
git revert a82d921
Git creates a new commit that reverses the changes from that commit.
This is especially useful when the original commit is already part of shared history.
31. Undo Multiple Commits
You can revert multiple commits, but the exact range and ordering should be considered carefully.
For example, Git supports specifying a range:
git revert <oldest-commit>^..<newest-commit>
For a shared branch, inspect the proposed changes and resolve any conflicts that occur during the revert sequence.
32. What If You Undo Something by Mistake?
This is where the previous article becomes useful:
Git Reflog
git reflog
Reflog can help you locate previous positions of HEAD and recover commits after operations such as reset or rebase.
For example:
a82d921 HEAD@{0}: reset: moving to HEAD~2
b91c441 HEAD@{1}: commit: Add payment service
c72d123 HEAD@{2}: commit: Add payment tests
You can inspect a previous commit:
git show b91c441
Then create a recovery branch:
git switch -c recovery/payment b91c441
This is much safer than guessing your way through recovery.
33. Decision Table — Which Git Undo Command Should You Use?
| Your Situation | Command |
|---|---|
| Discard modifications in a tracked file | git restore file |
| Unstage a file | git restore --staged file |
| Unstage everything | git restore --staged . |
| Undo local commit and keep changes staged | git reset --soft HEAD~1 |
| Undo local commit and keep changes unstaged | git reset HEAD~1 |
| Undo local commit and discard changes | git reset --hard HEAD~1 |
| Undo a shared/pushed commit | git revert <commit> |
| Remove untracked files | git clean -f |
| Recover after a bad reset | git reflog |
34. The Most Important Git Undo Rule
Before choosing a command, ask:
Where is my change?
Is it:
Working Directory?
↓
Staging Area?
↓
Local Commit?
↓
Already Pushed?
Then choose the appropriate command.
35. Common Git Undo Mistakes
❌ Mistake 1 — Using git reset --hard without checking
git reset --hard HEAD~3
This can discard local work.
❌ Mistake 2 — Using reset on a shared branch
Rewriting shared history can create problems for other developers.
❌ Mistake 3 — Confusing restore with restore --staged
git restore file.php
can discard local modifications.
Whereas:
git restore --staged file.php
only removes the file from the staging area.
❌ Mistake 4 — Using git clean without previewing
First:
git clean -n
Then, if appropriate:
git clean -f
❌ Mistake 5 — Reverting without checking the commit
Before reverting an important commit, inspect it:
git show <commit-hash>
36. Best Practices for Undoing Git Changes
- Always run
git statusfirst. - Use
git diffbefore discarding changes. - Prefer
git restorefor straightforward working-tree changes. - Use
git resetcarefully for local history. - Use
git revertwhen undoing shared history. - Preview
git cleanwith-n. - Use
git reflogwhen recovering from a bad reset or rebase. - Create a recovery branch before complicated recovery operations.
- Never assume an undo operation is reversible without checking.
37. Git Undo Workflow for Laravel Developers
A practical Laravel workflow can look like this:
# 1. Check status
git status
# 2. Inspect changes
git diff
# 3. If you want to discard a file
git restore app/Services/PaymentService.php
# 4. If you accidentally staged a file
git restore --staged app/Services/PaymentService.php
# 5. If a local commit was premature
git reset --soft HEAD~1
# 6. If a shared commit needs to be undone
git revert <commit-hash>
# 7. If you accidentally lost a commit
git reflog
# 8. Verify Laravel code
php artisan test
38. Git Undo Cheat Sheet
# Discard file changes
git restore file.php
# Discard all tracked file changes
git restore .
# Unstage a file
git restore --staged file.php
# Unstage everything
git restore --staged .
# Undo last local commit, keep staged
git reset --soft HEAD~1
# Undo last local commit, keep changes unstaged
git reset HEAD~1
# Undo last local commit and discard local changes
git reset --hard HEAD~1
# Undo a pushed/shared commit
git revert <commit-hash>
# Preview untracked files that would be removed
git clean -n
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Find previous HEAD positions
git reflog
39. Git Undo Interview Questions
Q1. How do you discard changes in a file?
git restore file.php
Q2. How do you unstage a file?
git restore --staged file.php
Q3. What is the difference between git restore and git reset?
git restore is primarily used to restore file contents or change staging state, while git reset can move HEAD/branch history and has different effects depending on the selected mode.
Q4. What is the difference between git reset and git revert?
git reset moves the branch to another commit and can rewrite local history. git revert creates a new commit that reverses an earlier commit.
Q5. When should you use git revert?
When you need to undo a commit that is already part of shared history, especially on a shared branch.
Q6. What does git reset --soft HEAD~1 do?
It moves the branch back one commit while keeping the changes from that commit staged.
Q7. What does git reset --hard HEAD~1 do?
It moves the branch back one commit and updates the index and working tree to match that commit, potentially discarding local changes.
Q8. What does git clean do?
It removes untracked files and, with the appropriate options, untracked directories.
Q9. How can you recover after an accidental reset?
git reflog
Then inspect and recover the appropriate previous commit.
Q10. Which command should you use to see changes before discarding them?
git diff
40. Easy Way to Remember Git Undo Commands
Modified file?
↓
git restore
Staged file?
↓
git restore --staged
Local commit?
↓
git reset
Shared/pushed commit?
↓
git revert
Untracked file?
↓
git clean
Lost commit?
↓
git reflog
This simple decision tree will solve most day-to-day Git undo situations.
Conclusion
Git gives developers several ways to undo changes, but each command has a different purpose.
The key is to understand where your changes currently exist before choosing a command.
The most important commands are:
git restore
git reset
git revert
git clean
git reflog
For PHP and Laravel developers, learning these commands is especially important because Git is used throughout feature development, debugging, code reviews, Bitbucket workflows, and production maintenance.
The most useful rule to remember is:
Check first, understand what you want to undo, then choose the least destructive command that solves the problem.
What's Next?
Now that we understand how to undo changes safely, the next important Git topic is working with remote repositories.
Next Article: Git Remote, Fetch, Pull and Push Explained
We will cover:
- What is a Git remote?
- What is
origin? git fetchgit pullgit push- Remote-tracking branches
- Upstream branches
- Fetch vs Pull
- Push vs Pull
- Practical Bitbucket workflow
- Common Git interview questions
Was this useful?
No ratings yet — yours can be the first.
Discussion
Notes from readers. Comments appear after a short review.
No comments yet. Start the thread below.