10

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, i.e. 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

1
  • If you have solved the problem, put the commands in a new answer. Dec 21, 2009 at 13:45

3 Answers 3

8

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

2
  • thanks but I can't make it work: local# ssh bar "mkdir -p /home/project && cd /home/project && git init --bare" local# git push bar No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. fatal: The remote end hung up unexpectedly error: failed to push some refs to 'ssh://bar/home/project'
    – yoda
    Dec 21, 2009 at 11:01
  • that's why I referenced the blog which went much more in depth and used the word "essentially". I will make my code sample more explicit so as to make the answer complete.
    – Mark Carey
    Dec 22, 2009 at 4:56
4

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.

1

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

1
  • "Follow Mark Carey's answer"... if my answer was the "correct" answer then would it not be appropriate to award it "Answer" status.
    – Mark Carey
    Dec 22, 2009 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.