Using Git for Source Control

by mike on May 4, 2012

GIT is the source control tool, a distributed version control system (dvcs) which is written in C. This provides a history of the files that are maintained by it. In distributed version control each user has a complete copy of the code so there is no central code repository. In this scenario an administrator makes changes, adds them to the index (called staging) and then adds them to the repository (called commit). Git will take this information and maintain a version history that users can track. This is all performed locally but could be synchronized with a remote repository.

Installation on CentOS 6

Install Dependencies
yum install -y zlib-devel openssl-devel perl cpio expat-devel gettext-devel autoconf gcc curl-devel

yum install -y perl-ExtUtils-MakeMaker

Get the latest version.
cd /tmp

tar zxvf git-1.7.10.tar.gz
cd git-1.7.10
make prefix=/usr/local all
sudo make prefix=/usr/local install
You can update with this command.
git clone git://

Set Up Your Identity
git config --global "Tom Smith"
git config --global

git config --global core.editor vim
git config --global merge.tool vimdiff
git config --global push.default
git config --global color.status auto
git config --global color.branch auto

Now you can review the global settings you have created.

git config --list Smith

If you need help you can use the help option and a subject.
git help config

Initialize an Existing Project
The idea is to run version control on this very important directory.

Move to the directory you want to incorporate into the project.
cd /your_project
git init

git add *.cfg

This will add all of the configuration files (.cfg) that are referenced by nagios.cfg.

Now commit these to the repository. You will need to use the commit command and then add “-m” an initial version of the code base.

git commit -m "1.0"
[master (root-commit) cc8a2ed] 1.0
29 files changed, 2751 insertions(+)
create mode 100644 commands.cfg
create mode 100644 contacts.cfg
create mode 100644 distributed.cfg
create mode 100644 escalation.cfg
create mode 100644 freebsd.cfg
create mode 100644 hostgroups.cfg
create mode 100644 ipv6.cfg
create mode 100644 localhost.cfg
create mode 100644 mysql.cfg
create mode 100644 nrpe.cfg
create mode 100644 passive.cfg
create mode 100644 postfix.cfg
create mode 100644 printer.cfg
create mode 100644 public.cfg
create mode 100644 reachability.cfg
create mode 100644 routers.cfg
create mode 100644 security.cfg
create mode 100644 servicedependency.cfg
create mode 100644 servicegroups.cfg
create mode 100644 snmp.cfg
create mode 100644 ssh.cfg
create mode 100644 switch.cfg
create mode 100644 templates.cfg
create mode 100644 timeperiods.cfg
create mode 100644 ubhosts.cfg
create mode 100644 ubservices.cfg
create mode 100644 webinject.cfg
create mode 100644 win08.cfg
create mode 100644 windows.cfg

The status command will help you understand where you are at. Here, nothing has changed.

git status
# On branch master
nothing to commit (working directory clean)

Every directory you use git with you will have a .git folder which manages the version control.

drwxr-xr-x 8 root root 4096 May 2 04:41 .git
branches COMMIT_EDITMSG config description HEAD hooks index info logs objects refs

If you had files or directories that you wanted to ingnore and not track you can create the .gitignore file and list each file. So in this example a README file is created and saved in the directory. If you do not wan to track this file add it to that .gitignore file.

git add .gitignore
git commit -m "ignore file added"

Now changes to the README are not tracked.

Review a history of changes.
git log
commit 9eed65846822bfb41e07d8442d3871dc09fa5f71
Author: Tom Smith <>
Date: Wed May 2 04:45:55 2012 -0600

ignore file added

commit cc8a2ed809cd85f611b9b8d1db08315106959b18
Author: Tom Smith <>
Date: Wed May 2 03:56:48 2012 -0600

An administrator now adds this service check for the MySQL servers in the mysql.cfg file:
define service{
use generic-service
hostgroup_name mysql_servers
check_command check_snmp_process!$_HOSTSNMP_COMMUNITY$!-n httpd -w 3,8 -c 0,9
service_description SNMP Apache Processes

This is an especially important process to watch as this check is added to an entire hostgroup which will push the changes to many servers at once. Just the edit is now tracked with git.
git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
# modified: mysql.cfg
no changes added to commit (use “git add” and/or “git commit -a”)

Using diff an administrator can review the changes which have not been committed.

git diff
diff –git a/mysql.cfg b/mysql.cfg
index 1992ba5..441b642 100644
— a/mysql.cfg
+++ b/mysql.cfg
@@ -25,4 +25,10 @@ define service{
check_command check_snmp_load!$_HOSTSNMP_COMMUNITY$!90%!95%
service_description SNMP CPU usage
+define service{
+ use generic-service
+ hostgroup_name mysql_servers
+ check_command check_snmp_process!$_HOSTSNMP_COMMUNITY$!-n httpd -w 3,8 -c 0,9
+ service_description SNMP Apache Processes
+ }

Now commit the change and add information for others what was done.

git commit -a -m "added new snmp process check for apache"
[master b86f07e] added new snmp process check for apache
1 file changed, 6 insertions(+)

A simple but powerful way to track changes on whatever project you have.

Comments on this entry are closed.

Previous post: