git diff 查看文件差异

git diff 命令用于对比文件差异,能够看到哪些文件进行了修改,新增了多少行,删除了多少行,也能对比不同版本、不同tag,甚至不同commit 之间的差异,功能非常强大。

1、差异统计

常用示例:git diff --stat

--stat 参数表示统计汇总,比如对代码进行一通修改后,想了解一共修改了多少个文件,新增了多少行,删除了多少行,就可以使用 git diff --stat,将所有变更信息统计出来,如下所示:

$ git diff --stat
README  | 7 +++++--
VERSION | 1 +
2 files changed, 6 insertions(+), 2 deletions(-)

常用示例:git diff -- test.txt

-- test.txt 表示具体某个文件的差异,不加 -- 具体文件,显示所有差异。

2、两次提交之间的差异

(1)统计两次提交之间的差异
git diff --stat  commit_id1 commit_id2   

(2)统计两次提交之间某个文件的差异
git diff --stat  commit_id1 commit_id2 -- test.txt

(3)查看两次提交之间某个文件的差异明细
git diff commit_id1 commit_id2 -- test.txt


3、两个分支的差异

统计汇总:
git diff --stat branch1 branch2
git diff --stat branch1 branch2 -- test.txt

差异明细:
git diff branch1 branch2 -- test.txt

远程分支也可以对比差异:
git diff --stat remotes/origin/branch1 remotes/origin/branch2
git diff --stat remotes/origin/branch1 remotes/origin/branch2 -- test.txt
git diff remotes/origin/branch1 remotes/origin/branch2 -- test.txt


4、两个标签(tag)的差异

统计汇总:
git diff --stat tag1 tag2
git diff --stat tag1 tag2 -- test.txt

差异明细:
git diff tag1 tag2 -- test.txt


5、前2次提交的某个文件差异

git diff head~ head test.txt

head表示最后一次的提交,head~表示最后一次提交之前的提交。


文章评论

0条评论