How can I push a Git repository to a folder over SSH?

55

28

I have a folder called my-project inside which I've done git init, git commit -a, etc.

Now I want to push it to an empty folder at /mnt/foo/bar on a remote server.

How can I do this?

I did try, based on what I'd read:

cd my-project
git remote add origin ssh://user@host/mnt/foo/bar/my-project.git
git push origin master

which didn't seem right (I'd assume source would come before destination) and it failed:

fatal: '/mnt/boxee/git/midwinter-physiotherapy.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

I'd like this to work such that I don't have to access the remote host and manually init a Git repository every time ... do I have to do that? Am I going down the right route at all?

Thanks.

rich

Posted 2011-01-08T18:39:52.037

Reputation: 1 071

Answers

58

The command is correct; however, the remote address must point to an initialized Git repository too. It's a one-time job, though.

ssh user@host "git init --bare /mnt/foo/bar/my-project.git"

(In Git, a "bare" repository is one without a working tree.)

user1686

Posted 2011-01-08T18:39:52.037

Reputation: 283 655

12

If you want to both push to the repo and have the files update on the server, you can create a server-side git hook to checkout the files after they've been pushed. In the server side git /hooks/ directory create a file named post-receive and add the following code (updating the directories to match your folder structure):

#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/site.git checkout -f

Then give the file proper permissions using chmod +x post-receive

More info & a detailed explanation here: https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

Kyle Chadha

Posted 2011-01-08T18:39:52.037

Reputation: 241

2

If you don't want to create the repository manually on the server, you could install gitosis, which will automate the process. But you have to have some process on the server to create the repository -- you can't do it over a git ssh connection from the client.

Mike Scott

Posted 2011-01-08T18:39:52.037

Reputation: 4 220