Running my own git server

From Wildsong Wiki
Jump to: navigation, search

Overview

Most work I do is out on github in public repos that anyone can access, search for geo-ceg and brian32768. I can't put every bit of my life out on the Internet, so I still operatet my own git server for proprietary projects.

Github.com migration

I have migrated most of my shareable projects to github.com to get them off my own server. Pretty much all the same git commands below work.

Migration: Use the Github Importer.

I used the command line importation. Basically you pull the repo with all its history onto your local machine then push it up to github. In brief

  1. Create the repository first at github, "ssurgo" in this case.
  2. Make a copy of your existing repo: git clone --bare [email protected]:contours.git
  3. cd ssurgo.git/
  4. Push everything up to github: git push --mirror https://github.com/geo-ceg/ssurgo.git
  5. cd ..
  6. Make sure it works! git clone [email protected]:Geo-CEG/ssurgo.git
  7. If it worked you have a directory "ssurgo" now. Remove the bare repo: rm -rf ssurgo.git
  8. Try commiting by making a README. emacs README.me
  9. git add README.md
  10. git commit -a -m "added README!"
  11. git push

Use case: web app development

One of the main reasons I want my own git server is so that I can sync a web app between my laptop and a server.

My work flow: I develop on the laptop, then I push changes to the git server, then I pull the changes down into the live web server.

The theory is that if the live server breaks after an update, I can revert the changes.

Use case: Asterisk servers

Each server needs tweaks to its configuration. When you are bending config files and something stops working you need to be able to back out the changes.

File ownerships and access control

Access to the server is through ssh. The repositories will be owned by the user 'git'. So I put the home for user 'git' where I want the repositories to be; on Bellman I use /green/repositories. Then I set up ssh with keys so that I don't need to share passwords.

I put create a separate 'git' key pair and put the public key into ~git/.ssh/authorized_keys I can test it with "ssh [email protected] list", this should list the available repos and exit.

Once that's all in place I don't need a complete path for the "remote" command, just one relative to git's home.

Creating a new private repo

See also: http://www.git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server

I am using ssh for file transfer.

I find it easiest to create a new empty repo directly on the server and then push existing content into it.

ssh bellman
cd ~git
sudo git init --bare myproject.git create a bare repository
sudo chown -R git.wildsong myproject.git
exit

cd Projects
assuming myproject is a folder containing the files you want in a repo
cd myproject
git init
make a gitignore file in myproject
git add some files
git commit -m 'initial commit'
git remote add origin [email protected]:myproject.git
git push origin master this pushes committed files from the local repository to the master branch on the remote machine

Now I should be able to clone my project onto the web server and start using git to keep it updated.

ssh webserver
cd /var/www/appserver
git clone git.wildsong.biz:myproject.git

On my copy I need to define the remote repo,

git remote add origin [email protected]:myproject.git

When I make changes on the local laptop, first I commit them locally.

git commit

..then I push them up to the git server

git push origin master

..then I pull them down onto the public web server

git fetch

Then I create a virtualenv and load the requirements.

cd myproject
virtualenv env
source env/bin/activate
pip install -r requirements.txt
python run.py

Branching and all that

This explanation is pretty good. Darn good. http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Git submodules

I keep some code in github, and I want to incorporate it into a project. I can use the submodule feature.

cd myproject
git submodule add [email protected]:brian32768/pyst2.git

This clones pyst2 into a subdirectory (called pyst2 by default) Now I can work on pyst2 and do commits and pushes but it remains separate from 'myproject'. If I ask for status...

git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

modified: .gitmodules new file: pyst2

If I am in a virtualenv then I can cd into pyst2 and do 'python setup.py install' and the right thing will happen.

When you clone a new copy or update an existing clone (git pull) then the submodules will be there but empty, you have do do this

cd pyst2 (or whatever your submodule folder is)
git submodule init
git submodule update

That will tell git to populate the submodule folder with code from the other repository.

Git in Docker

Docker mania! This one looks good. It is built on alpine and it has support for keys and data in volumes. Should drop right in on Bellman and replace the system level version.

https://hub.docker.com/r/jkarlos/git-server-docker/