Most people in DevOps won't set up their own instance of Git. With so many enterprise products on the market today, many of which integrate easily with workflow tools, the roll-your-own approach is becoming rare.
However, there are usecases where installing your own Git server is useful, or even necessary. It's also a good learning experience, and so before embarking on my DevOps journey, I wanted to setup a Git server and client.
I didn't want to use the public Github, or any free service. I wanted to do it the hard way to ensure that I knew the process and could relate to the pain that ops feel in setting a new system up!
Installing Git as a Server
I started out by following the instructions from Git on the Server – Setting Up the Server. It's an informative article, and well-referenced by others.
However, as with all things Linux, one of the steps didn't work. When I followed the process and tried to push my code to the remote Git server, it failed. After a bit of research, I found out these are the steps that works.
$ sudo adduser git $ su git $ cd $ mkdir .ssh && chmod 700 .ssh $ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys $ cd /opt/git $ git init --bare myproject.git #this step differs from the reference page Initialized empty Git repository in /opt/git/project.git/ # on John's computer $ cd myproject $ git init $ git add . $ git commit -m 'initial commit' $ git remote add origin git@gitserver:/opt/git/project.git $ git push origin master $ git clone git@gitserver:/opt/git/project.git $ cd project $ vim README $ git commit -am 'fix for the README file' $ git push origin master
It was basically one line that caused the problem:
$ git init –bare myproject.git
Without this step, the git reference files were created a directory above the folder marked as repository. The code push would fail and I’d see only errors. Once I made this correction, the error went away and the I was able to continue using Git.
Tweaking and Checking Git Settings
After installing Git, here are some tips to check or update the git settings. The first set lists the current settings on Git and the rest of the commands is for tweaking the git settings.
Git settings are divided into three sections as follows:
- System settings: applies to all users and all repositories.
- Global settings: applies to this users for all the user's repositories.
- Local settings: applies to this repository.
System settings are overridden by Global; Global settings are over-ridden by Local.
# print the current Git configuration git config --list # set the editor for this user globally git config --global core.editor=vim # set the git user name locally for this repository git config --local user.name # create a git ignore rules globally and create .gitignore git config --global core.excludesfile ~/.gitignore_global