说你的历史是
$ git lola
* 6473d7f (master) Update
| * 9bcfa7e (HEAD, topic) Munge a, b, and c
| * 99af942 Munge b and c
| * 8383e2c Munge a and b
|/
* d1363f4 Baseline
Note: lola http://blog.kfish.org/2010/04/git-lola.html是一个非标准但有用的别名。
提交修改了三个不同的文件。
$ git log --decorate=short --pretty=oneline --name-status topic
9bcfa7e946a92c226ad50ce430a9e4ae55b32490 (HEAD, topic)
M a
M b
M c
99af942dbb922effcad8a72e96bec9ee9afcc437 Munge b and c
M b
M c
8383e2c8d6092550fec13d3c888c037b3a68af15 Munge a and b
M a
M b
d1363f4fba67d94999b269b51bdb50a8a68ba27a Baseline
A a
A b
A c
对文件的修改b
是您想要保留的,并且您想要放弃对的所有更改a
and c
。一种方法是使用git filter-branch http://schacon.github.com/git/git-filter-branch.html.
$ git checkout -b tmp topic
Switched to a new branch 'tmp'
$ git merge-base topic master
d1363f4fba67d94999b269b51bdb50a8a68ba27a
$ git filter-branch --tree-filter 'git checkout d1363f -- a c' master..tmp
Rewrite 8383e2c8d6092550fec13d3c888c037b3a68af15 (1/3)
Rewrite 99af942dbb922effcad8a72e96bec9ee9afcc437 (2/3)
Rewrite 9bcfa7e946a92c226ad50ce430a9e4ae55b32490 (3/3)
Ref 'refs/heads/tmp' was rewritten
上面的树过滤器检查指定范围内的提交并恢复文件a
and c
到“合并基础”处的内容,即提交topic
分支远离master
.
Now tmp
拥有所有topic
的更改为b
但没有更改任何其他文件。
$ git log --decorate --pretty=oneline --name-status tmp
9ee7e2bd2f380cc338b0264686bcd6f071eb1087 (HEAD, tmp) Munge a, b, and c
M b
226c22f150af1ddc1f9adc19f97fc4f220851ada Munge b and c
M b
45e706f7b22c37ee2025ee0d04c651135e7b31cd Munge a and b
M b
d1363f4fba67d94999b269b51bdb50a8a68ba27a Baseline
A a
A b
A c
作为一项安全措施,git filter-branch
存储原始参考的备份。当您对更改感到满意并想要删除备份时tmp
, run
$ git update-ref -d refs/original/refs/heads/tmp