为什么filter-branch上的--cached选项从工作目录中删除文件? [英] Why did --cached option on filter-branch remove files from working directory?
问题描述
我需要从旧的回购中删除一些本应被忽略的Xcode文件。所以我运行了下面的命令:
git filter-branch --index-filter'git rm -f --cached --ignore -unmatch * mode1v3 * pbxuser'HEAD
我的理解是,加入--cached不会影响当前工作目录,但git也删除了这些匹配的文件。幸运的是我有一个备份(!),但我很好奇它为什么会这样做,或者我误解了 - cached
的作用?
罪魁祸首不是 git rm
命令。它的 - cached
选项的确如你所说的那样工作。你可以在一个小的git仓库中轻松地尝试。
虽然手册页没有提到它,但是 git filter-branch
似乎不能保存你的工作区域。实际上,如果你的工作区域不干净,命令就会拒绝运行,这已经是一个迹象。
但即使文件已经从工作区域中删除,它们也不是从回购去了。他们不再处于您当前分支中的任何提交。但是在重写引用名称空间refs / original /之前,过滤分支商店会引用您的分支。
使用命令 git show-ref
查看它。
您可以检出旧版本以存取您移除的档案。您可以使用命令
git cat-file blob refs / original / refs / heads / master:foo
来获取文件内容而不检出(使用由show-ref显示的引用,foo是所需文件的名称)。有很多可能性
您可以使用 gitk --all
浏览您的重写分行和当前分行你会发现没有什么东西真的没了。
I needed to remove some Xcode files from an old repo that should have been ignored. So I ran the following command
git filter-branch --index-filter 'git rm -f --cached --ignore-unmatch *mode1v3 *pbxuser' HEAD
My understanding was that adding --cached would not affect the current working directory, but git deleted those matching files too. Luckily I had a backup(!) but I'm curious why it does this, or am I misunderstanding what --cached
does?
The culprit is not the git rm
command. Its --cached
option works indeed as you say. You can easily try that in a small git repo.
Although the man page does not mention it, git filter-branch
does not seem to preserve your working area. Actually the command refuses to run if your working area is not clean, which is an indication already.
But even if the files are gone from the working area, they are not gone from the repo. They are just no longer in any commit reachable in your current branch. But filter-branch stores are reference to your branch before rewriting to reference name space refs/original/.
Use command git show-ref
to see it.
You could check out the old version to access your removed files. You could use command
git cat-file blob refs/original/refs/heads/master:foo
to get the contents of the file without checking out (use the reference shown by show-ref, foo is the name of the desired file). There are plenty of possibilities
You can use gitk --all
to navigate through both your rewritten and your current branches and you will see that nothing is really gone.
这篇关于为什么filter-branch上的--cached选项从工作目录中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!