如何从本地存储库中恢复意外删除的远程 git 存储库

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

how to recover an accidently deleted remote git repository from local repository

git

提问by yoda

I've done something like following:

我做了如下的事情:

(1) clone a remote git repository to local host

(1) 将远程git仓库克隆到本地主机

local# git clone http://www.foo.com foo

当地的# git clone http://www.foo.com foo

(2) add another project on a ssh host(bar) as the second remote repository

(2) 在 ssh 主机(bar)上添加另一个项目作为第二个远程存储库

local# git remote add bar ssh://bar/home/project

当地的# git remote add bar ssh://bar/home/project

local# git fetch bar

当地的# git fetch bar

(3) done something stupid on the ssh host(bar):

(3) 在 ssh 主机上做了一些愚蠢的事情(bar):

bar# rm -rf /home/project

酒吧# rm -rf /home/project

Could you please tell me how can I recover project on the ssh host(bar) from my local copy, so other developer on the ssh host can continue their work, and I can run 'git fetch bar' to get their commit, just like I didn't do anything wrong to their ssh host, i.e. undo all I did to host bar. thanks a lot.

您能否告诉我如何从本地副本恢复 ssh 主机(bar)上的项目,以便 ssh 主机上的其他开发人员可以继续他们的工作,并且我可以运行“git fetch bar”来获取他们的提交,就像我没有对他们的 ssh 主机做任何错事,即撤消我对主机栏所做的一切。多谢。

UPDATE:

更新:

bar# mkdir -p /home/project && cd /home/project && git init --bare

酒吧# mkdir -p /home/project && cd /home/project && git init --bare

local# git branch remote show bar

当地的# git branch remote show bar

local# git push bar bar/master:refs/heads/master

当地的# git push bar bar/master:refs/heads/master

local# git push bar bar/branch1:refs/heads/branch1

当地的# git push bar bar/branch1:refs/heads/branch1

local# git push bar bar/branch2:refs/heads/branch2

当地的# git push bar bar/branch2:refs/heads/branch2

采纳答案by Mark Carey

You can setup the remote host as a new git repository and then push to it.

您可以将远程主机设置为新的 git 存储库,然后推送到它。

This blog goes over how to do it:

这个博客介绍了如何做到这一点:

Toolman Tim - Setting up a new remote git repository

Toolman Tim - 设置新的远程 git 存储库

assuming you still have bar setup as a remote repository, essentially:

假设您仍然将 bar 设置为远程存储库,本质上:

ssh bar "mkdir -p /home/project && cd /home/project && git --init bare"
git push bar refspec

refspecis frequently just the simple name of the branch in the local repository so:

refspec通常只是本地存储库中分支的简单名称,因此:

git push bar master

See the git push manpagefor a detailed treatment of what qualifies for a refspec. The EXAMPLES section is particularly helpful in understanding more advanced respecs

有关符合refspec 的条件的详细说明,请参阅git push 联机帮助页。示例部分对于理解更高级的规格特别有帮助

回答by omnisis

Instead of one of these:

而不是其中之一:

$ git push bar bar/branchX:refs/heads/branchX

for every ref'd branch in local. Try this

对于本地的每个 ref'd 分支。尝试这个

$ git push bar refs/remotes/bar/*:refs/heads/*

The above command should push all the remote refs you had cached locally back to the remote and put them in the right spot.

上面的命令应该将您在本地缓存的所有远程引用推送回远程并将它们放在正确的位置。

Note that you also need to push any tags you might have had:

请注意,您还需要推送您可能拥有的任何标签:

$ git push --tags bar

Also, it helps to know what's going to happen before you actually do the push:

此外,它有助于在您实际执行推送之前了解将要发生的事情:

$ git push --dry-run ...(rest of push cmd)  

NOTE: I used 'bar' where most people would have 'origin' - replace with the name of your remote.

注意:我在大多数人都有“起源”的地方使用了“bar” - 替换为您的遥控器的名称。

回答by yoda

Follow Mark Carey's answer, I've particaly recovered the deleted repository as following:

按照 Mark Carey 的回答,我已经部分恢复了已删除的存储库,如下所示:

bar# mkdir -p /home/project && cd /home/project && git init --bare

酒吧# mkdir -p /home/project && cd /home/project && git init --bare

local# git branch remote show bar

当地的# git branch remote show bar

local# git push bar bar/master:refs/heads/master

当地的# git push bar bar/master:refs/heads/master

local# git push bar bar/branch1:refs/heads/branch1

当地的# git push bar bar/branch1:refs/heads/branch1

local# git push bar bar/branch2:refs/heads/branch2

当地的# git push bar bar/branch2:refs/heads/branch2

UPDATE:

更新:

How to back up private branches in git

如何在 git 中备份私有分支