我如何将删除分支恢复到git中? [英] How do I get the deleted-branch back in git?
问题描述
我正在尝试将git用于我们的软件开发。我发现如果我在git中删除一个分支,我可能永远失去我的代码。这让我感到惊讶。我认为这是一个版本控制系统,它应该能够让我做任何事情(甚至是愚蠢的),而不用担心会伤害我的工作。
以下步骤是我所做的:
- 初始化一个git存储库。
-
- 删除当前工作目录中的第一个提交。主分店。
- 然后我失去了我的所有作品,我无法相信我所看到的。这是真的吗?如果我在编码时喝醉了。我可能会在git中丢失代码。
问题是如何回滚删除分支的操作?
或者,如何获得git中的所有历史记录,甚至是日志中已经消失的内容?
问题首先在评论中 Jefromi 建议:
< blockquote>
另一个技巧:只使用 git branch -d
,而不是 git branch -D
。
如果您要删除可能会让您失去工作的东西,那么您会收到警告,那么在使用 -D
。
(或者,您可以从 gitk
>删除,以便您真正看到要删除的内容。 )
-d
lockquote
删除一个分支。
该分支必须在其上游分支中完全合并,或者在 HEAD
如果没有上游设置为 - track
或 - set-upstream
。
但如果你的工作失败,请参阅 -deleted-branch-using-git-reflogrel =nofollow noreferrer>很多博客 about reflog (正如 James Kyburz 在评论中提示的那样):
返回列表
Git reflog加入救援
2010年9月9日 - 由Chris Sloan撰写| 0评论»
有一天,我正在使用我们当前的分支策略开发Real Travel的功能,因为我们每次发布的版本都是一个单独的分支。
不知道是否是晚上睡眠不足的原因,但是在我将它合并回主服务器之前,我意外删除了本地和远程副本。分支发布。
在经历了一段时间的工作失误之后,我迅速感到震惊和思考,我平静下来,依靠我的Git知识。
阅读完整的提交历史记录:
有两种方法可以在git中读取提交历史记录。第一种方式显示详细提交列表,另一种显示引用当前
HEAD
的日志。
//用户详细提交记录
$> git log
//参考日志与当前HEAD相比
$> git reflog
使用
reflog
命令,我能够确切地找到最后一个引用到我删除的分支。
reflog
的示例输出可能看起来像这样:
c7f3d98 HEAD @ {0}:commit:在某些代码中合并
f5716c8 HEAD @ {1}:pull:快进
d93c27b HEAD @ {2}:commit:将项目添加到项目
...
现在reflog不会精确显示分支被删除的位置,但是如果您记得最后一次提交到该分支并且有一个足够详细的消息,它应该很容易找到并恢复。
恢复您的分支是直接检查出你想要一个新的分支的头。 p>
$> git checkout -b my_new_branch HEAD @ {5}
您也可以使用
$> git checkout -b my_new_branch d93c27b
足够简单,现在我可以继续实际上在删除前合并分支。
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.
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.
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?
To avoid the issue in the first place, Jefromi advices in the comments:
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.)
-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
.
But if you did "lose" your work, see one of the many blogs about reflog (as James Kyburz suggests in the comments):
back to list Git reflog to the rescue September 09, 2010 — written by Chris Sloan | 0 comments »
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
.
// 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:
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.
$> 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.
这篇关于我如何将删除分支恢复到git中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!