Search code examples
gitgit-filter-branch

Git filter-branch only on forked commits


I forked another repository, and then added a number of files to it. I occasionally merge in changes from the original repository to stay up-to-date.

I realized I have some files in my fork which should be removed, so I am trying to follow [1] to remove some files from my git repository. The source repo has thousands of commits, while I have a few hundred.

When I execute the command, it tries to search all the source commits as well, not just my fork commits, which would take hours instead of minutes.

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch filename.txt' --prune-empty @

There are numerous merges from upstream in between.

master         A---B---C---D---E---F---G (HEAD)
                  /       /
upstream/master  H---I---J---K

[1] https://help.github.com/en/github/authenticating-to-github/removing-sensitive-data-from-a-repository


Solution

  • I haven't looked at the filter-repo command yet, but you can feed filter-branch the exact list of commits you want to examine, everything after a -- arg gets fed to the rev-list filter-branch runs to generate its candidates, e.g.

    git filter-branch --index-filter "$myfilter" -- --first-parent HEAD