git 进阶 (八)奇技淫巧

本文介绍了Git的三个高级用法:filter-branch用于从历史记录中彻底删除文件;blame用于追踪文件中每一行的修改来源;bisect则帮助通过二分查找定位错误的提交。通过示例详细讲解了每个命令的使用方法和应用场景。

 1.git filter-branch 

git filter-branch 常见用法就是从版本库历史记录中彻底删除文件,简单的git rm 达不到这个效果,还是可以从历史记录处中查出来。而使用git filter-branch使得删除文件看起来从来都没有在版本库中出现过一样。

git filter-branch 强烈要求所有操作都要在一个干净的目录中展开,因为其直接修改原始版本库,所以其经常被描述为破坏性的操作。

该命令会重写修改记录范围内的所有commit号。

 

举例:

在历史中已经添加了一个 test.c 文件:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git branch 
  AAA
  master
* master_dragon
  test


toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n4
commit 95c0c96a81bc51bc3d30bf2b74da9d9a346fbb2d
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:56 2019 +0800

    bb

commit 4ee7929e11dc793531a9ec18a319af9063741c46
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:30 2019 +0800

    aa

commit 09800e45c3762bd0ad04ce28e6520f7606f6557a
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:22:19 2019 +0800

    add test

commit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800

    [DBS]This is the local page



toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git show 09800e45c3762bd0ad04ce28e6520f7606f6557a
commit 09800e45c3762bd0ad04ce28e6520f7606f6557a
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:22:19 2019 +0800

    add test

diff --git a/test.c b/test.c
new file mode 100644
index 0000000..e69de29
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 


toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ ls
cli     cmes           cmoid  csmme      htmlpages  LR    msr      npapi   test.c  web
cmcpad  cmmsgctrlcust  cmrpc  defconfig  inc        mibs  msrscan  syslog  tools

使用--tree-filter彻底在历史记录中删除test.c文件: 

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git filter-branch -f --tree-filter 'rm -f test.c' master_dragon~4..master_dragon
Rewrite 95c0c96a81bc51bc3d30bf2b74da9d9a346fbb2d (4/4)
Ref 'refs/heads/master_dragon' was rewritten
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$

test.c文件被删除,但是当初这个文件提交记录中的描述还在: 

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ ls
cli  cmcpad  cmes  cmmsgctrlcust  cmoid  cmrpc  csmme  defconfig  htmlpages  inc  LR  mibs  msr  msrscan  npapi  syslog  tools  web



toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git show 2812eca62ad316381155d97ef62576182d2e2378
commit 2812eca62ad316381155d97ef62576182d2e2378
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:22:19 2019 +0800

    add test
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 

 

使用--msg-filter 彻底删除该描述:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git filter-branch -f --msg-filter 'sed -e "/test/d"' master_dragon~4..master_dragon
Rewrite 49e23a70a7dc9c39a4d3939df29ed59c1cc12938 (4/4)
Ref 'refs/heads/master_dragon' was rewritten
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n4
commit be919f95f28d1024f94731a74d0bb10eed147bf6
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:56 2019 +0800

    bb

commit ab6581da9ad7958a5fd4769db3ed4be8911ab182
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:30 2019 +0800

    aa

commit 6e5257ed5ca2fc29411201b22ad947c14f765f6e
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:22:19 2019 +0800

commit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800

    [DBS]This is the local page
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 

 

2.git blame

git blame告诉你一个文件中每一行最后是谁修改和哪次提交做出了变更:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git blame cli/defs/cmcliLa.def
122c11ca (zhangming  2014-08-21 11:45:17 +0800   1) DEFINE GROUP : LA_ALLMODE_CMDS
122c11ca (zhangming  2014-08-21 11:45:17 +0800   2) 
95b55974 (wujianning 2017-04-27 15:16:52 +0800   3)     COMMAND  : show channel-group [{channel [<integer (1-8)>] {detail | neighbor}
122c11ca (zhangming  2014-08-21 11:45:17 +0800   4)     ACTION   : 
122c11ca (zhangming  2014-08-21 11:45:17 +0800   5)                     {
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800   6)                             if($2 != NULL)
1578c510 (lingling   2015-08-14 14:39:42 +0800   7)                             {
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800   8)                     CLI_ADD("channel", (char *)$2);
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800   9) 
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800  10)                     if ($3 != NULL) { CLI_ADDN("ChannelID", $3); }
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800  11)                                     
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800  12)                                     if ($4 != NULL) { CLI_ADD("detail", (char *)$
a8e8c767 (zhangming  2015-09-17 19:44:11 +0800  13)                                     else if ($5 != NULL) { CLI_ADD("neighbor", (c
1578c510 (lingling   2015-08-14 14:39:42 +0800  14)                             }
1578c510 (lingling   2015-08-14 14:39:42 +0800  15)                             else if($6 != NULL)
1578c510 (lingling   2015-08-14 14:39:42 +0800  16)                             {
1578c510 (lingling   2015-08-14 14:39:42 +0800  17)                                     CLI_ADD("load-balance", (char *)$6);    

 

 

3.git bisect

使用git bitsect可以对提交记录进行二分查找从而确认出错提交。

比如之前代码运行正常,但是后来代码出错了,怀疑中间某一版本提交导致。可以使用该命令进行二分查找。

git bisect start //开始

git bisect bad commitN //确定当前出错版本

git bisect good commitM //确定当前正确版本

git bisect bad //确定二分出来的版本存在问题

git bisect good//确定二分出来的版本没有问题

git bisect reset//结束

 

举例:

假设这10个提交中,最后一个是出错的,但是能确定最早那个是没有问题的:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n10
commit be919f95f28d1024f94731a74d0bb10eed147bf6
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:56 2019 +0800

    bb

commit ab6581da9ad7958a5fd4769db3ed4be8911ab182
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:23:30 2019 +0800

    aa

commit 6e5257ed5ca2fc29411201b22ad947c14f765f6e
Author: chk <chk@163.com>
Date:   Mon Aug 26 20:22:19 2019 +0800

commit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800

    [DBS]This is the local page

commit 3162d0391dbabff2ba7effe252f6c8b1aabed541
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Mon Jul 24 15:44:07 2017 +0800

    [Dragon-Function-Implement]:Single fw support for 52MP(Rtk839X)

commit 846c0af7a77559d989dbc60be88facecc1ddfc3c
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Thu Jul 27 14:05:21 2017 +0800

    Dragon-fix: FW upgrade will cause crash

commit d8a0e2cd3c9cd3e32588cdccc62ebc002cf79462
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Thu Jul 20 18:33:37 2017 +0800

    [Dragon-Fuction-Implement]:Support Single fw for 28P/10MP

commit 11316b05d9eb14d0be27cb5efb8e0ef3120fce95
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Tue Jul 18 14:06:06 2017 +0800

    [Dragon-SDK-Shell]optimise sdk diag shell

commit bc54a0d3e59848d3622112709f74e0b5427508e3
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Fri Jul 7 14:45:41 2017 +0800

    Dragon-fix: Fix the save configuration failure issue

commit 73fd84da808c0d9d3c9213fd605620730710a1fa
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Mon Jul 3 15:14:58 2017 +0800

    Dragon-DNS: Implement the DNS module and SNTP support DNS
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 

开始二分搜索:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect start 

 

确定错误和正确commit,自动reset到两者中间commit提交号:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect start 
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect bad be919f95f28d1024f94731a74d0bb10eed147bf6
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect good 73fd84da808c0d9d3c9213fd605620730710a1fa
Bisecting: 4 revisions left to test after this (roughly 2 steps)
[846c0af7a77559d989dbc60be88facecc1ddfc3c] Dragon-fix: FW upgrade will cause crash
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n2
commit 846c0af7a77559d989dbc60be88facecc1ddfc3c
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Thu Jul 27 14:05:21 2017 +0800

    Dragon-fix: FW upgrade will cause crash

commit d8a0e2cd3c9cd3e32588cdccc62ebc002cf79462
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Thu Jul 20 18:33:37 2017 +0800

    [Dragon-Fuction-Implement]:Support Single fw for 28P/10MP
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 


假如经过测试后,该版本没有问题,则自动reset到该版本和出错版本的中间commit:
 

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect good 
Bisecting: 2 revisions left to test after this (roughly 1 step)
[42947bf9888c01669ed3541ddb7d585502f38bc8] [DBS]This is the local page
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n1
commit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800

    [DBS]This is the local page
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 

 

又经过测试,当前版本有问题,则继续,直到找到导致的出错版本:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect bad 
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[3162d0391dbabff2ba7effe252f6c8b1aabed541] [Dragon-Function-Implement]:Single fw support for 52MP(Rtk839X)


toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect bad 
3162d0391dbabff2ba7effe252f6c8b1aabed541 is the first bad commit
commit 3162d0391dbabff2ba7effe252f6c8b1aabed541
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Mon Jul 24 15:44:07 2017 +0800

    [Dragon-Function-Implement]:Single fw support for 52MP(Rtk839X)

:040000 040000 1de772c456c6b7634d696b7e3df9a5cd732421c3 c1966da67304489fc488d7d458c68ed09987c496 M      npapi

二分结束,注意在二分的过程中是在分离的HEAD中进行的:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git branch 
* (no branch, bisect started on master_dragon)
  AAA
  master
  master_dragon
  test


toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git bisect reset 
Previous HEAD position was 3162d03... [Dragon-Function-Implement]:Single fw support for 52MP(Rtk839X)
Switched to branch 'master_dragon'
Your branch is ahead of 'origin/master_dragon' by 3 commits.
  (use "git push" to publish your local commits)


toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git branch 
  AAA
  master
* master_dragon
  test
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值