Undo Changes in Git: restore, reset, revert and amend Explained

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.

Advertisement

๐Ÿ“– 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

SituationCommandSafe?
Discard unsaved edits in a filegit restore fileโš ๏ธ Edits are lost
Unstage a file (keep the edits)git restore --staged fileโœ…
Fix the last commit’s messagegit commit --amend -m "new message"โœ… if not pushed
Add a forgotten file to the last commitgit add file then git commit --amend --no-editโœ… if not pushed
Undo the last commit, keep changes stagedgit reset --soft HEAD~1โœ… if not pushed
Undo the last commit, keep changes unstagedgit reset HEAD~1 (mixed)โœ… if not pushed
Throw away the last commit and its changesgit reset --hard HEAD~1โš ๏ธ Changes are lost
Undo a commit that is already pushedgit revert <hash>โœ… Always safe
Delete untracked filesgit 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:

ModeBranch moves backStaging areaWorking directoryUse it to
--softโœ…Changes kept stagedUnchangedRedo the commit, e.g. combine several commits
--mixed (default)โœ…Changes unstagedUnchangedRe-select what to commit
--hardโœ…ResetChanges deletedThrow 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 resetgit revert
How it undoesMoves the branch back; removes commits from historyAdds a new commit with the inverse changes
HistoryRewrittenPreserved
Safe for pushed or shared branchesโŒ Noโœ… Yes
Typical useClean up local work before pushingUndo 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:

  1. Edit a file and discard the edit with git restore.
  2. Stage two files and unstage one with git restore --staged.
  3. Make a commit with a typo in the message and fix it with --amend.
  4. Make a commit, forget a file, and add it with --amend --no-edit.
  5. Make three test commits and try reset --soft, reset --mixed and reset --hard on them, noting what happens to the changes each time.
  6. Make a commit, then undo it with git revert and explain how the history differs from a reset.
  7. Use git reflog to recover a commit you removed with reset --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

๐ŸŽฌ 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”

Leave a Comment