参考博文 https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/diffsjava
若是 git status
命令的输出对于你来讲过于模糊,你想知道具体修改了什么地方,能够用 git diff
命令。 稍后咱们会详细介绍 git diff
,你可能一般会用它来回答这两个问题:当前作的哪些更新尚未暂存? 有哪些更新已经暂存起来准备好了下次提交? 尽管 git status
已经经过在相应栏下列出文件名的方式回答了这个问题,git diff
将经过文件补丁的格式显示具体哪些行发生了改变。git
假如再次修改 README 文件后暂存,而后编辑 CONTRIBUTING.md
文件后先不暂存, 运行 status
命令将会看到:github
git status $On branch masterChanges to be committed:(use "git reset HEAD <file>..." to unstage)modified: READMEChanges 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: CONTRIBUTING.md
要查看还没有暂存的文件更新了哪些部分,不加参数直接输入 git diff
:less
git diff $diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 8ebb991..643e24f 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -65,7 +65,8 @@ branch directly, things can get messy.Please include a nice description of your changes when you submit your PR;if we have to read the whole diff to figure out why you're contributingin the first place, you're less likely to get feedback and have your change-merged in.+merged in. Also, split your changes into comprehensive chunks if your patch is+longer than a dozen lines.If you are starting to work on a particular area, feel free to submit a PRthat highlights your work in progress (and note in the PR title that it's
此命令比较的是工做目录中当前文件和暂存区域快照之间的差别, 也就是修改以后尚未暂存起来的变化内容。spa
若要查看已暂存的将要添加到下次提交里的内容,能够用 git diff --cached
命令。(Git 1.6.1 及更高版本还容许使用 git diff --staged
,效果是相同的,但更好记些。)code
git diff --staged $diff --git a/README b/READMEnew file mode 100644index 0000000..03902a1--- /dev/null+++ b/README@@ -0,0 +1 @@+My Project
请注意,git diff 自己只显示还没有暂存的改动,而不是自上次提交以来所作的全部改动。 因此有时候你一会儿暂存了全部更新过的文件后,运行 git diff
后却什么也没有,就是这个缘由。ip
像以前说的,暂存 CONTRIBUTING.md
后再编辑,运行 git status
会看到暂存先后的两个版本。 若是咱们的环境(终端输出)看起来以下:get
git add CONTRIBUTING.md CONTRIBUTING.md git status $$echo'# test line'>>$On branch masterChanges to be committed:(use "git reset HEAD <file>..." to unstage)modified: CONTRIBUTING.mdChanges 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: CONTRIBUTING.md
如今运行 git diff
看暂存先后的变化:it
$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 643e24f..87f08c8 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -119,3 +119,4 @@ at the## Starter ProjectsSee our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).+# test line
而后用 git diff --cached
查看已经暂存起来的变化:(--staged 和 --cached 是同义词)io
$ git diff --cached
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 8ebb991..643e24f 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -65,7 +65,8 @@ branch directly, things can get messy.Please include a nice description of your changes when you submit your PR;if we have to read the whole diff to figure out why you're contributingin the first place, you're less likely to get feedback and have your change-merged in.+merged in. Also, split your changes into comprehensive chunks if your patch is+longer than a dozen lines.If you are starting to work on a particular area, feel free to submit a PRthat highlights your work in progress (and note in the PR title that it's
git diff filename
git diff --name-status head origin
注意最好先cd 到相应的文件夹下面不然可能由于路径问题,找不到文件。
git diff head origin src/com/hp/auth/LogAgent.java
$ git diff --name-only head origin