Sub topic 8
Unstaging files with git reset \<path\>
This chapter assumes you know git add, git commit, git status, and the idea of the index (the staging area) from earlier chapters. You should also know that git reset <commit> moves the current branch. Here you will see a second form of the command, which takes a file path and does not move any branch.
What changes when you give a path
Without a path, git reset moves the branch that HEAD points to. With a path, it does not move anything. Git copies the entry for that path from a commit into the index, and nothing else changes.
The commit defaults to HEAD. So git reset -- file.txt means "make the index entry for file.txt match what is in HEAD." The working directory is never touched. That is why this form is safe: your edits stay on disk, and only the staging decision is undone.
Because the working directory is never touched, path reset has no --soft, --mixed, or --hard choice. Git refuses to combine --soft or --hard with a path. The only mode that applies is the default one, and it behaves like --mixed for that path.
Worked example
Start from a repository with commits, such as the one built in an earlier chapter. Stage a change to file.txt:
echo four >> file.txt
git add file.txt
git statusgit status lists file.txt under "Changes to be committed." Now unstage it:
git reset -- file.txt
git statusThe file moves to "Changes not staged for commit." Open file.txt and the line four is still there. Only the index entry was reverted to the HEAD version.
The -- separates options and revisions from paths. It is optional when the name cannot be mistaken for a branch or commit, but it removes any doubt. If a branch and a file share the name file.txt, Git would otherwise stop and ask you to be explicit.
Unstaging several files
You can give more than one path, or a directory:
git reset -- src/ README.mdRunning git reset with no arguments at all unstages everything. The index is reset to HEAD, and the working directory is left alone, as in --mixed.
Resetting a path to another commit
You can name a commit before the path. This copies the file's version from that commit into the index:
git reset HEAD~2 -- file.txt
git statusNow the index holds the version of file.txt from two commits ago. The working directory still holds your current version. git status therefore shows two things: a staged change that reverts the file to the old content, and an unstaged change that brings your current content back. This is a legitimate but uncommon use. To restore a file's contents on disk, use git restore, covered in a later chapter.
Common mistakes
- Expecting the edits to disappear. Path reset does not discard changes. If you want them gone, that is a different command and a destructive one.
- Unstaging a brand-new file. A new file has no entry in HEAD, so resetting it removes it from the index. It then shows as untracked, and its contents stay on disk.
- Using it before the first commit. In a repository with no commits, there is no HEAD to copy from. Current Git versions handle this and unstage the file, but older ones may report an error.
Exercise
Exercise
You staged a.txt and b.txt with git add. You want to commit only a.txt. Which command unstages b.txt without losing your edits to it, and where do the edits end up?
Show answer
Run git reset -- b.txt. Git copies the HEAD entry for b.txt into the index, so the file is no longer staged. Your edits stay in the working directory and show as unstaged modifications (or as untracked, if b.txt is a new file). You can then run git commit to commit only a.txt.
Exercise
Why does git reset --hard -- file.txt fail, when git reset -- file.txt succeeds?
Show answer
Path reset only updates the index from a commit and never touches the working directory or moves the branch. --hard and --soft describe how far a reset reaches into those other areas, so Git rejects them when a path is given.