Ztyx
Ztyx

Reputation: 14908

Remove history of a file from just a single branch, not the entire repo

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:

Upvotes: 5

Views: 582

Answers (3)

Ztyx
Ztyx

Reputation: 14908

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.

Upvotes: 0

Ztyx
Ztyx

Reputation: 14908

Using git filter-branch:

$ git checkout topic-branch
$ git filter-branch --index-filter 'git checkout master -- myfile' master..HEAD

Upvotes: 5

jwg
jwg

Reputation: 5827

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.

Upvotes: 0

Related Questions