git - Git filter-branch 仅适用于分叉提交
问题描述
我分叉了另一个存储库,然后向其中添加了许多文件。我偶尔会合并原始存储库中的更改以保持最新。
我意识到我的 fork 中有一些文件应该被删除,所以我试图按照 [1] 从我的 git 存储库中删除一些文件。源代码库有数千个提交,而我有几百个。
当我执行命令时,它也会尝试搜索所有源提交,而不仅仅是我的 fork 提交,这将花费数小时而不是数分钟。
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch filename.txt' --prune-empty @
两者之间有许多来自上游的合并。
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
解决方案
我建议使用git filter-repo
替换BFG 和git filter-branch
.
注意:如果您在运行上述命令时收到以下错误消息:
Error: need a version of `git` whose `diff-tree` command has the `--combined-all-paths` option`
这意味着你必须更新git
。
请参阅“基于路径的过滤”:
git filter-repo --path file-to-remove --invert-paths
您可以将其与 ref 过滤结合使用:首先将原始 repo(您已分叉的)的 URL 添加到本地 repo:
cd /path/to/local/clone/of/my/fork
git remote add upstream /url/original/repo
git fetch upstream
这样,您可以将过滤限制为仅您的 fork 提交。
这是一个示例,假设您已在upstream/master
.
git filter-repo --path file-to-remove --invert-paths \
--refs upstream/master..<myBranch>
推荐阅读
- image - 如何创建一个根据某些图像大小进行自我调整的小部件?
- react-native - 没有在formik中用reactnative调用handleSubmit?
- kubernetes - 证书经理。发生内部错误:调用 webhook 失败。错误的证书管理器挂钩 ID
- c# - MVC 控制器名称在 URL 中出现两次
- python - 替代 `keras.backend.rnn` 以将 TensorFlow SavedModel 转换为 tensorflow.js 图模型
- php - 在 debian docker 上安装 php7.3-intl 时出现错误“没有安装候选者”
- xamarin - 运行 Xamarin.Forms 时,是否需要在共享、iOS 和 Android 项目中或仅在共享中添加 appcenter 的启动?
- c# - 无法获取 OneDrive 文件夹的重新分析点信息
- php - 用php分割子串
- sql - 是否可以将子查询值用作 SELECT FIELD 和 FROM?