How do I remove a single file from the staging are

2019-01-20 20:48发布

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 staging
14条回答
狗以群分
2楼-- · 2019-01-20 21:20

git reset <file>

Works whether or not you have any previous commits.

查看更多
女痞
3楼-- · 2019-01-20 21:22

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.

查看更多
Melony?
4楼-- · 2019-01-20 21:23

git checkout -- <file>

It works perfectly to remove files from Staging Area

查看更多
干净又极端
5楼-- · 2019-01-20 21:25

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.

查看更多
戒情不戒烟
6楼-- · 2019-01-20 21:30
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.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-20 21:30

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.

查看更多
登录 后发表回答