Everyone makes mistakes in Git. The trick is knowing which undo command fits the situation. This lesson gives you a decision table and explains git restore, commit –amend, the three modes of git reset, git revert, git clean and the reflog safety net.
๐ What You’ll Learn: a quick decision table, discarding and unstaging changes with git restore, fixing the last commit with –amend, git reset –soft, –mixed and –hard, git revert for pushed commits, reset vs revert, git clean, and recovering with git reflog.
โฉ๏ธ Mistakes Happen: Git Can Undo Almost Anything
Edited the wrong file? Committed too early? Pushed a bug? Git has a precise command for each situation. The key question is always: where is the change now? In the working directory, the staging area, a local commit, or a commit already pushed to GitHub?
Review the three areas in Working Directory, Staging Area and Repository if needed.
๐งญ Quick Decision Table
| Situation | Command | Safe? |
|---|---|---|
| Discard unsaved edits in a file | git restore file | โ ๏ธ Edits are lost |
| Unstage a file (keep the edits) | git restore --staged file | โ |
| Fix the last commit’s message | git commit --amend -m "new message" | โ if not pushed |
| Add a forgotten file to the last commit | git add file then git commit --amend --no-edit | โ if not pushed |
| Undo the last commit, keep changes staged | git reset --soft HEAD~1 | โ if not pushed |
| Undo the last commit, keep changes unstaged | git reset HEAD~1 (mixed) | โ if not pushed |
| Throw away the last commit and its changes | git reset --hard HEAD~1 | โ ๏ธ Changes are lost |
| Undo a commit that is already pushed | git revert <hash> | โ Always safe |
| Delete untracked files | git clean -n (preview), then git clean -fd | โ ๏ธ Files are lost |
๐ 1. Discard Changes in the Working Directory: git restore
git restore test-plan.txt # discard edits in one file
git restore . # discard all unstaged edits
This replaces the file with the version from the staging area (or the last commit). Older tutorials use git checkout -- file, which does the same thing.
โ ๏ธ Common mistake: Discarded edits were never committed, so Git cannot bring them back. Run git diff file first to be sure.
๐ 2. Unstage Changes: git restore –staged
git add .
git restore --staged secrets.txt # remove it from the next commit, keep the file as it is
The older equivalent is git reset HEAD secrets.txt.
โ๏ธ 3. Fix the Last Commit: git commit –amend
Typo in the message:
git commit --amend -m "Add login tests for locked-out users"
Forgot a file:
git add tests/logout.spec.ts
git commit --amend --no-edit
--amend replaces the last commit with a new one (new hash). Use it only for commits you have not pushed yet.
โช 4. Undo Local Commits: git reset
git reset moves your branch back to an earlier commit. The three modes decide what happens to the changes from the removed commits:
| Mode | Branch moves back | Staging area | Working directory | Use it to |
|---|---|---|---|---|
--soft | โ | Changes kept staged | Unchanged | Redo the commit, e.g. combine several commits |
--mixed (default) | โ | Changes unstaged | Unchanged | Re-select what to commit |
--hard | โ | Reset | Changes deleted | Throw work away completely |
git log --oneline
# 9c7e1aa (HEAD -> main) WIP debug logs
# 8b1d4e2 Add learning goals and initial test plan
# 3f2a91c Add README with project title
git reset --soft HEAD~1 # undo "WIP debug logs", changes stay staged
git reset HEAD~1 # same, but changes become unstaged
git reset --hard HEAD~1 # undo it and delete its changes
git reset --hard 3f2a91c # go back to a specific commit
โ ๏ธ Common mistake: Never use git reset on commits that are already pushed to a shared branch. It rewrites history, and teammates who pulled those commits will end up with conflicts. Use git revert instead.
๐ 5. Undo Pushed Commits Safely: git revert
git revert does not delete anything. It creates a new commit that applies the opposite changes, so history stays intact and everyone can simply pull it:
git revert 9c7e1aa
# Git opens the editor with the message "Revert "WIP debug logs"". Save and close.
git push
a1b2c3d (HEAD -> main) Revert "WIP debug logs"
9c7e1aa WIP debug logs
8b1d4e2 Add learning goals and initial test plan
git reset | git revert | |
|---|---|---|
| How it undoes | Moves the branch back; removes commits from history | Adds a new commit with the inverse changes |
| History | Rewritten | Preserved |
| Safe for pushed or shared branches | โ No | โ Yes |
| Typical use | Clean up local work before pushing | Undo a change that is already on GitHub |
Useful options: git revert --no-edit <hash> (keep the default message) and git revert HEAD~3..HEAD (revert a range of commits).
๐งน 6. Remove Untracked Files: git clean
git clean -n # dry run: list what would be deleted
git clean -f # delete untracked files
git clean -fd # also delete untracked folders
Files listed in .gitignore are kept unless you add -x.
๐ The Safety Net: git reflog
Ran git reset --hard by mistake? Commits are not gone immediately. git reflog lists every position HEAD has been in:
git reflog
# 8b1d4e2 HEAD@{0}: reset: moving to HEAD~1
# 9c7e1aa HEAD@{1}: commit: WIP debug logs
git reset --hard 9c7e1aa # bring the "lost" commit back
Learn more in git reflog: recover lost commits.
๐ Assignment
In your git-practice repository:
- Edit a file and discard the edit with
git restore. - Stage two files and unstage one with
git restore --staged. - Make a commit with a typo in the message and fix it with
--amend. - Make a commit, forget a file, and add it with
--amend --no-edit. - Make three test commits and try
reset --soft,reset --mixedandreset --hardon them, noting what happens to the changes each time. - Make a commit, then undo it with
git revertand explain how the history differs from a reset. - Use
git reflogto recover a commit you removed withreset --hard.
โ FAQ
๐ค What is the difference between git reset and git revert?
git reset moves the branch back and removes commits from history, which is fine for local work. git revert adds a new commit that undoes a previous one, which is safe for commits already pushed.
๐ค How do I undo the last commit but keep my changes?
git reset --soft HEAD~1 keeps them staged; git reset HEAD~1 keeps them unstaged in your files.
๐ค How do I undo git add?
git restore --staged <file> (or git reset HEAD <file> in older Git versions).
๐ค What does git reset –hard do?
It moves the branch to the given commit and deletes all uncommitted changes and the removed commits’ changes from your files. Use it with care.
๐ค How do I undo a commit that I already pushed?
Use git revert <commit-hash> and push the new revert commit. Avoid resetting and force-pushing shared branches.
๐ฏ Next Steps
- ๐ Remote Repositories: origin, upstream and git remote
- ๐ค git clone, push and pull Step by Step
- ๐ Git and GitHub Tutorial: full course
๐ฌ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in
4 thoughts on “Undo Changes in Git: restore, reset, revert and amend Explained”