2012-12-21 40 views
14

Git回购,有一个文件夹ABC有三个文件,所有名称中都有空格file - 1.ext,file - 2.extfile - 3.ext。我想删除文件夹和文件。我只知道如何删除这些文件通过这个命令删除所有以前提交的目录

git filter-branch --index-filter 'git rm --cached --ignore-unmatch FILE' \--prune-empty --tag-name-filter cat -- --all

那么你git push origin master --force

(1)但是,如何删除文件夹ABC及其所有以前提交的文件?

此外,文件的文件名中的空间和下面没有检测他们在回购承诺:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch file\ -\ 1.ext' \--prune-empty --tag-name-filter cat -- --all 

(2)什么都要语法是名称中带有空格删除文件? 注

  • OSX
  • github上
+2

为什么你希望他们从所有提交中删除?提交是您的项目的快照。当你想删除一些目录或文件时,你应该创建一个新的提交,从你的项目中删除它们。 –

+12

@WouterJ有时候您可能想从存储库的历史记录中删除无用的大文件,或者删除包含敏感信息的文件。 –

回答

20

说出你的

$ git lola --name-status 
* e709131 (HEAD, master) bar 
| A  bar 
* 61493ac ABC/file - 3.ext 
| A  ABC/file - 3.ext 
* 34cce9e ABC/file - 2.ext 
| A  ABC/file - 2.ext 
* 115e6d5 ABC/file - 1.ext 
| A  ABC/file - 1.ext 
* 5ea5b42 foo 
    A  foo

注意历史开始:git lola是一个非标准的,但非常有用的别名。

git rm支持删除子树的选项。

-r
允许递归删除时被赋予了领先的目录名。

运行

$ git filter-branch --index-filter 'git rm --cached -r --ignore-unmatch ABC' \ 
    --prune-empty --tag-name-filter cat -- --all

,你会看到类似于

Rewrite 115e6d5cd06565ca08f1e5c98c4b91246cf59fa1 (2/5)rm 'ABC/file - 1.ext' 
Rewrite 34cce9e90f832460137e620ebacc8a73a99e64ce (3/5)rm 'ABC/file - 1.ext' 
rm 'ABC/file - 2.ext' 
Rewrite 61493ac3211808f34f616dbc33d51d193b3f45a3 (4/5)rm 'ABC/file - 1.ext' 
rm 'ABC/file - 2.ext' 
rm 'ABC/file - 3.ext' 
Rewrite e709131f1fe6103adf37616c9fa500994aeb30d0 (5/5)rm 'ABC/file - 1.ext' 
rm 'ABC/file - 2.ext' 
rm 'ABC/file - 3.ext' 

Ref 'refs/heads/master' was rewritten

如果你很高兴与结果输出后,删除旧的主用

$ git update-ref -d refs/original/refs/heads/master

现在你有一个

的历史
$ git lola --name-status 
* 19680d4 (HEAD, master) bar 
| A  bar 
* 5ea5b42 foo 
    A  foo

要回答你的第二个问题,假设你只想删除ABC/file - 2.ext。请记住,您将需要两层引用:一层用于整个命令,另一层用于转义该命令的参数中的空格,即要删除的文件的名称。要做到这一点

一种方法是

$ git filter-branch --index-filter \ 
    'git rm --cached --ignore-unmatch "ABC/file - 2.ext"' --prune-empty \ 
    --tag-name-filter cat -- --all

注单引号里面的双引号。

如果你在运行这个命令来代替,你的历史将成为

$ git lola 
* a8d1b0d (HEAD, master) bar 
* cff0c4e ABC/file - 3.ext 
* 115e6d5 ABC/file - 1.ext 
* 5ea5b42 foo
11

而不是使用--index-filter的,尝试用--tree-filter

--tree-filter <command> 
    This is the filter for rewriting the tree and its contents. The argument is 
    evaluated in shell with the working directory set to the root of the 
    checked out tree. The new tree is then used as-is 
    (new files are auto-added, disappeared files are auto-removed - 
    neither .gitignore files nor any other ignore rules HAVE ANY EFFECT!). 

的命令行:

git filter-branch --tree-filter 'rm -rf path/to/ABC' \ 
    --prune-empty --tag-name-filter cat -- --all