Restore and reset

Topic 2 of 4 in Undoing changes7 min readBeginnerFree course

Overview

Mistakes happen at every stage: a bad edit you haven’t saved, a file staged by accident, a commit that should never have been made. Git has an undo for each — the trick is knowing which area you’re undoing. This lesson covers the two main tools: git restore for files and git reset for commits.

git restore: undoing changes to files

git restore puts files back to a known version. It never touches history.

Discard edits in the working directory

Terminal
git restore config.json          # back to the staged version (or the last commit if nothing is staged)
git restore .                    # every file in the current folder

Your uncommitted edits to those files are gone — Git never had a copy of them. Look with git diff config.json first.

Unstage a file

Terminal
git restore --staged secrets.txt

The file leaves the staging area, but your edits stay in the working directory. Nothing is lost.

Bring back an older version

Terminal
git restore --source=HEAD~2 menu.txt        # menu.txt as it was two commits ago
git restore --source=v1.0 -- src/           # a whole folder from a tag

The old version lands in your working directory as an ordinary modification. Review it with git diff, then commit it like any change. History isn’t rewritten — you’re making a new commit that happens to contain old content.

git restore doesn’t create or delete untracked files. To remove untracked files and folders, use git clean — carefully: git clean -n lists what would be deleted, then git clean -f (add -d for folders) actually deletes.

git reset: moving a branch back

git reset <commit> moves the current branch to a different commit. Commits after that point are no longer on the branch. What happens to their changes depends on the mode:

git reset HEAD~1: what each mode changes. –soft: the branch moves back, the staging area and working directory are kept, so the changes stay staged. –mixed, the default: the branch moves back, the staging area is reset and the working directory is kept, so the changes become unstaged. –hard: the branch moves back and both the staging area and the working directory are reset, so the changes are thrown away. –hard is the only one that can lose work — uncommitted changes are deleted. Committed work is still recoverable with git reflog for a while.

Terminal
git reset --soft HEAD~1     # undo the commit, keep its changes staged
git reset HEAD~1            # (--mixed) undo the commit, keep its changes unstaged
git reset --hard HEAD~1     # undo the commit and throw its changes away

–soft: redo the last commit(s)

Your branch moves back, but the staging area and files stay exactly as they were. The changes from the “undone” commits are staged and ready to commit again. It’s perfect for squashing a few small local commits into one:

Terminal
git reset --soft HEAD~2
git commit -m "Add dessert section"

–mixed (the default): unpick and re-stage

The branch moves back and the staging area matches the new position, but your files are untouched. The changes appear as unstaged modifications, so you can re-stage them differently — to split one commit into several, for example.

–hard: start over

The branch, the staging area and your working directory all go back. Any uncommitted changes in tracked files are destroyed. It’s the right tool when you really do want to throw everything away:

Terminal
git reset --hard              # discard all uncommitted changes, back to HEAD
git reset --hard origin/main  # make your branch exactly match the remote

Commits you “remove” with reset --hard aren’t destroyed immediately — the reflog can usually get them back (Recovering with reflog). Uncommitted changes can’t be recovered. Run git status and git stash if in doubt.

reset on files

git reset <file> (without a commit) unstages a file — the same as git restore --staged <file>. You’ll see it in older guides. restore is clearer, so this course uses it.

The golden rule

git reset rewrites the history of your branch. That’s fine for commits only you have — local commits you haven’t pushed. If you reset commits that others have already pulled, your branch and theirs disagree, and you’ll need a force-push that can wipe out their work.

For commits that have been shared, use git revert, which undoes a commit by adding a new one. That’s the next lesson.

Quick reference

Situation Command
Throw away my edits to a file git restore <file>
Unstage a file git restore --staged <file>
Get a file as it was in another commit git restore --source=<commit> <file>
Undo the last commit, keep changes staged git reset --soft HEAD~1
Undo the last commit, keep changes unstaged git reset HEAD~1
Undo the last commit and its changes git reset --hard HEAD~1
Discard all uncommitted changes git reset --hard (or git restore . for unstaged only)
Delete untracked files git clean -n, then git clean -fd

Safety first: before any --hard command, run git status. If there’s anything you might want, git stash it or commit it to a temporary branch — commits are much easier to recover than edits.

Exercise

Undo at every level

Work through four undo situations in the repository the script creates. (1) You accidentally emptied config.json — get the committed version back. (2) You staged secrets.txt by mistake — unstage it and delete the file. (3) Your last two commits, “Add cake” and “Add pie”, should have been one — use git reset --soft to combine them into a single commit called “Add dessert section”. (4) menu.txt was better before the “Shout about tea” commit — bring back the version from the “Add tea” commit and save it as a new commit, without rewriting history.

Terminal
mkdir cafe && cd cafe && git init -b main
echo '{"open": "8am"}' > config.json
echo "coffee" > menu.txt
git add . && git commit -qm "Open the cafe"
echo "tea" >> menu.txt && git commit -qam "Add tea"
echo "TEA!!!" >> menu.txt && git commit -qam "Shout about tea"
echo "cake" > desserts.txt && git add desserts.txt && git commit -qm "Add cake"
echo "pie" >> desserts.txt && git commit -qam "Add pie"

: > config.json                         # oops, emptied
echo "password=hunter2" > secrets.txt
git add secrets.txt                     # oops, staged

# 1. Get config.json back

# 2. Unstage and delete secrets.txt

# 3. Combine the last two commits

# 4. Bring back menu.txt from the "Add tea" commit, as a new commit

Summary

  • git restore <file> discards working-directory edits; --staged unstages; --source=<commit> brings back an old version.
  • git reset <commit> moves the current branch to another commit — it rewrites history on your branch.
  • --soft keeps changes staged, --mixed (the default) keeps them unstaged, --hard discards them.
  • git reset --hard is the one undo that can permanently lose uncommitted work — check git status first.
  • Only reset commits that haven’t been pushed; for shared commits, use git revert instead.