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