2016-02-28 66 views
1

我不小心将一个巨大的文件添加到我的回购(〜250 MB),名称为--exclude。这个文件是在我为这个回购编写一些新代码时在OS X上生成的。现在,我试图学习如何从Git的历史记录herehere中删除它。教程很好,很清楚,但问题是,Git“认为”--exclude不是一个文件,而是一个选项。Git:删除历史记录中名为类似CLI选项的文件

当我运行此命令:

git filter-branch \ 
    --index-filter "git rm -r --cached --ignore-unmatch '--exclude' " HEAD

Git的抱怨:

error: unknown option `exclude' 
usage: git rm [<options>] [--] <file>... 

我试图用文件的SHA数量,而是改变nothing.What是删除这样的文件的正确方法是什么?

+1

你可以尝试使用通配符吗?不确定什么git接受,但也许'*排除'? –

+0

@ LasseV.Karlsen是的,你可以。好主意!看[我的回答](http://stackoverflow.com/a/35681754/674064)。 –

回答

3

构建说法线常见的Unix方式是

<command> <keys&arguments> [--] <arguments> 

在你平时的密钥由领先-小号检测情况

command: git rm 
keys: -r --cached --ignore-unmatch 
arguments: '--exclude' 

但是你可以强制使用-- 所以,你得到

git filter-branch --index-filter "git rm -r --cached --ignore-unmatch -- '--exclude' " HEAD 
+1

请注意,这个答案被称为“键”的东西通常也被称为“选项”,键/选项也是参数,所以这个答案称为“参数”的东西有时被称为“非选项参数”。进一步注意(大多数其他UNIX命令除外)大多数(可能全部)git子命令,你不能在'--'分隔符之后放置任意的非选项参数,而只能放置文件和目录。 –

3

尝试至少从文件参数分离git rm选项,使用“--”(双连字符)

git filter-branch --index-filter "git rm -r --cached --ignore-unmatch -- '--exclude' " HEAD 
                     ^^ 
                   (double-hyphen) 

查看更多关于与双连字符语法“Deleting a badly named git branch”。

2

推荐的方式做到这一点是使用--分离器在该行的参数部分明确地写他们将其移动到参数as seen in the answers by VonCand Lol4t0并在其后放置文件名称。

但是使用通配符来避免--exclude--…部分,因为Lasse suggests in a comment的确可以工作,因为Git似乎自己做通配符扩展,而与shell无关。

如果你只是在一个单一的,手动git rm使用它,你必须要么

  • 逃避它:

    git rm -r --cached --ignore-unmatch \*exclude 
    

  • 引号,

    git rm -r --cached --ignore-unmatch '*exclude' 
    

  • 删除实际文件第一

    rm ./--exclude 
    git rm -r --cached --ignore-unmatch *exclude 
    

这样shell已经不进行扩展和通配符传递给所有饭桶。但在

git filter-branch \ 
    --index-filter "git rm -r --cached --ignore-unmatch *exclude" HEAD 

整个内部命令反正引用,并git filter-branch似乎并没有调用shell来解释它,所以这个作品原样。 (使用嵌套命令可能是Git完全实现自己的通配符扩展的原因。)

相关问题