70

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?

4 Answers 4

72

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

18

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

5

For me, I had to use ssh://user@host:/mnt/foo/bar/my-project (note the : after the host, and remove .git at the end).

So I could clone a repository with:

git clone ssh://user@host:/mnt/foo/bar/my-project

We also can note that the format is like using scp command.

1
  • 1
    cloning works with this command but for me pushing again gives: remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: is denied, because it will make the index and work tree inconsistent remote: with what you pushed, and will require 'git reset --hard' to match remote: You can set the 'receive.denyCurrentBranch' configuration variable ....
    – dcsan
    Apr 26, 2020 at 10:11
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .