简体   繁体   中英

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

I woyuld recommend to use the new tool git filter-repo which replaces BFG and git filter-branch .

Note: if you get the following error message when running the above-mentioned commands:

Error: need a version of `git` whose `diff-tree` command has the `--combined-all-paths` option`

it means you have to update git .


See " Path based filtering ":

git filter-repo --path file-to-remove --invert-paths

You can combine it with ref filtering: add first the URL of the original repo (that you have forked) to your local repo:

cd /path/to/local/clone/of/my/fork
git remote add upstream /url/original/repo
git fetch upstream

That way, you can limit the filtering to only your fork commits.
Here is an example assuming you have added commits on top of upstream/master .

git filter-repo --path file-to-remove --invert-paths \
  --refs upstream/master..<myBranch>

我还没有看过filter-repo命令,但是您可以将要检查的提交的确切列表提供给filter-branch-- arg 之后的所有内容都被提供给rev-list filter-branch运行以生成其候选人,例如

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM