我正在研究通过删除二进制文件来减少存储库大小的方法。我正在编写脚本,它会根据 http://help.github.com/remove-sensitive-data/ 为我做这件事。
似乎我的解决方案有效,但不知何故我无法将参数传递给嵌套的 git 命令。 带有硬编码文件的脚本有效:
git filter-branch --index-filter 'git rm --cached --ignore-unmatch /*selenium-server-standalone-2.16.0.jar' --prune-empty -- --all
git push origin master --force
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git status
git pull
这给了我输出
Rewrite f9a33e41dd6da4630773272ec18d194d14935a83 (10/10)rm 'bin/selenium-server-standalone-2.16.0.jar'
rm 'selenium-server-standalone-2.16.0.jar'
Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
WARNING: Ref 'refs/remotes/origin/master' is unchanged
但是当我尝试对其进行参数化时,如下所示,整个过程不起作用
fileOLD=selenium-server-standalone-2.16.0.jar
git filter-branch --index-filter 'git rm --cached --ignore-unmatch /*${fileOLD}' --prune-empty -- --all
git push origin master --force
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git status
git pull
输出
Rewrite 0491bf3461726c2ebd595ebc480010b5bf722302 (1/10)fatal: '/About Administration Tools.app' is outside repository
index filter failed: git rm --cached --ignore-unmatch /*${fileOLD}
它不会删除文件。
问题是在将变量传递给嵌套的 git 命令时如何正确传递/转义变量? (MAC 操作系统 x 10.7.2)
最佳答案
问题是您在 git 过滤器中使用的单引号 (')。 Bash 不会替换单引号内的变量。
以下应该有效(未经测试):
fileOLD=selenium-server-standalone-2.16.0.jar
git filter-branch --index-filter "git rm --cached --ignore-unmatch /*${fileOLD}" --prune-empty -- --all
关于macos - 将变量传递给 git filter-branch 中的命令(通过删除旧二进制文件来减少存储库大小时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8943408/