Sub topic 8
Unstaging files with git reset <path>
This chapter assumes you can run git add, git commit, git status, and git log. It also assumes you know what the index (staging area) and the working directory are. If those terms are fuzzy, review the chapter on the three areas first.
What a path reset does
Earlier chapters used git reset with a commit, such as git reset --soft HEAD~1. In that form, the command moves the current branch. Adding a path changes what the command does.
When you give git reset one or more paths, Git does not move the branch or HEAD. It copies the entry for each path from a commit into the index. The default commit is HEAD. The working directory is not touched.
The result is that a staged change for that path disappears from the index, while the edited file stays on disk. This is what "unstaging" means.
| Form | Moves the branch? | Changes the index? | Changes the working directory? |
|---|---|---|---|
| `git reset --soft HEAD~1` | Yes | No | No |
| `git reset HEAD~1` | Yes | Yes | No |
| `git reset --hard HEAD~1` | Yes | Yes | Yes |
| `git reset -- file.txt` | No | Only for that path | No |
Unstaging a file
Start from the repository built in the first chapter. It has three commits, and file.txt contains the lines one, two, and three. Edit the file and stage the change:
echo four >> file.txt
git add file.txt
git statusgit status lists file.txt under "Changes to be committed". Now unstage it:
git add file.txt
git reset -- file.txt
git statusAfter the reset, git status lists file.txt under "Changes not staged for commit". The four line is still in the file. Only the index entry changed: it went back to matching the version in HEAD.
The -- separator marks the end of options and the start of paths. It is optional when the path cannot be mistaken for a commit name. It matters when a file and a branch share a name, or when a filename starts with a dash. Using it by habit avoids ambiguity.
Why this is safe
A path reset is one of the few reset forms that cannot lose your edits. The working directory is never written. If you staged a change and then unstage it, the content is still in the file.
One related edge case needs care. Suppose you staged a version of the file, then edited the file again before resetting. The index holds the first version and the working directory holds the second.
echo four >> file.txt
git add file.txt
echo five >> file.txt
git reset -- file.txtAfter the reset, the index matches HEAD. The working file contains both four and five. Git discarded the staged snapshot, but that snapshot was a subset of what is on disk, so nothing is lost here. If you had staged a version and then replaced the file's content with something unrelated, the staged snapshot would be gone from the index. Its blob would still exist in the object database for a while, but you would have to dig for it. When in doubt, commit or stash first.
Resetting to a commit other than HEAD
You can name a commit before the path. Git then copies the path's entry from that commit instead of from HEAD:
git reset HEAD~2 -- file.txt
git statusHere the index entry for file.txt becomes the version from two commits back. Compared with HEAD, that makes the index look like a change that reverts the last two commits' edits to the file. The working directory still holds the current content, so git status shows both a staged change (HEAD to index) and an unstaged change (index to working directory).
This form is useful when you want to stage an old version of one file without touching anything else. To also put that old content into the working directory, use git restore --source=HEAD~2 --staged --worktree -- file.txt, which the comparison chapter covers.
The following diagram shows which area a path reset reads from and which it writes to.
Several paths, directories, and everything
You can pass more than one path:
git reset -- file.txt notes.mdYou can also pass a directory. Git unstages every tracked or newly added file below it:
git reset -- src/With no path and no commit, git reset resets the entire index to HEAD. That unstages everything:
git resetThis is the --mixed mode with the target HEAD. The branch does not move because it is already at HEAD, so the only effect is on the index. It is the quickest way to back out of a large git add . that picked up files you did not want.
Newly added files
If you stage a brand new file, HEAD has no entry for it. Resetting its path removes the entry from the index, and the file becomes untracked again:
echo draft > notes.md
git add notes.md
git status
git reset -- notes.md
git statusAfter the reset, notes.md appears under "Untracked files". The file on disk is unchanged.
Interactive unstaging
To unstage part of a file, use patch mode:
git reset -p -- file.txtGit shows each staged hunk and asks whether to unstage it. Answer y to unstage, n to keep it staged, s to split a large hunk into smaller ones, and q to quit. The working directory is again left alone.
Common errors
- Using
--softor--hardwith a path. Git refuses with an error such as "Cannot do hard reset with paths". Path resets only update the index, so only--mixed(the default) makes sense. Newer Git versions also accept--mixedexplicitly, but the usual form omits it. - Expecting
git reset -- fileto discard edits. It does not. If you want to throw away working directory changes for a file, that is a job forgit restore -- file, covered later. - Forgetting the
--and hitting an ambiguity error. Iffile.txtis also the name of a branch or tag, Git reports that the argument is ambiguous. Adding--resolves it. - Typing a path that Git does not know. Git reports "ambiguous argument" or "unknown revision or path not in the working tree". Check the spelling and your current directory.
- Unstaging a deletion. If you ran
git rm file.txtand thengit reset -- file.txt, the file is restored in the index. On disk it stays deleted, sogit statusnow shows it as "deleted" under unstaged changes. Usegit restore -- file.txtto bring it back.
Modern equivalent
Since Git 2.23, git restore --staged does the same job with a name that says what it does:
git restore --staged -- file.txtBoth commands update the index entry from HEAD and leave the working directory alone. Git's own git status output suggests git restore --staged <file> for unstaging. git reset -- <path> remains widely used, works on older Git versions, and is what many scripts and tutorials show, so you should recognize both.
A short worked session
This session shows an accidental git add . and the cleanup.
echo debug > debug.log
echo five >> file.txt
git add .
git status
git reset -- debug.log
git statusThe first git status shows both file.txt and debug.log staged. After git reset -- debug.log, only file.txt is staged, and debug.log is untracked again. You can now commit file.txt alone. Adding debug.log to .gitignore stops it from being picked up next time.
Exercise
You staged config.yml with git add config.yml, then realized it contains a password. You want it out of the next commit, but you must keep your edits in the file. Which command do you run, and what happens to the working directory?
Show answer
Run git reset -- config.yml (or git restore --staged -- config.yml). Git copies the HEAD entry for config.yml into the index, so the file is no longer staged. The working directory is not touched, so the edits, including the password, remain in the file. If the file is new and not in HEAD, it becomes untracked.
Exercise
Why does git reset --hard -- file.txt fail, and what would you use to discard unstaged edits to file.txt instead?
Show answer
A path reset only updates the index, so --hard (and --soft) cannot be combined with a path. Git reports an error such as "Cannot do hard reset with paths". To discard unstaged working directory edits for that file, use git restore -- file.txt. This overwrites the file with the index version and cannot be undone for uncommitted content.