How to remove a file from the index in git?
如何从索引中删除文件(= staging area = cache)而不从文件系统中删除文件?
You want:
git rm --cached [file]
If you omit the --cached
option, it will also delete it from the working tree. git rm
is slightly safer than git reset
, because you'll be warned if the staged content doesn't match either the tip of the branch or the file on disk. (If it doesn't, you have to add --force
.)
这应该为你取消一个<file>(不删除或修改文件):
git reset HEAD <file>
Depending on your workflow, this may be the kind of thing that you need rarely enough that there's little point in trying to figure out a command-line solution (unless you happen to be working without a graphical interface for some reason).
Just use one of the GUI-based tools that support index management, for example:
git gui
<-- uses the Tk windowing framework -- similar style to gitk
git cola
<-- a more modern-style GUI interface These let you move files in and out of the index by point-and-click. They even have support for selecting and moving portions of a file (individual changes) to and from the index.
How about a different perspective: If you mess up while using one of the suggested, rather cryptic, commands:
git rm --cached [file]
git reset HEAD <file>
...you stand a real chance of losing data -- or at least making it hard to find. Unless you really need to do this with very high frequency, using a GUI tool is likely to be safer .
Working without the index
Based on the comments and votes, I've come to realize that a lot of people use the index all the time. I don't. Here's how:
git commit -a
git commit (list of files)
git commit -a
then amend via git gui
git difftool --dir-diff --tool=meld
上一篇: Git:显示提交后的文件内容
下一篇: 如何从git中的索引中删除文件?