git "dirty" 是否意味着文件未暂存或未提交?(术语冲突)

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

Does git "dirty" mean files not staged, or not committed? (glossary conflict)

git

提问by ChrisPhoenix

https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_dirtyA working tree is said to be "dirty" if it contains modifications which have not been committed to the current branch.

https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_dirty如果工作树包含尚未提交到当前分支的修改,则它被称为“脏”。

http://www.gitguys.com/topics/glossary/Dirty working directory If files have been updated in the working directory after they were updated in the index then the working directory is considered “dirty”. The working directory is clean if all modified files in the working directory have been added to the index.

http://www.gitguys.com/topics/glossary/脏工作目录 如果文件在索引中更新后在工作目录中进行了更新,则工作目录被视为“脏”。如果工作目录中所有修改过的文件都已添加到索引中,则工作目录是干净的。

If I understand correctly, the "index" is also known as the "staging area" and is a place where files will be stored (copied to? symlinked?) when you have changed them, want to commit them, but haven't done a commit yet. (The first glossary says the staging area can also be used for merging. The second glossary says files are moved there by 'git add'.)

如果我理解正确,“索引”也称为“暂存区”,是您更改文件时将存储文件(复制到?符号链接?)的地方,想要提交它们,但还没有完成一个提交。(第一个词汇表说暂存区也可以用于合并。第二个词汇表说文件被“git add”移动到那里。)

So the two glossaries seem to be saying incompatible things. Which is correct? Or is there some way they could both be correct?

所以这两个词汇表似乎在说不相容的东西。哪个是正确的?或者有什么方法可以让他们都正确?

回答by torek

They're actually both reasonable claims. I think the "best answer" is that bothare wrong, although the former (the kernel.org version) is probably closer.

它们实际上都是合理的主张。我认为“最佳答案”是两者都是错误的,尽管前者(kernel.org 版本)可能更接近。

Consider:

考虑:

$ mkdir /tmp/repo && cd /tmp/repo
$ git init
Initialized empty Git repository in /tmp/repo/.git/
$ echo contents > file
$ git add file
$ git commit -m initial
[master (root-commit) e1731a6] initial
 1 file changed, 1 insertion(+)
 create mode 100644 file

We now have a repository with one commit containing one file.

我们现在有一个包含一个文件的提交的存储库。

$ echo second line >> file; git add file; echo contents > file

At this point, the index has filewith twolines in it. But the work-tree version of filehas just the one line in it, and matches what's in the repository.

此时,索引中file行。但是工作树版本中file只有一行,并且与存储库中的内容相匹配。

Is filedirty? Well, git status --shortsays that it is, twice (two Ms). Both git diffand git diff --cachedshow changes (so yes, it's "dirty"), but git diff HEADsays there's no change, and if we git addit againand try git status:

file脏?好吧,git status --short说它是,两次(两个Ms)。无论git diffgit diff --cached变化显示,(所以是的,它的“脏”),但git diff HEAD说有没有变化,如果我们git add一次和尝试git status

$ git status --short
MM file
$ git diff HEAD
$ git add file
$ git status
# On branch master
nothing to commit, working directory clean

Let's put that odd change back and do one more thing. This time let's use the long form of git statusso that it gives us more information:

让我们把那个奇怪的改变放回去,再做一件事。这次让我们使用长形式,git status以便它为我们提供更多信息:

$ echo second line >> file; git add file; echo contents > file
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   file
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   file
#

It says we can use git reset(which is the same as git reset --mixed) with HEADand the file name to un-stage; surely that will make the working directory dirty? :-)

它说我们可以使用git reset(与 相同git reset --mixed) withHEAD和文件名来取消暂存;肯定会使工作目录变脏吗?:-)

$ git reset HEAD file
$ git status
# On branch master
nothing to commit, working directory clean

No, in fact, it makes the working directory clean again! This follows from the (lack of) output of git diff HEAD: "un-staging" the change that adds the second line makes the index refer to the HEADversion, and the working-directory version is the same as the HEADversion, so un-staging the "changes to be committed" causes there to be nothing to commit andno working-directory changes.

不,事实上,它使工作目录再次干净!这是从(缺乏)输出git diff HEAD:“un-staging”添加第二行的更改使索引引用HEAD版本,并且工作目录版本与HEAD版本相同,因此取消暂存“要提交的更改”导致没有任何内容要提交,也没有工作目录更改。

The "right" definition is, I think, that your tree is "clean" if there are no changes to commit andno changes between "tree staged for commit" (contents of index) and "work directory". However, it's reasonable to ask separately whether the index is clean (i.e., there is nothing staged for commit) and/or the work-tree is clean (unchanged) with respect to fill-in-the-blank, where the blank can be filled in with "the staging area" or "the HEADcommit".

我认为,“正确”的定义是,如果没有要提交的更改,并且“提交提交的树”(索引内容)和“工作目录”之间没有任何更改,则您的树是“干净的”。但是,单独询问索引是否干净(即,没有任何准备提交)和/或工作树是否干净(未更改)关于fill-in-the-blank是合理的,其中空白可以是用“暂存区”或“HEAD提交”填充。

What git statustells you is both the answer to "what, if anything, is staged for commit" and "what, if anything, is different between the work-tree and the index". You have to use git diff HEAD(you may want to add --name-onlyor similar) to see what, if anything, is different between the work-tree and the HEADcommit unless(as is often the case) the index matches the HEADcommit.

什么git status告诉你俩的答案是“是什么,如果有的话,是上演提交”和“是什么,如果有的话,是工作树和索引之间的不同”。您必须使用git diff HEAD(您可能想要添加--name-only或类似)来查看工作树和HEAD提交之间有什么不同(如果有的话),除非(通常情况下)索引与HEAD提交匹配。

回答by VonC

From torek's answer:

托雷克回答

What git statustells you is both the answer to:

  • "what, if anything, is staged for commit" and
  • "what, if anything, is different between the work-tree and the index".

什么git status告诉你俩的答案是:

  • “什么,如果有的话,是为了提交而上演的”和
  • “工作树和索引之间有什么不同,如果有的话”。

Note that only the working tree is involved with git status, not the "working directory".

请注意,只有工作树与 git status 相关,而不是“工作目录”。

Git 2.9.1+ (Q3 2016) will make that clearer.
See commit 2a0e6cd(09 Jun 2016) by Lars Vogel (vogella).
(Merged by Junio C Hamano -- gitster--in commit a010d61, 27 Jun 2016)

Git 2.9.1+(2016 年第三季度)将使这一点更加清晰。
请参阅Lars Vogel ( ) 的commit 2a0e6cd(2016 年 6 月 9 日)(由Junio C Hamano合并-- --in commit a010d61,2016 年 6 月 27 日)vogella
gitster

Use "working tree" instead of "working directory" for git status

Working directory can be easily confused with the current directory.
In one of my patches I already updated the usage of working directory with working tree for the man page but I noticed that git statusalso uses this incorrect term.

使用“工作树”而不是“工作目录” git status

工作目录很容易与当前目录混淆
在我的一个补丁中,我已经使用手册页的工作树更新了工作目录的使用,但我注意到它git status也使用了这个不正确的术语。

回答by Mad Physicist

According to the official Git documentation, in the section on Stashing, a dirty state is defined as ... the dirty state of your working directory — that is, your modified tracked files and staged changes. From this definition, files staged for commit are dirty as well. This means that the kernel.orgarticle if correct, while the gitguys.comarticle is pretty much wrong. You should probably point this out to them.

根据官方 Git 文档,在关于Stashing的部分中,脏状态被定义为... the dirty state of your working directory — that is, your modified tracked files and staged changes。根据这个定义,提交提交的文件也是脏的。这意味着kernel.org文章如果正确,而gitguys.com文章几乎是错误的。你可能应该向他们指出这一点。