git filter-branch尊重我的最初提交.gitignore [英] git filter-branch that respects my initial commits .gitignore

查看:65
本文介绍了git filter-branch尊重我的最初提交.gitignore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用git filter-branch并让我的由.gitignore组成的初始提交(此后将重新基于分支)生效.

I am trying to use git filter-branch and to let my intial commit consisting of a .gitignore (rebased into the branch afterwards) come into effect.

这是我的跑步方式:

git filter-branch -f --tree-filter 'git rm -r --cached . && git add .' --prune-empty

但是此命令使存储库保持不变.

This command however leaves the repository unchanged.

为什么?

我还想知道为什么以下命令不能删除存储库中的所有文件:

I also wonder why the following command does not remove all files in the repository:

git filter-branch -f --tree-filter 'git rm -r --cached .'

推荐答案

--tree-filter不能对索引进行操作. 1 而是将每个提交复制到一个临时目录,该目录用作工作树,在该临时目录中运行命令,并根据临时目录中所有剩余文件和/或新文件进行新提交.在此过程中,您对索引本身所做的更改将被有效忽略.

A --tree-filter does not operate on/with the index.1 Instead, it copies each commit to a temporary directory used as a work-tree, runs your command in that temporary directory, and makes the new commit out of whatever files remain and/or are new in the temporary directory. Changes you make to the index itself during this process are effectively ignored.

这意味着您必须从工作树中完全删除那些阻止您添加新.gitignore的文件.您可以在--index-filter中的git rm --cached中手动执行此操作,这会更快,但是,如果您想让git在--tree-filter中为您完成工作,则可以使用git本身使用的相同技巧(请参阅脚注):使用git clean(带有-x)清理临时目录.

What this means is that you must remove from the work-tree exactly those files your new .gitignore prevented from being added. You could do this manually with git rm --cached in an --index-filter, which would be much faster, but if you want to get git to do the work for you in a --tree-filter, you can use the same trick git itself uses (see the footnote): use git clean (with -x) to clean up the temporary directory.

1 这不是很正确:filter-branch--tree-filter代码使用git read-tree来填充索引,然后(重新)填充临时目录(这又意味着它必须做一些工作以清除临时目录从以前的提交,为此它使用git clean).然后,在eval过滤器之后,它再次使用索引来检查临时目录中的操作.当您分别按git rm -r --cached .git add .时,它将比较不再拥有被忽略文件的结果索引与工作树,并... 添加被忽略文件.啊!

1This is not quite true: filter-branch's --tree-filter code uses git read-tree to populate the index before (re)populating the temporary directory (which in turn means it has to do some work to clean out the temporary directory from previous commits, for which it uses git clean). Then, after evaling your filter, it uses the index again to inspect what was done in the temporary directory. When you git rm -r --cached . and then git add ., it compares the resulting index, which no longer has your ignored files, to the work-tree and ... adds your ignored files. Argh!

这篇关于git filter-branch尊重我的最初提交.gitignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆