简体   繁体   English

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

[英]how to recover an accidently deleted remote git repository from local repository

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 local# 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, ie undo all I did to host bar. 你能告诉我如何从我的本地副本恢复ssh主机(bar)上的项目,所以ssh主机上的其他开发人员可以继续他们的工作,我可以运行'git fetch bar'来获取他们的提交,就像我没有对他们的ssh主机做任何错误,即撤消我所做的所有主持吧。 thanks a lot. 非常感谢。

UPDATE: 更新:

bar# mkdir -p /home/project && cd /home/project && git init --bare bar# 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 local# git push bar bar/master:refs/heads/master

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

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

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: 假设您仍然将栏设置为远程存储库,基本上:

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

refspec is frequently just the simple name of the branch in the local repository so: refspec通常只是本地存储库中分支的简单名称,因此:

git push bar master

See the git push manpage for a detailed treatment of what qualifies for a refspec. 有关refspec的资格,请参阅git push联机帮助页 The EXAMPLES section is particularly helpful in understanding more advanced respecs EXAMPLES部分特别有助于理解更高级的respec

Instead of one of these: 而不是其中之一:

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

for every ref'd branch in local. 对于本地的每个ref'd分支。 Try this 试试这个

$ 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',其中大多数人会有'起源' - 替换为遥控器的名称。

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 bar# 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 local# git push bar bar/master:refs/heads/master

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

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

UPDATE: 更新:

How to back up private branches in git 如何在git中备份私有分支

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM