如何在 git 中恢复已删除的分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5543280/
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
How do I get the deleted-branch back in git?
提问by Edison Chuang
I'm trying to use git for our software development. I found that if I delete a branch in git, I could lose my code forever. That surprised me. I thought as a version control system, it should be able to let me do anything (even stupid one) without worry about harming my work.
我正在尝试使用 git 进行我们的软件开发。我发现如果我在 git 中删除一个分支,我可能会永远丢失我的代码。这让我很惊讶。我认为作为一个版本控制系统,它应该能够让我做任何事情(甚至是愚蠢的)而不必担心损害我的工作。
The steps below are what I did:
下面的步骤是我所做的:
- Initialize a git repository.
- commit several changes to repository.
- switch/checkout current working directory to first commit.
- delete master branch.
- then I lost all of my works and I can't believe what I saw. Is it real? If I am drunk while coding. I could lose the code in git.
- 初始化一个 git 存储库。
- 向存储库提交一些更改。
- 切换/检出当前工作目录以首次提交。
- 删除主分支。
- 然后我失去了我所有的作品,我无法相信我所看到的。这是真的吗?如果我在编码时喝醉了。我可能会丢失 git 中的代码。
The question is how can I roll back the action of deleting a branch? Or, How can I get all history in git, even something which has disappeared in log?
问题是如何回滚删除分支的动作?或者,我怎样才能在 git 中获取所有历史记录,甚至是在日志中消失的东西?
回答by VonC
To avoid the issue in the first place, Jefromiadvices in the comments:
为了首先避免这个问题,Jefromi在评论中提出建议:
Another tip: only use
git branch -d
, notgit branch -D
.
You'll be warned if you're about to delete something that might make you lose work, then you can take a second to think before using-D
.
(Or, you can go delete fromgitk
so you really see what you're deleting.)
另一个提示:只使用
git branch -d
,而不使用git branch -D
。
如果您要删除可能使您失去工作的内容,您会收到警告,然后您可以在使用-D
.
(或者,您可以从中删除,gitk
以便您真正了解要删除的内容。)
-d
Delete a branch.
The branch must be fully merged in its upstream branch, or inHEAD
if no upstream was set with--track
or--set-upstream
.
删除一个分支。
该分支必须在其上游分支中完全合并,或者HEAD
如果没有使用--track
或设置上游分支--set-upstream
。
But if you did "lose" your work, see one of the many blogsabout reflog(as James Kyburzsuggests in the comments):
但是,如果您确实“丢失”了您的工作,请参阅有关 reflog的众多博客之一(正如James Kyburz在评论中所建议的那样):
back to list Git reflog to the rescue September 09, 2010 — written by Chris Sloan | 0 comments ?
返回列表 Git reflog 来救援 2010 年 9 月 9 日 — 克里斯·斯隆 (Chris Sloan) 撰写 | 0 条评论?
The other day, I was working on a feature for Real Travel using our current branching strategy in that each release we do is a separate branch.
Not sure if it was a cause of lack of sleep from late hours pulled, but I accidentally deleted my local and remote copy of the branch before I merged it back into the master branch for release.
After a quick state of shock and thoughts running through my head of losing hours of work, I calmed down and relied on my Git knowledge.
Reading your full commit history:There are two ways to read the commit history in git. The first way shows a list of details commits while the other shows the log in reference to the current
HEAD
.
前几天,我正在使用我们当前的分支策略为 Real Travel 开发一个功能,因为我们所做的每个版本都是一个单独的分支。
不确定这是否是由于拉到深夜导致睡眠不足的原因,但在将其合并回 master 分支以进行发布之前,我不小心删除了该分支的本地和远程副本。
在短暂的震惊和失去工作时间的想法在我脑海中闪过之后,我冷静下来并依靠我的 Git 知识。
阅读完整的提交历史记录:有两种方法可以在 git 中读取提交历史。第一种方式显示提交细节的列表,而另一种方式显示引用当前
HEAD
.
// log of detailed commits by users
$> git log
// reference log compared to the current HEAD
$> git reflog
Using the
reflog
command, I was able to find out exactly where the last reference to my deleted branch was.
An example output of thereflog
might look like this:
使用该
reflog
命令,我能够准确地找出对已删除分支的最后一个引用的位置。
的示例输出reflog
可能如下所示:
c7f3d98 HEAD@{0}: commit: Merged in some code
f5716c8 HEAD@{1}: pull : Fast-forward
d93c27b HEAD@{2}: commit: Added some items to project
...
Now the reflog will not show exactly where the branch was deleted, but if you remember your last commit to that branch and have a detailed enough message, it should be easy to find and restore.
Restoring your branch is straight forward by checking out the HEAD you want to a new branch.
现在 reflog 不会准确显示分支被删除的位置,但是如果你记得你最后一次提交到那个分支并且有足够详细的消息,应该很容易找到和恢复。
通过检查你想要一个新分支的 HEAD 来恢复你的分支是直接的。
$> git checkout -b my_new_branch HEAD@{5}
You can also use the hash too to checkout the new branch.
您也可以使用散列来签出新分支。
$> git checkout -b my_new_branch d93c27b
Simple enough and now I can move on with actually merging the branch in before deletion.
足够简单,现在我可以继续在删除之前实际合并分支。