Git: best way to remove all changes from a given file for one branch

感情迁移 提交于 2019-12-03 02:29:15

Say your history is

$ 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 is a non-standard but helpful alias.

The commits have modified three different files.

$ 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

The changes to file b are the ones you want to keep, and you want to discard all changes to a and c. One way to do this is with git filter-branch.

$ 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

The tree filter above checks out the commits in the named range and restores files a and c to the content at the “merge base,” that is, the commit at which topic branched away from master.

Now tmp has all topic’s changes to b but no changes to any other file.

$ 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

As a safety measure, git filter-branch stores a backup of your original ref. When you’re satisfied with your changes and want to delete the backup tmp, run

$ git update-ref -d refs/original/refs/heads/tmp

To checkout to a specific version you can do git checkout <sha1> <file> where sha1 is the unique sha1 hash of that version you want the file to be in.

I ran into a couple of problems as Windows user:

  1. Use Double Quotes for --tree-filter command.
  2. Use forward slashes for filepath.
  3. Dont start filepath with forward slash.

Here is how my command ended up looking:

git filter-branch -f --tree-filter "git checkout a59f75 -- source/_posts/blog1.md source/_posts/blog2.md" master..tmp

You could make your changes to remove functionality as a new commit, then create a patch from it. Do your rebasing/squashing or whatever you need to to prepare to merge back to master, then when you want to add the new functionality back in, you can apply the patch in reverse mode with

git apply -R

Note I've never done this... but it should work in theory I think. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!