如何使用git比较两次commit之间的差异文件
有时候需要查看两次提交之间的差异。
只需下面几个步骤即可。
1、git log 查看提交历史 如下:
commit 5dab4a955535fcc42832ce33696489d8e8b232ec Author: songchong <[email protected]> Date: Tue Aug 22 20:10:08 2017 +0800 git commit 5th commit c324747f80f9612f59dd7b575136261dd6dff01d Author: songchong <[email protected]> Date: Tue Aug 22 20:07:32 2017 +0800 git commit fourth commit 2584c25bc1288e9519f78b53cda9081978c56735 Author: songchong <[email protected]> Date: Tue Aug 22 19:47:47 2017 +0800 git commit three commit b8b70ca89eb574a12b1b49c5f454d5e0525f60d6 Author: songchong <[email protected]> Date: Tue Aug 22 19:45:34 2017 +0800 git commit two commit d13fd344d51d042cf48a8acab43070550cff9ce4 Author: songchong <[email protected]> Date: Tue Aug 22 19:44:42 2017 +0800 git commit one如果我们想查看第四次和第五次提交之间的差异。可以用如下命令:
git diff hash1 hash2 --stat
显示第四次和第五次这两次提交的差异文件即:
songchong@srv-artek-pad:~/mytest/songchong$ git diff c324747f80f9612f59dd7b575136261dd6dff01d 5dab4a955535fcc42832ce33696489d8e8b232ec --stat test | 4 ++-- test2 | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) songchong@srv-artek-pad:~/mytest/songchong$2、可以看出,git列出了四五两次提交之间的差异文件是 test 和 test2.
如果想进一步查看test修改了那些地方:
只需将 git diff hash1 hash2 --stat 中的 --stat改为具体的文件即可
songchong@srv-artek-pad:~/mytest/songchong$ git diff c324747f80f9612f59dd7b575136261dd6dff01d 5dab4a955535fcc42832ce33696489d8e8b232ec test diff --git a/test b/test index 6d6cdbb..60781a8 100644 --- a/test +++ b/test @@ -1,6 +1,6 @@ - +6666666666666666666 llllllllllllllaaaa - +8888888888888888888 dsfsdkgdfkghjjh songchong@srv-artek-pad:~/mytest/songchong$
3、如果想将两次提交的差异部分提取成补丁文件:
git diff hash1 hash2 filename > patch_name songchong@srv-artek-pad:~/mytest/songchong$ git diff c324747f80f9612f59dd7b575136261dd6dff01d 5dab4a955535fcc42832ce33696489d8e8b232ec test > testpatch songchong@srv-artek-pad:~/mytest/songchong$ cat testpatch diff --git a/test b/test index 6d6cdbb..60781a8 100644 --- a/test +++ b/test @@ -1,6 +1,6 @@ - +6666666666666666666 llllllllllllllaaaa - +8888888888888888888 dsfsdkgdfkghjjh
4、如果想将多个文件的差异生成到同一patch文件 则:
git diff hash1 hash2 filename1 filename2 > patch_name 即: git diff c324747f80f9612f59dd7b575136261dd6dff01d 5dab4a955535fcc42832ce33696489d8e8b232ec test test2 > testpatch生成补丁后,到相关目录里面可以打补丁。
pathc -p1 < patch_name
其中-p1是代表忽略当前目录下的几级目录。