[git] How do I remove a single file from the staging area (undo git add)?

Situation: I have a Git repository with files already in the index. I make changes to several files, open Git and add these files to my staging area with "git add ."

Question: How do I remove one of those files from the staging area but not remove it from the index or undo the changes to the file itself?

This question is related to git version-control staging

The answer is


If you want to remove files following a certain pattern and you are using git rm --cached, you can use file-glob patterns too.

See here.


You need to be in the directory of the file and then type the following into the terminal

git reset HEAD .

Assumption is that you need to reset one file only.


You want:

  • Affect to a single file

  • Remove file from staging area

  • Not remove single file from index

  • Don't undo the change itself

and the solution is

git reset HEAD file_name.ext

or

git reset HEAD path/to/file/file_name.ext

To unstage everything at once, run this command

git reset HEAD -- .

After version 2.23, Git has introduced the git restore command which you can use to do that. Quoting the official documentation:

Restore specified paths in the working tree with some contents from a restore source. If a path is tracked but does not exist in the restore source, it will be removed to match the source.

The command can also be used to restore the content in the index with --staged, or restore both the working tree and the index with --staged --worktree.

So you can invoke git restore --staged <path> and unstage the file but also keep the changes you made. Remember that if the file was not staged you lose all the changes you made to it.


So, a slight tweak to Tim Henigan's answer: you need to use -- before the file name. It would look like this:

git reset HEAD -- <file>

For newer versions of Git there is git restore --staged <file>.

When I do a git status with Git version 2.26.2.windows.1 it is also recommended for unstaging:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)

(This post shows, that in earlier versions git reset HEAD was recommended at this point)

I can highly recommend this post explaining the differences between git revert, git restore and git reset and also additional parameters for git restore.


My sample:

$ git status
On branch feature/wildfire/VNL-425-update-wrong-translation
Your branch and 'origin/feature/wildfire/VNL-425-update-wrong-translation' have diverged,
and have 4 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   ShopBack/Source/Date+Extension.swift
    modified:   ShopBack/Source/InboxData.swift
    modified:   ShopBack/en.lproj/Localizable.strings

As you may notice

> Changes to be committed:
>       (use "git reset HEAD <file>..." to unstage)

You need to be in the directory of the file and then type the following into the terminal

git checkout -- <file>

In case you just want to remove a subset of the changes to your file, you can use:

git reset -p

or

git reset -p <file_name>

This command is basically the reverse of git add -p: it will only remove the selected changes from the staging area. I find it extremely useful in "unadding" something that I added by mistake.


git rm --cached FILE

,

git rm -r --cached CVS */CVS

git reset <file>

Works whether or not you have any previous commits.


git checkout -- <file>

It works perfectly to remove files from Staging Area


I think you probably got confused with the concept of index, as @CB Bailey commented:

The staging area is the index.

You can simply consider staging directory and index as the same thing.
So, just like @Tim Henigan's answer, I guess:

you simply want to "undo" the git add that was done for that file.



Here is my answer:

Commonly, there are two ways to undo a stage operation, as other answers already mentioned:

git reset HEAD <file>

and

git rm --cached <file>

But what is the difference?

Assume the file has been staged and exists in working directory too, use git rm --cached <file> if you want to remove it from staging directory, and keep the file in working directory. But notice that this operation will not only remove the file from staging directory but also mark the file as deleted in staging directory, if you use

git status

after this operation, you will see this :

        deleted:    <file>

It's a record of removing the file from staging directory. If you don't want to keep that record and just simply want to undo a previous stage operation of a file, use git reset HEAD <file> instead.


-------- END OF ANSWER --------

PS: I have noticed some answers mentioned:

git checkout -- <file>

This command is for the situation when the file has been staged, but the file has been modified in working directory after it was staged, use this operation to restore the file in working directory from staging directory. In other words, after this operation, changes happen in your working directory, NOT your staging directory.


When you do git status, Git tells you how to unstage:

Changes to be committed: (use "git reset HEAD <file>..." to unstage).

So git reset HEAD <file> worked for me and the changes were un-touched.


git reset filename.txt

If you have a modification in filename.txt , you added it to stage by mistake and you want to remove the file from staging but you don't want to lose the changes.


If you make changes to many tracked files but only want to stage a few of them, doing a

git add .

isn't always favorable (or recommended)- as it stages all the tracked files (some cases where you want to keep changes only to yourself and don't want to stage them to the remote repository).

nor is it ideal doing a bunch of

git add path/to/file1 path/to/file2

if you have a lot of nested directories (which is the case in most projects) - gets annoying

That's when Git GUI is helpful (probably only time I use it). Just open Git GUI, it shows staged and unstaged file sections. Select the files from the staged section that you want to unstage and press

Ctrl+U (for windows)

to unstage them.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to version-control

How can I switch to another branch in git? Do I commit the package-lock.json file created by npm 5? Project vs Repository in GitHub Remove a modified file from pull request Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: How to squash all commits on branch git: updates were rejected because the remote contains work that you do not have locally Sourcetree - undo unpushed commits Cannot checkout, file is unmerged Git diff between current branch and master but not including unmerged master commits

Examples related to staging

git add only modified changes and ignore untracked files How do I remove a single file from the staging area (undo git add)?