Undoing uncommitted changes

Beginner · 13 min

Use git restore to discard edits and unstage files, and git clean to remove untracked files, plus which of these can never be taken back.

Before thisThe three areas of GitReading history with log and diff

Nick Janetakis · 13 min

Undoing uncommitted changes

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Nick JanetakisReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • git restore <file> throws away your unstaged edits to a file and puts back the staged (or last committed) version.
  • git restore --staged <file> unstages a file and keeps your edits in the working directory.
  • git clean removes untracked files, and git clean -n shows what it would remove first.
  • Discarding unstaged edits and cleaning untracked files cannot be undone, because Git never stored that content.
  • git restore --source=<commit> <file> brings back a file as it was in any commit.

Three different “undos”

“Undo” in Git means different things depending on where the change is. Using the three areas from earlier:

Where the change is You want to… Command
Working directory (edited, not staged) throw the edit away git restore <file>
Staging area unstage, keep the edit git restore --staged <file>
Working directory, but a new file delete it git clean

Once a change is in a commit, the tools differ, and that’s the next lesson and the whole of a later group. This lesson covers only work you haven’t committed.

Before the commands, a warning. Of these, discarding unstaged edits and cleaning untracked files are permanent. Git had never stored that content, so there is nothing to bring back. Run git diff before you discard something, and git clean -n before you clean.

Discarding an edit

I appended a scratch line to notes.py and changed README.md and made two new files. git status lists all of it and gives the commands to use:

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md
	modified:   notes.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	ideas.txt
	tmp/

Read the hints in brackets, since git status writes the right command for your situation. To drop the scratch line in notes.py:

$ git restore notes.py
$ git status -s
 M README.md
?? ideas.txt
?? tmp/

notes.py is back to its last committed version and is no longer listed. git restore takes its source from the staging area, which for an unstaged file is the same as the last commit. The old way to say the same thing was git checkout -- notes.py. I ran both, and both worked. restore came in with Git 2.23 to give “put files back” its own command, separate from checkout’s other jobs.

Run git restore with no path and it refuses: “fatal: you must specify path(s) to restore”. That’s deliberate. git restore . restores everything, so you have to say so.

Unstaging

$ git add README.md
$ git status -s
M  README.md
$ git restore --staged README.md
$ git status -s
 M README.md

The M moved from the left column (staged) to the right (unstaged). The edit is still in the file; only the staging was undone. The old spelling was git reset HEAD README.md, and it prints “Unstaged changes after reset:” when it works. To undo both in one go, pass both flags:

$ git restore --staged --worktree README.md
$ git status -s
?? ideas.txt
?? tmp/

Cleaning untracked files

git restore can’t touch an untracked file, since Git doesn’t know about it. I tried git restore u.txt on one and got “error: pathspec ‘u.txt’ did not match any file(s) known to git”. For those use git clean, and always dry-run first:

$ git clean -n
Would remove ideas.txt
$ git clean -n -d
Would remove ideas.txt
Would remove tmp/

By default it ignores untracked directories, so -d adds them. When the list looks right, -f does it. Git refuses to clean without -f or -n by default, which is a safety catch:

$ git clean -f
Removing ideas.txt
$ git clean -fd
Removing tmp/

Files matched by .gitignore are skipped unless you add -x. I did not use -x, because in tinynotes it would have deleted notes.txt, the real data.

Bringing back an older version of a file

--source takes the file from any commit. Here is notes.py as it was four commits ago:

$ git restore --source=HEAD~4 notes.py
$ git diff --stat
 notes.py | 6 ------
 1 file changed, 6 deletions(-)

That replaced the working copy and left the staging area alone, so the difference shows up as an unstaged change you can inspect or discard with a plain git restore notes.py. Add --staged --worktree and it also stages the old version, so I saw M notes.py in git status -s.

Can a discard be undone?

Mostly not, but there is a nuance. I tested it. I appended # precious, staged it, appended # more, then discarded both with git restore --staged --worktree. Then:

$ git fsck --lost-found
dangling commit 887111e76aa8e1454da8943a3e1ddb7d1a00393e
dangling blob ce013625030ba8dba906f756967f9e9ca394464a
dangling blob 5ee4001f5066fd62bb16ac7a4d7a2f5295b99407
...

(I trimmed the list. The dangling commit is from an earlier experiment, and ce01362... is the hello blob from the storage lesson.) git cat-file -p 5ee4001... printed notes.py ending in # precious. Staging had stored that content as an object, and discarding left the object behind. The # more edit was never staged, so Git never stored it, and it was gone. The lesson: git add is a cheap save point. If you’re about to do something risky to edits you care about, stage them first. Such dangling objects are eventually deleted by Git’s cleanup, so don’t count on them.

git restore -p

Like git add -p, git restore -p goes hunk by hunk, asking “Discard this hunk from worktree”. I answered y for the README.md hunk and n for the one in notes.py, and only README.md was reverted.

What the video covers

Nick Janetakis’s “Git Restore All Unstaged and Untracked Files Back to their Latest Commit” is about thirteen minutes and was published around 2023. I had no transcript. The title says it covers restoring unstaged and untracked files, which maps to restore and clean here. He is a long-standing English-language creator. Cameron McKenzie’s video on unstaging is linked as a second view.

Where this fits

You can now discard or unstage anything that isn’t committed. For a mistake that is already committed, but only the last commit, there is a lighter tool: amending the last commit.

Check yourself

  1. 01You ran git add on a file by mistake. How do you take it out of the staging area without losing your edits?

    git restore --staged <file>. The file leaves the staging area and your working copy is untouched.

  2. 02What does git restore notes.py do to a file you have edited but not staged?

    It replaces the file with the staged or last committed version and throws your edits away. Git has no record of them, so they cannot be recovered.

  3. 03Why run git clean -n before git clean -f?

    -n is a dry run that lists what would be deleted. git clean removes untracked files for good, so look first.

  4. 04You staged a file, then discarded both the staged and unstaged versions with git restore --staged --worktree. Is anything recoverable?

    Possibly the staged version. Git already stored its content, and git fsck --lost-found can list it as a dangling blob. The unstaged edits made after staging are gone.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark