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
Does git "dirty" mean files not staged, or not committed? (glossary conflict)
提问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 file
with twolines in it. But the work-tree version of file
has just the one line in it, and matches what's in the repository.
此时,索引中file
有两行。但是工作树版本中file
只有一行,并且与存储库中的内容相匹配。
Is file
dirty? Well, git status --short
says that it is, twice (two M
s). Both git diff
and git diff --cached
show changes (so yes, it's "dirty"), but git diff HEAD
says there's no change, and if we git add
it againand try git status
:
是file
脏?好吧,git status --short
说它是,两次(两个M
s)。无论git diff
和git 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 status
so 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 HEAD
and 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 HEAD
version, and the working-directory version is the same as the HEAD
version, 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 HEAD
commit".
我认为,“正确”的定义是,如果没有要提交的更改,并且“提交提交的树”(索引内容)和“工作目录”之间没有任何更改,则您的树是“干净的”。但是,单独询问索引是否干净(即,没有任何准备提交)和/或工作树是否干净(未更改)关于fill-in-the-blank是合理的,其中空白可以是用“暂存区”或“HEAD
提交”填充。
What git status
tells 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-only
or similar) to see what, if anything, is different between the work-tree and the HEAD
commit unless(as is often the case) the index matches the HEAD
commit.
什么git status
告诉你俩的答案是“是什么,如果有的话,是上演提交”和“是什么,如果有的话,是工作树和索引之间的不同”。您必须使用git diff HEAD
(您可能想要添加--name-only
或类似)来查看工作树和HEAD
提交之间有什么不同(如果有的话),除非(通常情况下)索引与HEAD
提交匹配。
回答by VonC
What
git status
tells 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 thatgit status
also 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.org
article if correct, while the gitguys.com
article 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
文章几乎是错误的。你可能应该向他们指出这一点。