简体   繁体   中英

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

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

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

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

local# git fetch bar

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

bar# 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. thanks a lot.

UPDATE:

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

local# git branch remote show bar

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

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

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.

This blog goes over how to do it:

Toolman Tim - Setting up a new remote git repository

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:

git push bar master

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

Instead of one of these:

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

for every ref'd branch in local. 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.

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

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

local# git branch remote show bar

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

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

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

UPDATE:

How to back up private branches in git

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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