git 如何从“舞台区”删除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7193552/
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-19 05:53:57  来源:igfitidea点击:

How to delete file from "stage area"?

git

提问by smwikipedia

After I made a commit, I want to clean the stage area. Here is what I did:

提交后,我想清理舞台区域。这是我所做的:

git add 1.txt    ----- stage the 1.txt file
git commit -m "commit 1.txt"  ----- commit the 1.txt file
git reset HEAD 1.txt   ----- I want to clean the stage area

Then I check the stage area with this command:

然后我用这个命令检查舞台区域:

git ls-files

The 1.txt is still shown.

1.txt 仍然显示。

Why?

为什么?

回答by Lukas Stejskal

Commiting changes will clean the stage area, you don't have to do anything after commit.

提交更改将清理舞台区域,提交后您无需执行任何操作。

Otherwise use git reset:

否则使用git reset

git add 1.txt    ----- stage the 1.txt file
git reset 1.txt    ----- remove 1.txt file from stage

Also, use git statusinstead of git ls-files.

另外,使用git status代替git ls-files.

回答by manojlds

Short answer:

简答:

Your staging is clean, atleast wrt HEAD if not the working directory too, once you commit.

一旦你提交,你的登台是干净的,如果不是工作目录,至少也是 HEAD 。

Long answer:

长答案:

You have committed the file 1.txt. When you commit the index (staging) and HEAD become the same. And when you do git reset HEAD 1.txt, it is basically a noop, as both index and HEAD have the same version of 1.txt.

您已提交文件1.txt。当您提交索引(暂存)时,HEAD 变得相同。当你这样做时git reset HEAD 1.txt,它基本上是一个 noop,因为 index 和 HEAD 都有相同的 1.txt 版本。

And git ls-fileswill display 1.txtbecause the file is in your repo.

并且git ls-files会显示,1.txt因为该文件在您的存储库中。

Not really sure what you are trying to do. But once you have committed a file, you can unstage the modifications you have done to it. In the case of a newly added file, unstaging the modifications is the same as removing the newly added file from index and hence unstaging it. But that is not the same for a file already in the repo.

不太确定您要做什么。但是一旦提交了文件,就可以取消对它所做的修改。对于新添加的文件,取消暂存修改与从索引中删除新添加的文件并因此取消暂存相同。但是对于已经在 repo 中的文件来说,情况就不一样了。

Ideal way to remove file from staging, is git rm --cached- and when you commit, you remove the file from the repo as well. Remember that staging is the view of what HEAD will be once you commit.

从暂存中删除文件的理想方法是git rm --cached- 当你提交时,你也从 repo 中删除文件。请记住,分期是您提交后 HEAD 的视图。

But I think git rm --cachedis not what you want to do as you just want a clean staging / index. After you commit it is clean anyway ( atleast wrt HEAD if not the working directory).

但我认为git rm --cached这不是您想要做的,因为您只想要一个干净的暂存/索引。在你提交之后它仍然是干净的(如果不是工作目录,至少 wrt HEAD)。

回答by Andy

git ls-filesshows what you are currently tracking. Whether you have committed that file or not.

git ls-files显示您当前正在跟踪的内容。无论您是否提交了该文件。

If you have committed the file, it points to the same file that the actual commit points to. If you do a git diff --cached, those will be the changes that your staging area has, but haven't been commited

如果您已提交该文件,则它指向实际提交所指向的同一文件。如果您执行 a git diff --cached,则这些将是您的暂存区已进行但尚未提交的更改

The "staging area" is just the difference between what HEAD is pointing to in the repository and what the index is pointing to. It's not a physical file that you flush.

“暂存区”只是 HEAD 在存储库中指向的内容与索引指向的内容之间的区别。这不是您刷新的物理文件。

回答by Philip Oakley

The staging area confuses many [been there, got scars].

集结区让许多人感到困惑 [去过那里,有疤痕]。

One of the better explanations is in the git wiki FAQ on why git rmis not the opposite of git addhere.

更好的解释之一是在 git wiki FAQ 关于为什么git rm不是git add这里的相反。

I use the visualisation of a storyboard wall where copies are posted (staged), and need to be actively removed from the wall as a separate action from any adjustments at you desk (working directory).

我使用故事板墙的可视化,其中副本被张贴(上演),并且需要从墙上主动移除,作为与您办公桌(工作目录)的任何调整分开的单独操作。

The staging area is also used during merging, to avoid cluttering your personal work area.

合并期间也使用暂存区,以避免弄乱您的个人工作区。

回答by mripard

Because git ls-files lists all the files tracked by git.

因为 git ls-files 列出了 git 跟踪的所有文件。

You probably want git status here.

你可能想要 git status 在这里。