30

似乎我不应该这样做时删除了github上的一个分支。

我所做的如下:

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

2-我用

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

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

然后我将我的分支推送到服务器,因为我没有第二个分支,所以第二个分支在服务器上被删除了。

我怎样才能把它带回来?

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

4

4 回答 4

74

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

git reflog

# 搜索消息

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

# 结帐修订

git checkout fd0e4da 

或者

git checkout HEAD@{14}

# 创建分支

git branch my-recovered-branch

#推送分支

git push origin my-recovered-branch:my-recovered-branch
于 2013-05-06T12:41:14.363 回答
17

如果此分支在拉取请求期间被删除,您可以在 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.

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

于 2016-01-21T13:31:10.327 回答
4

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

于 2013-05-06T12:20:31.223 回答
0

Inside github.com you can restore it after the fact by Pull requests, open the last request and you should see a Recover/Restore branch button

于 2021-03-17T20:53:43.397 回答