将变量传递给git filter-branch中的命令(通过删除旧的二进制文件来减少repo大小)

yoo*_*iba 4 git macos shell github

我正在研究通过删除二进制文件来减少repo大小的方法.我正在编写脚本来为我做这个,基于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
Run Code Online (Sandbox Code Playgroud)

这给了我输出

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
Run Code Online (Sandbox Code Playgroud)

但是当我尝试对此进行参数化时,如下所示,整个过程不起作用

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
Run Code Online (Sandbox Code Playgroud)

产量

Rewrite 0491bf3461726c2ebd595ebc480010b5bf722302 (1/10)fatal: '/About Administration Tools.app' is outside repository
index filter failed: git rm --cached --ignore-unmatch /*${fileOLD} 
Run Code Online (Sandbox Code Playgroud)

它不会删除文件.

问题是如何在将变量传递给嵌套的git命令时正确传递/转义变量?(MAC OS x 10.7.2)

小智 5

问题是您在git过滤器中使用的单引号(').Bash不会替换单引号内的变量.

以下应该工作(未经测试):

fileOLD=selenium-server-standalone-2.16.0.jar
git filter-branch --index-filter "git rm --cached --ignore-unmatch /*${fileOLD}" --prune-empty -- --all
Run Code Online (Sandbox Code Playgroud)