撤消 git 过滤器分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14542326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:21:07  来源:igfitidea点击:

Undo git filter-branch

git

提问by Chin

I accidentally delete a file from my repo using git filter-branch:

我不小心使用 git filter-branch 从我的仓库中删除了一个文件:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch images/thumb/a.JPG' HEAD

How can I undo this? Is it possible? i.e. is the file permanently deleted?

我怎样才能撤消这个?是否可以?即文件是否被永久删除?

回答by William Seiti Mizuta

When you use git filter-branch, a backup file is created in

使用时git filter-branch,会在

refs/original/refs/heads/master

If you used the command in branch master. You can check if you have the backup in .git/refsdirectory. With this in mind, you can use this backup to recover your files with:

如果您在 branch 中使用了该命令master。您可以检查.git/refs目录中是否有备份。考虑到这一点,您可以使用此备份来恢复您的文件:

git reset --hard refs/original/refs/heads/master

回答by Eldar Abusalimov

Probably a more proper way than just doing hard reset to the original master would be to restore allrefs rewritten by git filter-branch, and maybe even delete backup refs afterwards in order to be able to invoke git filter-branchagain without --force:

可能比仅对原始主服务器进行硬重置更合适的方法是恢复由 重写的所有引用git filter-branch,甚至可能在事后删除备份引用,以便能够在git filter-branch没有 的情况下再次调用--force

for orig_ref in $(git for-each-ref --format="%(refname)" refs/original/); do
    git update-ref "${orig_ref#refs/original/}" $orig_ref
    git update-ref -d $orig_ref  # to also remove backup refs
done

And after that:

在那之后:

git reset --hard master

UPD.

更新。

Here's (arguably) a bit more git'ish way to perform the same without a shell for-loop:

这是(可以说)在没有 shell for 循环的情况下执行相同操作的更git'ish 方式:

git for-each-ref --format="update %(refname:lstrip=2) %(objectname)" refs/original/ | git update-ref --stdin
git for-each-ref --format="delete %(refname) %(objectname)" refs/original/ | git update-ref --stdin

回答by Mr_and_Mrs_D

A much cleaner solution is given in this answerby @jthill.

@jthill在这个答案中给出了一个更清晰的解决方案。

git fetch . +refs/original/*:*

As noted in that answer you may need to detach the HEAD if the currently checked out branch is to be restored.

如该答案所述,如果要恢复当前签出的分支,您可能需要分离 HEAD。

To delete the refs/originalrefs, issue:

要删除refs/original引用,请发出:

git for-each-ref refs/original --format='delete %(refname) %(objectname)' | git update-ref --stdin

回答by che

It's possible that your old branch tip will be preserved in your reflog. Then you should be able to check out the unchanged commit with all the previous history.

您的旧分支提示可能会保留在您的reflog 中。然后,您应该能够使用所有以前的历史记录检查未更改的提交。