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

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.)


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


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.

Tags:

Git