Michael Crosby

Setup your own Git Server over SSH

The thing about dvsc that makes me nervous is that all the code and revision history is on your local computer. This makes it very fast when committing, branching, and merging but what if you have a drive failure or worse? All your code is gone if you don't have backups. You have many different ways to backup your git and mercurial repositories. Especially this open-source project that I am working when it is done: DVCS-Backup

If you want to work with others on a project and need a git server setup to be an intermediary, then this is how you can do it as well as having an offsite backup copy of your repos.

First find a server that you are going to use. Setup keyless ssh access to it following my tutorial here

After that ssh into your server and make a new user that will manage the repos if sharing or just user your own account if it's just for you.

Install git on your server. I run debian so it's a simple:

apt-get install git-core

To get git installed.

Now create a new folder for the repo that you want to push to. I have many repos on my development computer that I work from so we are going to push an already populated repo to the server.

mkdir somereponame.git

You add the .git to the end of the folder name to that it's easy to distinguish between other folders. .git means this is a git repo.

Now cd into it and create a bare repo.

git init --bare

Now that server part is done. Easy.

Now go to your local dev computer and cd into the repo that you want to push to the server. Make sure that you don't have a remote origin already setup in this repo. To clear it out just:

git remote rm origin

Now we need to set the origin to your remote server.

git remote add origin ssh://sshusername@yourserver.com/~/myrepo.git

Now your done. Time to push. If you have more than one branch, you can either push one or all the branches to the server. I just push all of them because I want the server to have a full backup or my local repo.

git push origin --all

But for inividual branches:

git push origin branchname

That's it. Now when you want to get data from the server just do a pull.

git pull origin branchname

If you want to clone the repo to a new folder:

git clone ssh://sshusernane@yourserver.com/~/myrepo.git

Or you can make a new dir, cd into it and create a new local repo, set the origin to your server and pull specific branches.

mkdir newrepo
cd newrepo
git init
git remote add origin ssh://sshusername@yourserver.com/~/myrepo.git
git pull origin branchname
comments powered by Disqus