git Git撤销本地分支删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4025916/
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
Git undo local branch delete
提问by Stefan Kendall
I just deleted the wrong branch with some experimental changes I need with git branch -D branchName
.
我刚刚删除了错误的分支,并进行了一些我需要的实验性更改git branch -D branchName
。
How do I recover the branch?
如何恢复分支?
回答by bobDevil
You can use git reflogto find the SHA1 of the last commit of the branch. From that point, you can recreate a branch using
您可以使用git reflog查找分支最后一次提交的 SHA1。从那时起,您可以使用重新创建分支
git branch branchName <sha1>
Edit:As @seagullJS says, the branch -D
command tells you the sha1, so if you haven't closed the terminal yet it becomes real easy. For example this deletes and then immediately restores a branch named master2
:
编辑:正如@seagullJS 所说,该branch -D
命令会告诉您 sha1,所以如果您还没有关闭终端,它会变得非常简单。例如,这会删除然后立即恢复名为 的分支master2
:
user@MY-PC /C/MyRepo (master)
$ git branch -D master2
Deleted branch master2 (was 130d7ba). <-- This is the SHA1 we need to restore it!
user@MY-PC /C/MyRepo (master)
$ git branch master2 130d7ba
回答by Chetan
回答by amichaud
If you haven't push the deletion yet, you can simply do :
如果您还没有推送删除,您可以简单地执行以下操作:
$ git checkout deletedBranchName
回答by Freelancer
If you just deleted the branch, you will see something like this in your terminal:
如果您刚刚删除了分支,您将在终端中看到如下内容:
Deleted branch branch_name(was e562d13)
- where e562d13 is a unique ID (a.k.a. the "SHA" or "hash"), with this you can restore the deleted branch.
- 其中 e562d13 是唯一 ID(又名“SHA”或“hash”),这样您就可以恢复已删除的分支。
To restore the branch, use:
要恢复分支,请使用:
git checkout -b <branch_name> <sha>
for example:
例如:
git checkout -b branch_name e562d13
回答by Rajeev Jayaswal
Thanks, this worked.
谢谢,这有效。
git branch new_branch_name
sha1
git checkout new_branch_name
git 分支 new_branch_name
sha1
git checkout new_branch_name
//can see my old checked in files in my old branch
//可以在我的旧分支中看到我的旧签入文件
回答by Cameron Skinner
First: back up your entire directory, including the .git directory.
首先:备份整个目录,包括 .git 目录。
Second: You can use git fsck --lost-found
to obtain the ID of the lost commits.
第二:可以git fsck --lost-found
用来获取丢失提交的ID。
Third: rebase or merge onto the lost commit.
第三:变基或合并到丢失的提交上。
Fourth: Always think twice before using -D or --force with git :)
第四:在将 -D 或 --force 与 git 一起使用之前,请务必三思:)
You could also read this good discussionof how to recover from this kind of error.
EDIT: By the way, don't run git gc
(or allow it to run by itself - i.e. don't run git fetch
or anything similar) or you may lose your commits for ever.
编辑:顺便说一句,不要运行git gc
(或允许它自己运行——即不要运行git fetch
或任何类似的东西),否则你可能会永远失去你的提交。
回答by greencheese
Follow these Steps:
按着这些次序:
1: Enter:
1:输入:
git reflog show
This will display all the Commit history, you need to select the sha-1 that has the last commit that you want to get back
这将显示所有提交历史记录,您需要选择具有您想要取回的最后一次提交的 sha-1
2: create a branch name with the Sha-1 IDyou selected eg: 8c87714
2:使用您选择的Sha-1 ID创建一个分支名称,例如:8c87714
git branch your-branch-name 8c87714
回答by Alej priv
This worked for me:
这对我有用:
git fsck --full --no-reflogs --unreachable --lost-found
git show d6e883ff45be514397dcb641c5a914f40b938c86
git branch helpme 15e521b0f716269718bb4e4edc81442a6c11c139