可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
git rm --cached FILE
,
git rm -r --cached CVS */CVS
回答3:
git reset <file>
Works whether or not you have any previous commits.
回答4:
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>
回答5:
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.
回答6:
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.
回答7:
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.
回答8:
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
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
To unstage everything at once, run this command
git reset HEAD -- .
回答13:
git checkout -- <file>
It works perfectly to remove files from Staging Area
回答14:
In my case, I do
git checkout -- FILE