git 如何取消删除github上的一个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16398501/
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 16:05:25  来源:igfitidea点击:

How to undelete a branch on github?

gitgithub

提问by mans

It seems that I delete a branch on github when I should not do it.

看来我不应该删除github上的一个分支。

What I did was as follow:

我所做的如下:

1- I add a new .gitignore to my system

1- 我在我的系统中添加了一个新的 .gitignore

2- I use

2-我用

 git rm -r --cached .  
 git add .  
 git commit -m ".gitignore is now working"  

When I did this, I had one branch on my local system but the server had two branch.

当我这样做时,我的本地系统上有一个分支,但服务器有两个分支。

Then I pushed my branches to server and since I had not the second branch, the second branch was deleted on server.

然后我将我的分支推送到服务器,由于我没有第二个分支,第二个分支在服务器上被删除。

How can I bring it back?

我怎样才能把它带回来?

I am using Github as remote server.

我使用 Github 作为远程服务器。

回答by sites

If you know the last commit message of the deleted branch you can do this:

如果您知道已删除分支的最后一条提交消息,则可以执行以下操作:

git reflog

# search for message

# 搜索消息

fd0e4da HEAD@{14}: commit: This is the commit message I want

# checkout revision

# 结帐修订

git checkout fd0e4da 

or

或者

git checkout HEAD@{14}

# create branch

# 创建分支

git branch my-recovered-branch

# push branch

#推送分支

git push origin my-recovered-branch:my-recovered-branch

回答by Brock

If this branch was deleted during the Pull Request, you can undo that right there in the UI using the "restore branch" button.

如果此分支在 Pull Request 期间被删除,您可以在 UI 中使用“恢复分支”按钮撤消该操作。

Tricky part is actually finding a PR that has been merged and closed, you just need to know the URL or the PR number to put into the URL. You can try looking in your deleted notification emails or just guess the PR number.

棘手的部分实际上是找到一个已经合并关闭的 PR,你只需要知道 URL 或 PR 号放入 URL。您可以尝试查看已删除的通知电子邮件或仅猜测 PR 编号。

Writing this cause reflogdidn't help me restoring a teammate commit to a branch I've never pulled on my local git.

写这个原因reflog并没有帮助我恢复对我从未在本地 git 上拉过的分支的队友提交。

回答by Luiz E.

what if you git fetch remote && git checkout -b remote_branch_name

如果你 git fetch remote && git checkout -b remote_branch_name