简体   繁体   English

Git diff命令文档

[英]Git diff command documentation

I am having trouble understanding the explanation of git diff command given in the book linked at the git website. 我无法理解git网站上链接的书中给出的git diff命令的解释。

According to the Pro Git Book , it says 根据Pro Git Book ,它说

That command( git diff ) compares what is in your working directory with what is in your staging area. 该命令( git diff )比较工作目录中的内容与暂存区域中的内容。

.

From my working with git it seems as though git diff compares your working directory with your repository . 从我使用git看起来好像git diff将你的工作目录与你的存储库进行比较。 I have a file that has been committed to the repo. 我有一个已经提交给repo的文件。 I now modify this file. 我现在修改这个文件。 Without staging it, running git status shows me the file under Changes not staged for commit . 如果没有暂存它,运行git status会向我显示未提交的更改下的文件。 Now if I run git diff I get the difference in code between what was in the repo and what is in the working directory. 现在,如果我运行git diff我会在repo中的内容和工作目录中的内容之间获得代码差异。 Shouldn't I get no output, as the modified file was not even staged? 我不应该得到没有输出,因为修改后的文件甚至没有上演?

Is there something wrong with my understanding in what the author was trying to convey? 我对作者试图传达的内容的理解是否有问题?

  • git diff View difference between Stage and Working Directory git diff查看Stage和Working Directory之间的区别
  • git diff --staged View difference between HEAD and Stage git diff --staged查看HEAD和Stage之间的区别
  • git diff HEAD View difference between HEAD and Working Directory git diff HEAD查看HEAD和工作目录之间的区别

Here Stage is the changes you have marked to include in the next commit. 这里Stage是您标记为包含在下一次提交中的更改。

Working Directory is the current directory you are working on and making changes. Working Directory是您正在处理和进行更改的当前目录。

HEAD is a reference to the last commit in the currently checked-out branch. HEAD是对当前签出分支中最后一次提交的引用。

Try this once which might help you to clear up things: 尝试一次这可能有助于你清理事情:

Suppose I have a file test.txt and I have content Hello in the file which is currently in my repository. 假设我有一个文件test.txt ,我在目录中的文件中有内容Hello Now I change the file and add World and do git status : 现在我更改文件并添加World并执行git status

On branch master
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:   test.txt

And when I check git diff it will show something like this: 当我检查git diff ,它将显示如下内容:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World

Now if I stage this file and check git status : 现在,如果我git status此文件并检查git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   test.txt

Now I observe I forgot to add exclamation mark to the text so I add that and check git status again: 现在我注意到我忘记在文本中添加感叹号,所以我添加并再次检查git status

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   test.txt

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:   test.txt

So you can see that we have the same file in staged and unstaged area both. 所以你可以看到我们在staged和unstaged区域都有相同的文件。 And when I check git diff it shows me this: 当我检查git diff它会告诉我这个:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello World
+Hello World!

We have changed Hello World which is in staged area right now to Hello World! 我们已经改变了Hello World这是在上演区现在到Hello World! , so it compared to the staged area. ,所以它与舞台区域相比。 And now if I check git diff --staged : 现在,如果我检查git diff --staged

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World

This compares the staged changes to the HEAD (the last commit). 这将比较HEAD (最后一次提交)的staged更改。 As I have not staged the ! 因为我还没上演过! change it is not showing it here. 改变它没有在这里显示它。 And finally when I will do git diff HEAD it will show this: 最后,当我要做git diff HEAD它会显示:

--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-Hello
+Hello World!

The changes between HEAD (the last commit) and your Working Directory . HEAD (最后一次提交)和您的Working Directory之间的变化。 As the HEAD has only Hello in the file and in your Working Directory you have changed it to Hello World! 由于HEAD在文件和Working Directory只有Hello ,因此您已将其更改为Hello World! (it doesn't matters that you have not staged the ! change, it will just look the file for changes whether they are staged or unstaged). (并不重要的是你没有上演!更改,它只会查看文件的更改,无论它们是分阶段还是非分阶段)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM