【git】看懂git diff

git diff

可以用来比较:

1.staging area和working area的文件 (无其他参数时)

[plain]  view plain copy print ?
  1. git diff  

2.master分支和working area的文件 (用master参数)

[plain]  view plain copy print ?
  1. git diff master   


3.HEAD指向的内容和working area的文件

[plain]  view plain copy print ?
  1. git diff HEAD  


4.用远程master分支比较当前工作区

[plain]  view plain copy print ?
  1. git diff refs/remotes/origin/master  

5.经常还要用到master分支的某个文件的历史版本和working area的该文件的比较

[plain]  view plain copy print ?
  1. git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp  
上面的命令中, diff后面的参数指的是commit id, ./socket_helper.cpp是要比较的文件路径。


diff的命令输出格式注意:

[plain]  view plain copy print ?
  1. ---代表源文件  
  2. +++代表目标文件  
通常working area的文件都是被当作目标文件来看待。

-开头的行,是只出现在源文件中的行

+开头的行,是只出现在目标文件中的行

空格开头的行,是源文件和目标文件中都出现的行

差异按照差异小结进行组织,每个差异小结的第一行都是定位语句,由@@开头,@@结尾。

[plain]  view plain copy print ?
  1. chenshu@chenshu-yangzhou-home:~/kaimei/data_service/src$ git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp  
  2. diff --git a/data_service/src/socket_helper.cpp b/data_service/src/socket_helper.cpp  
  3. index d606452..047e213 100755  
  4. --- a/data_service/src/socket_helper.cpp  
  5. +++ b/data_service/src/socket_helper.cpp  
  6. @@ -4,6 +4,7 @@  
  7.  #include "data/login_response.h"  
  8.  #include "data/heartbeat_response.h"  
  9.  #include "helper/parser.h"  
  10. +#include "helper/time_measure.h"  
  11.  #include <booster/log.h>  
  12.  #include "exception/socket_error.h"  
  13.  #include "exception/data_error.h"  
上面的diff结果表明

1.某个提交记录0c5ee代表的socket_helper.cpp文件是源文件,当前working area的socket_helper文件是目标文件。

2.在源文件第4行开始的6行和目标文件第4行开始的7行构成一个差异小结

3.这个差异小结中,目标文件添加了一行#include "helper/time_measure.h"

4.其他空格开头的行表明没有差异。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值