git 如何从git中的索引中删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2223308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:48:54  来源:igfitidea点击:

How to remove a file from the index in git?

git

提问by hcs42

How to remove a file from the index ( = staging area = cache) without removing it from the file system?

如何从索引(= 暂存区 = 缓存)中删除文件而不将其从文件系统中删除?

回答by John Feminella

You want:

你要:

git rm --cached [file]

If you omit the --cachedoption, it will also delete it from the working tree. git rmis 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.)

如果省略该--cached选项,它也会从工作树中删除它。git rm比 稍安全git reset,因为如果暂存内容与分支的尖端或磁盘上的文件不匹配,您将收到警告。(如果没有,则必须添加--force。)

回答by David Underhill

This should unstage a <file> for you (without removing or otherwise modifying the file):

这应该为您取消暂存 <file> (无需删除或以其他方式修改文件):

git reset <file>

回答by Abdul Gafoor

git reset HEAD <file> 

for removing a particular file from the index.

用于从索引中删除特定文件。

and

git reset HEAD

git reset HEAD

for removing all indexed files.

用于删除所有索引文件。

回答by nobar

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:

只需使用支持索引管理的基于 GUI 的工具之一,例如:

  • git gui<-- uses the Tk windowing framework -- similar style to gitk
  • git cola<-- a more modern-style GUI interface
  • git gui<-- 使用 Tk 窗口框架 -- 类似的风格 gitk
  • git cola<-- 更现代风格的 GUI 界面

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>
  • 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.

...您确实有可能丢失数据——或者至少很难找到。除非您确实需要非常频繁地执行此操作,否则使用 GUI 工具可能更安全



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:

根据评论和投票,我开始意识到很多人一直在使用该索引。我不。就是这样:

  • Commit my entire working copy (the typical case): git commit -a
  • Commit just a few files: git commit (list of files)
  • Commit all but a few modified files: git commit -athen amendvia git gui
  • Graphically review all changes to working copy: git difftool --dir-diff --tool=meld
  • 提交我的整个工作副本(典型情况): git commit -a
  • 只提交几个文件: git commit (list of files)
  • 提交除了少数修改过的文件之外的所有文件:git commit -a然后通过修改git gui
  • 以图形方式查看对工作副本的所有更改: git difftool --dir-diff --tool=meld