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?
git reset <file>
Works whether or not you have any previous commits.
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
to unstage them.
git checkout -- <file>
It works perfectly to remove files from Staging Area
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.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.
When you do
git status
, Git tells you how to unstage:So
git reset HEAD <file>
worked for me and the changes were un-touched.