Create a new self-hosted git repository

08 Aug 2013

You have access to a server already running git, and you want to use it to store a private remote repository.

You might want to make sure to have set up passwordless ssh beforehand. Login to the remote server and create a bare repository:

you@local ~$ ssh user@remote.com
user@remote.com ~$ mkdir project.git
user@remote.com ~$ cd project.git
user@remote.com ~/project.git$ git init --bare
Initialized empty Git repository in /home/user/project.git/

To make the project usable by all users members of a group, say staff, also add:

user@remote.com ~/project.git$ cd ..
user@remote.com ~$ chgrp -hR staff project.git
user@remote.com ~$ chmod -R g+rwXs project.git
user@remote.com ~$ exit

Back on the local machine, clone the newly created empty repository:

you@local ~$ git clone ssh://user@remote.com/~/project.git
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
you@local ~$ cd project
you@local ~/project$ edit README.md      # copy or create some files
you@local ~/project$ git add . --dry-run # check what would be added
add 'README.md'
you@local ~/project$ git add .           # add all your initial files
you@local ~/project$ git commit -m 'initial commit'
[master (root-commit) 14da8ac] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
you@local ~/project$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 251 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://user@remote.com/~/project.git
 * [new branch]      master -> master
you@local ~/project$ git pull
Already up-to-date.

That’s it, now you can push and pull at will from the remote repository.