我可以在 Git 中删除分支后恢复分支吗? [英] Can I recover a branch after its deletion in Git?
问题描述
如果我运行 git branch -d XYZ
,有没有办法恢复分支?有没有办法像我没有运行删除分支命令一样返回?
If I run git branch -d XYZ
, is there a way to recover the branch? Is there a way to go back as if I didn't run the delete branch command?
推荐答案
是的,您应该能够执行 git reflog --no-abbrev
并在提示处找到提交的 SHA1您删除的分支,然后只需 git checkout [sha]
.一旦你完成了那个提交,你就可以 git checkout -b [branchname]
从那里重新创建分支.
Yes, you should be able to do git reflog --no-abbrev
and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha]
. And once you're at that commit, you can just git checkout -b [branchname]
to recreate the branch from there.
感谢@Cascabel 提供了此压缩/单行版本,感谢@Snowcrash 提供了如何获取 sha.
如果你刚刚删除了分支,你会在终端中看到类似这样的内容 Deleted branch
.然后在这个单行中使用
:
If you've just deleted the branch you'll see something like this in your terminal Deleted branch <your-branch> (was <sha>)
. Then just use that <sha>
in this one-liner:
git checkout -b <your-branch> <sha>
这篇关于我可以在 Git 中删除分支后恢复分支吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!