git是否“肮脏"?是指文件未上演,还是未提交? (词汇冲突) [英] Does git "dirty" mean files not staged, or not committed? (glossary conflict)
问题描述
https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_dirty 如果工作树包含尚未提交给当前分支的修改,则称该工作树为脏".
https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_dirty A working tree is said to be "dirty" if it contains modifications which have not been committed to the current branch.
http://www.gitguys.com/topics/glossary/ 工作目录肮脏 如果文件在索引中更新后已经在工作目录中进行了更新,则该工作目录被认为是脏文件".如果工作目录中所有已修改的文件都已添加到索引中,则工作目录是干净的.
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.
如果我理解正确,那么索引"也称为临时区域",它是在您更改文件,想要提交文件但避风港的情况下将文件存储(复制到?符号链接?)的地方.尚未提交. (第一个词汇表中的暂存区域也可以用于合并.第二个词汇表中的文件通过"git add"移动到那里.)
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'.)
因此,两个词汇表似乎在说不兼容的话.哪个是正确的?还是有某种方法可以使它们都正确?
So the two glossaries seem to be saying incompatible things. Which is correct? Or is there some way they could both be correct?
推荐答案
它们实际上都是合理的主张.我认为最佳答案"是,两者都是错误的,尽管前者(kernel.org版本)可能更接近.
They're actually both reasonable claims. I think the "best answer" is that both are wrong, although the former (the kernel.org version) is probably closer.
考虑:
$ 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
此时,索引包含file
,其中包含两条行.但是file
的工作树版本只有一行,并且与存储库中的内容匹配.
At this point, the index has file
with two lines in it. But the work-tree version of file
has just the one line in it, and matches what's in the repository.
file
是否脏了?好吧,git status --short
说是两次(两个M
). git diff
和git diff --cached
都显示更改(所以,是的,它是脏"的),但是git diff HEAD
表示没有更改,如果再次 尝试git status
,请尝试:
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 again and try git status
:
$ git status --short
MM file
$ git diff HEAD
$ git add file
$ git status
# On branch master
nothing to commit, working directory clean
让我们把这个奇怪的变化放回去,再做一件事.这次让我们使用git status
的长格式,以便为我们提供更多信息:
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:
$ 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
#
它表示我们可以将git reset
(与git reset --mixed
相同)与HEAD
一起使用,并且文件名可以取消登台;肯定会使工作目录变脏吗? :-)
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 HEAD file
$ git status
# On branch master
nothing to commit, working directory clean
不,实际上,它使工作目录再次干净!这来自git diff HEAD
的(缺少)输出:取消暂存"添加第二行的更改使索引引用HEAD
版本,并且工作目录版本与
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 and no working-directory changes.
我认为,正确"的定义是,如果没有更改要提交的 和为暂存的提交的树"(索引的内容)与树之间没有任何更改,则您的树是干净的" 工作目录".但是,有必要分别询问关于空白填充,其中可以用登台区域"或"HEAD
提交"填充空白.
The "right" definition is, I think, that your tree is "clean" if there are no changes to commit and no 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".
git status
告诉您的答案是为登台准备进行什么操作"和在工作树和索引之间有什么区别".您必须使用git diff HEAD
(您可能想添加--name-only
或类似的东西)来查看工作树和HEAD
提交除非之间有什么不同(如果有的话)(照原样)通常情况下)索引与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是否“肮脏"?是指文件未上演,还是未提交? (词汇冲突)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!