以下是您能使用 git filter-branch 命令来删除所有不需要的文件的方法:
下面是利用 git filter-branch 命令来移除不需要的所有文件的步骤:
Get a list of the filenames that you don't want to appear in the history both the old names and the new names in case of renames. For example put them in a file called toberemoved.txt
Run git filter-branch like this:
$ git filter-branch --tree-filter "rm -f `cat toberemoved.txt`" branch1 branch2 ...
这是来自git filter-branch的相关手册页面:
过滤器功能
--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 log --raw |awk '/^:/ { if (! printed[$6]) { print $6; printed[$6] = 1 }}'|while read f;do if [ ! -f $f ]; then echo Deleted: $f;fi;done
在git log的--raw模式下,$6是提交中受影响的文件名。
如果您想知道每个提交对每个文件发生了什么([D]eleted,[R]enamed,[M]odified等),请参阅git log的--diff-filter选项。
也许其他人可以帮忙找出已跟踪文件的先前名称,以防重命名。