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条回答
小情绪 Triste *
2楼-- · 2019-01-20 21:12

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
查看更多
做个烂人
3楼-- · 2019-01-20 21:13

To unstage everything at once, run this command

git reset HEAD -- .
查看更多
戒情不戒烟
4楼-- · 2019-01-20 21:14

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-20 21:15

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.

查看更多
何必那么认真
6楼-- · 2019-01-20 21:17

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.

查看更多
冷血范
7楼-- · 2019-01-20 21:18

In my case, I do

git checkout -- FILE
查看更多
登录 后发表回答