Create a git repository on a server with ssh access

2 minute read

Here is a simple way to set up a git repository on a server with ssh access.

1. Init the empty repository on the server

Instead of creating a bare repository locally, transfer it to a git server and delete the local copy afterwards, i create the (empty) git repository on the server itself (which requires shell access via ssh).

thomas@gitserver:/home/git$ mkdir <projectname>
thomas@gitserver:/home/git$ cd <projectname>/
thomas@gitserver:/home/git/<projectname>$ git --bare init --shared
Initialized empty shared Git repository in /home/git/<projectname>/

I still don’t remember the mandatory order of these options…

2. Connect the local git repository with the newly created server repository

The local git repository has to be created.

thomas@local$ git remote add origin thomas@gitserver:/home/git/<projectname>

Now the local and remote repository are connected.

3. Initial upload of content

Now we can push our local created content (branch master) to the server repo.

thomas@local:/home/thomas/<projectname>$ git push origin master
Counting objects: 67, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (56/56), done.
Writing objects: 100% (67/67), 79.96 KiB, done.
Total 67 (delta 10), reused 0 (delta 0)
To thomas@gitserver:/home/git/<projectname>
 * [new branch]      master -> master
4. Connect the master branch for pull

Finally you have to tell git which branch to pull:

thomas@local:/home/thomas/<projectname>$ git remote set-branches origin master

Done!