gpt4 book ai didi

git - 在没有 git rm 失败的情况下使用 git filter-branch 删除不需要的文件的正确方法

转载 作者:太空狗 更新时间:2023-10-29 13:16:47 25 4
gpt4 key购买 nike

我有一个 SNMP 代理项目,其中相关的 MIB 文件(*.smiv2 文件)是与其一起开发的,但现在我希望它们位于单独的 git 存储库中。

为了不丢失任何 MIB 文件历史记录,因为它们不是从现在所在的同一目录开始的,我不能只使用 --subdirectory-filter filter-branch ,所以我尝试了 --filter-index 方法,基于 this question .这个想法是删除所有不以 .smiv2 结尾的文件(显然是在原始项目的新克隆上,在过程结束时应将其推送到我的新 MIB 存储库) .

为了让它更简单,我选择使用 ls-files 而不是 ls-tree,所以不是:

git filter-branch --prune-empty --index-filter 'git ls-tree -r --name-only \
--full-tree $GIT_COMMIT | grep -v ".smiv2$" | xargs git rm --cached \
--ignore-unmatch -r'

我用过这个:

git filter-branch --prune-empty --index-filter 'git ls-files | \
grep -v ".smiv2$" | xargs git rm --cached --ignore-unmatch'

但其中任何一个在第一次提交时都失败了,因为看起来 git rm 根本没有提供任何参数(我想 --ignore-unmatch 将工作正常,如果未找到提供的参数,但在没有提供参数的情况下不会):

$ git filter-branch --prune-empty --index-filter 'git ls-files | \
> grep -v ".smiv2$" | xargs git rm --cached --ignore-unmatch'
Rewrite 4cd2f1c98dbaa96bc103ae81fbd405bd1d991d9a (1/418)usage: git rm [options] [--] <file>...

-n, --dry-run dry run
-q, --quiet do not list removed files
--cached only remove from the index
-f, --force override the up-to-date check
-r allow recursive removal
--ignore-unmatch exit with a zero status even if nothing matched

index filter failed: git ls-files | \
grep -v ".smiv2$" | xargs git rm --cached --ignore-unmatch

我让它在脚本中包装 git rm ,即使由于缺少参数而失败时也会返回成功(将其保存在 /usr/local/bin/gitrm_alt):

#!/bin/sh
git rm --cached --ignore-unmatch "$@"
exit 0

然后调用它而不是 git rm:

git filter-branch --prune-empty --index-filter 'git ls-files | \
grep -v ".smiv2$" | xargs gitrm_alt'

但我发现那非常丑陋和笨拙,所以我想问一下是否有更直接/正确的方法来做到这一点。

最佳答案

最简单的解决方案是向 git rm 添加一个虚拟参数,以便它始终至少有一个文件参数。

例如

... | xargs git rm --cached --ignore-unmatch DoesNotExistInMyProject

关于git - 在没有 git rm 失败的情况下使用 git filter-branch 删除不需要的文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12179611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com