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

Using Github

git status

this is used for checking the status of local on the current branch.

git fetch origin aRemoteBranchName:targetLocalBranch

this will fetch the remote branch named aRemoteBranchName into local repo with branch name targetLocalBranch

git push origin aLocalBranch:targetRemoteBranch

this will push local branch aLocalBranch into the remote repo of the targetRemoteBranch, the :targetRemoteBranch can omitted if the branch name is the same as local branch.

Squash commits

//squash 3 commit into one, doing following:
git rebase -i HEAD~3
//in the coming edit, replace 'pick' to 's' for the following 2 commits, this will squash them into the first commit
//then 
git rebase --continue

Edit commit message

// a -> b -> c -> d
//to edit the commit message for d, doing following:
git commit --amend
//in the edit interface, just edit the commit message there

//if you want to edit the message for b, doing the following:
git rebase -i HEAD~3
//just save the coming text
//then the b becomes the last commit and c,d is disappeared temporarily
git commit --amend
//edit the message for b
//then rebase back
git rebase --continue