1434

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?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
PHLAK
  • 22,023
  • 18
  • 49
  • 52

19 Answers19

2155

If I understand the question correctly, you simply want to "undo" the git add that was done for that file.

If you need to remove a single file from the staging area, use

git reset HEAD -- <file>

If you need to remove a whole directory (folder) from the staging area, use

git reset HEAD -- <directoryName>

Your modifications will be kept. When you run git status the file will once again show up as modified but not yet staged.

See the git reset man page for details.

Kurtis Jungersen
  • 2,204
  • 1
  • 26
  • 31
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 48
    Thanks... I just noticed that that is stated just above the staged files. Guess I've been looking at that screen so long I was selectively choosing what I wanted to see. – PHLAK Oct 01 '09 at 20:34
  • 2
    This unstages all of my changes, contrary to every advice I've gotten anywhere (man pages, here, friends, &c). I keep thinking some day it's going to do what's advertised, but no. – rektide Jun 27 '13 at 23:34
  • 6
    Is there anyway to remove files from staging when there haven't been any commits to the repo? – Jared Forsyth Jun 29 '13 at 02:21
  • 1
    quick tip @JaredForsyth try rm -rf .git if your repository is just created, then use git init to recreate empty repository, but only in case that your repo (local repo) is totally empty without any commits – David Jun 06 '15 at 17:32
  • I do think adding `--` makes the answer more complete, this forces git to interpret the proceeding text as a filename – stolli Aug 07 '15 at 20:49
  • 3
    @Jared Forsyth To remove a file never commited from the stage use the command `git rm --cached FILE` as suggested by another answer. – chmike Jan 17 '16 at 15:42
  • Waaaay counter-intuitive, but works. I would have thought that to undo an add, it'd be something like "git add -r ". But this is git! – SMBiggs Mar 04 '16 at 18:00
  • the `git reset HEAD -- ` syntax doesn't works! It would be better to use `git reset HEAD ` [without -- ] – sheetal Jul 18 '18 at 08:23
  • confusing way of reporting unstaged changes after commit led me to initially think it wasn't working. – simon coleman Dec 12 '18 at 16:54
  • It also worked for me without the double dash, e.g. `git reset HEAD ` – jmm Mar 20 '19 at 14:41
  • This helped me. I was confused between `rm --cache` or `reset`. Thanks. :) – Kumar Rohit Mar 10 '21 at 21:27
  • I come to this answer many times, only to come yet again... – Saurabh Tiwari Sep 07 '21 at 10:01
166
git rm --cached FILE

,

git rm -r --cached CVS */CVS
user335425
  • 1,773
  • 1
  • 10
  • 2
  • 31
    true, but it's better to use `git reset` here I think, as you may ommit the --chached option and get sad quickly using the `git rm` command :-) With `git reset` you're on the safe side, if you forget to add "some option" it's gonna keep the change so it's "safer" for daily use (I'm talking about `git reset --hard`). – Konrad 'ktoso' Malawski Mar 06 '11 at 12:42
  • 20
    This method is useful if you don't have previous commits. – SomeKittens Apr 03 '13 at 00:42
  • 3
    `git rm --cached FILE` stages file deletion, without deleting the file form the working tree. This is different than the question, which was about undoing `git add`. – Sampo Smolander Aug 18 '15 at 04:57
  • 1
    This answer throws an error if you have deleted the file from the working tree. – Samuel Robert Oct 19 '16 at 07:32
  • 5
    `git rm --cached` will cause the file being removed from the index, i.e. the file will become **untracked** file. I don't think this is what OP wants. Please see a related thread here: https://stackoverflow.com/questions/45047810/why-git-rm-cached-remove-the-up-to-date-files – smwikipedia Jul 12 '17 at 07:17
  • this will just make the file untracked. the added file with git add still remains staged. – infoclogged Aug 11 '17 at 13:56
108

git reset <file>

Works whether or not you have any previous commits.

MTS
  • 1,845
  • 2
  • 17
  • 18
56

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>
KatieK
  • 13,586
  • 17
  • 76
  • 90
GuerillaNerd
  • 1,130
  • 1
  • 10
  • 12
  • 9
    What does the `--` do and why add it? I did `git reset HEAD ` and it worked. – Paolo Mar 03 '14 at 10:13
  • 19
    `--` is kind of a divider. In case the file name is unconventional e.g.(`-f` or `master`) git would interpret it as command line argument or branch name instead of file name. [See here](http://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes) – Andrew Jul 24 '14 at 23:31
  • 3
    This worked for me where the command without -- did not due to having no previous commits for that file. Thanks. – Matt Dec 06 '14 at 11:12
27

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.

Ruik
  • 1,222
  • 1
  • 20
  • 36
24
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.

Liam
  • 27,717
  • 28
  • 128
  • 190
Vlad Cioaba
  • 298
  • 2
  • 8
7

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.

fede1024
  • 3,099
  • 18
  • 23
6

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.

Kyung Lee
  • 925
  • 1
  • 10
  • 21
6

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.

Mike
  • 706
  • 7
  • 16
4

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.

thilinarmtb
  • 239
  • 4
  • 11
4

I think you could go for

git restore --staged <path/to/file_name>

netlemon
  • 964
  • 8
  • 22
3

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.

2

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
Vy Do
  • 46,709
  • 59
  • 215
  • 313
2

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

git checkout -- <file>
  • 1
    this works only if you are trying to undo changes in a file that isn't staged. If you want to undo the changes of a file that is stages, you first need to `git reset HEAD -- ` before running your command. – lucasreta Mar 03 '21 at 15:47
1

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.

rite2hhh
  • 372
  • 2
  • 15
0

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)
azun
  • 391
  • 4
  • 11
-1

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.

nevosial
  • 1,034
  • 2
  • 13
  • 20
-4

To unstage everything at once, run this command

git reset HEAD -- .
Devendra Verma
  • 938
  • 1
  • 13
  • 29
-9

git checkout -- <file>

It works perfectly to remove files from Staging Area

d4Rk
  • 6,622
  • 5
  • 46
  • 60