Setup Git Repo on VPS

We can setup our own Git repo on VPS, it’s very easy. After the setup, we can access our repo as:

//clone our repo to local, we use git as username and domain as the server domain(like unieagle.net).
git clone git@domain:a_repo.git

The simple steps are as follows:

  1. add user ‘git’ on your host
  2. setup ssh access for your local computer to your host as ‘git’
  3. create and init your repo on your server
  4. access your repo in local computer

Details:

add user ‘git’ on your host

ssh login into your host as root, and do the following:

useradd -d /home/git -m git
passwd git

setup ssh access for your local computer to your host as user ‘git’

in your local computer, do the following:

if you have no rsa keys, you need to generate one first:

cd ~/.ssh
ssh-keygen -t rsa 

this will generate id_rsa and id_rsa.pub in your ~/.ssh directory

if your key name is not id_rsa you need to add your key with:

ssh-add your_custome_name_of_key

with these keys, do the following:

cat id_rsa.pub | ssh git@domain 'cat >> .ssh/authorized_keys'

then you are able to run ssh git@domain without your password if everything goes well.
if not, check your host with permissions of /home/git, it should belongs to user git.

create your own repo

this is done on your host

ssh git@domain
mkdir your_first_repo.git
cd your_first_repo.git
git --bare init

then your git repo is setup fullly

access your repo on your local computer

you can clone your repo to local using:

git clone git@domain:your_first_repo.git

then all works done. enjoy it.

Or, if you have a repo already exists in your local computer and want to using the repo on your host, just setup the remote and up stream of your local repo, it will works well, as following:

//assume you are already in your local repo directory
git remote add origin git@domain:your_first_repo.git
git push --set-upstream origin master