5

I have a topic branch (off master) has that lots of changes. Throughout the history of this topic branch there's been numerous changes to a certain file. How can remove all changes to this file throughout the history of my topic branch?

Additional notes:

  • Note that the file existed before the topic branch was created. I don't want to delete the file per se.
  • I have a couple of solutions, but so far they seem tedious. I'll post them as separate answers.
  • Can git filter-branch somehow be used for this?
  • My topic branch contains around...say...60 commits.
Ztyx
  • 14,100
  • 15
  • 78
  • 114
  • Do you want to remove the changes from history, or is it ok, if the changes are dropped when merging to master? – knittl Sep 25 '13 at 09:14
  • `git filter-branch` can definitely do it. What you want is: for each new commit, if that file was modified, un-modify it by re-loading the desired version. If the file was not modified, you can still read the desired version, since that will be a no-op. I think this can be done with `--index-filter` and `git update-index`. (I'd have to experiment to make it work, myself.) Edit: or, yes, `git checkout -- `, as in your own answer :-) – torek Sep 25 '13 at 09:24
  • knittl: I want the changes removed from history. – Ztyx Sep 25 '13 at 21:52
  • On a side note, 60 commits is a lot for a feature branch. From a dev process standpoint, the scope of your features may be too large. Either that, or you should consider squashing your commits in the feature branch more. –  Apr 05 '14 at 01:04
  • Cupcake: Indeed. This was the result of a summer intern that we failed to communicate early merging to. – Ztyx Apr 07 '14 at 14:24

3 Answers3

5

Using git filter-branch:

$ git checkout topic-branch
$ git filter-branch --index-filter 'git checkout master -- myfile' master..HEAD
Ztyx
  • 14,100
  • 15
  • 78
  • 114
0

Rebase the tree against the start of the branch. Use attributes to determine a different merge strategy for that particular file, in particular 'merge.ours' (always keep our file). This thread explains how to: tell git to use ours merge strategy on specific files (the question itself tells you how to do it, the answer is simply a gotcha that the questioner had).

I don't know how 'tedious' this is, eg whether you have to confirm something at each commit, because I haven't done it. But I would think not.

Community
  • 1
  • 1
jwg
  • 5,547
  • 3
  • 43
  • 57
0

Do an interactive rebase. The tedious version would be to simply edit/modify every commit removing the change:

git checkout topic-branch
git rebase -i master    
# set all commits to edit

# for every commit; do
git show --stat
# if myfile is not changes: git rebase --continue

# copy the commit message
git reset HEAD^
git reset myfile
git checkout myfile
git commit
# paste in commit message

git rebase --continue
# ...until we are done

The slightly improved version is to use git log master.. myfile and only edit the commits listed.

Ztyx
  • 14,100
  • 15
  • 78
  • 114