Some systems engineers and admins are reluctant to embrace version control, I aim to show it’s not a black art, and should be in the toolbox of all admins.
The simplest thing to say about using git with github is that they combine to provide tools and a place for storing your code, scripts, config files, allowing you track updates and recover previous versions.
This guide shows you how to install git on a Debian based system and how to start using it. This use case illustration is simple because everyone needs to start somewhere.
Concepts
Git – git is software allowing you to do version management, change control on your files and much more. It was written originally for the Linux Kernel development project, plenty more information can be found on the official Git website http://git-scm.com/
Github – Is a web-based service which has made quite an impact on software development, it allows hosting of a git repository, public or private (for subscribers), and make sharing your code simple, further help with github can be found at https://help.github.com/
The github site allow you to create a ‘repository’ (henceforth ‘repo’), this is a container for your objects, i.e. files containing code, scripts, configs. It is possible to add, remove, amend both content in files and files in the repo locally, and then to synchronise with comments to the master repo.
In this example, I’m using a repo hosted on Github, but there is no reason you shouldn’t set up your own Git server. Setting up your own server is outside the scope of this illustration, Linux Magazine has an article about it http://www.linux-magazine.com/Online/Features/Install-Your-Own-Git-Server
Getting set up
Begin by installing git, and follow up by setting some variables..
$ sudo apt-get install git
<……>
$ git config –global user.name “Henry Rollins”
$ git config –global user.email hrollins@example.com
$ git config –global core.editor vi
N.B. Using –global flag makes these entries the default.
Now we see that someone named Henry Rollins is all set up ready to go and have a look at my ‘utils’ repo.
Pull down a repo
After setting up a repo on github using the web interface, change to your preferred working directory, i.e. wherever in the filesystem you want to pull the copy of the repo to (in my case ~/Documents/dev/ ), this is known as creating a clone of repo..
$ git clone https://github.com/robthatcher/utils.git
Note the clone operation creates a ‘utils’ directory, in your current working directory. Changing into the directory of the repo you will see that there might be a README.md file, or the contents of the repo you just cloned. It’s important to note this is a full local copy of the repository, and you can do anything you like to it without breaking the master copy.
Add a file to a repo
It’s quite simple to add a file to a repo, simple write what you want directly using your text editor or whatever of choice and save in the repo directory, or copy a file in from where it’s currently stored, e.g.
$ cp ~/bin/TestUtil
$ git add TestUtil
That copied the TestUtil script to the directory of the repo and added it(multiple files can be added using a ‘.’ ). You can see what you’ve done by using the command below.
$ git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD …” to unstage)
#
# new file: TestUtil
N.B. This file is now ‘staged’ as denoted by the ‘to be committed’ above.
Remove a file from a repo
Similarly, the file can be removed as follows
$ git rm TestUtil
Commit the file(s) into the repo
Whenever you are adding files to a new repo you may want to do an initial commit as illustrated.
$ git commit -m ‘Initial Commit’
[master 2082d26] Initial Commit
1 file changed, 5 insertions(+)
create mode 100644 TestUtil
The local repo is now ahead of the master, as denoted when running ‘git status’
$ git status
# On branch master
# Your branch is ahead of ‘origin/master’ by 1 commit.
#
nothing to commit (working directory clean)
Making further changes and updating
Edit the files as you need, saving changes and then on the command line run
$ git commit -a -m ‘Updated Date Information’
[master 68ccb95] Updated Date Information
1 file changed, 1 insertion(+), 1 deletion(-)
N.B. Note the -a flag to add all staged files and the -m to set the commit comment.
Checking what has been changed is easy there are two simple and useful methods
$git log -p -2
which shows a diff of the last two changes, or
$git log –graph
which gives a more visual representation as show in the picture to the right.
Managing remote repo and synchronising your changes
To check the location details of the repo you are in, use the remote -v command, which will show you the origin which was cloned.
$ git remote -v
origin https://github.com/robthatcher/utils.git (fetch)
origin https://github.com/robthatcher/utils.git (push)
Of course if you know the username and password to the origin repo you can push your changes back up to it using the command
$ git push origin master
Summary
Basic use of git is very simple, and something everyone could and probably should be doing.
I’ve chosen to use Github but as noted a private git repo server can be used if that better suits your requirements. Guides to set up and run in that fashion are scattered around the internet, some helpful websites are:
Git – http://git-scm.com/
Github Help – https://help.github.com/
Gitready – http://gitready.com/
One thought on “How to start using Git with Github as a Sysadmin”