A Git Primer For New Users
This is a git primer for newbies or those people who – want to use Git to version their data, but don’t know how.
What is Git
Git is a Version Control System.
Version Control System (VCS) is a software that allows you to manage changes of documents, programs, images and other information that is stored in form of computer files. Changes are usually identified by an incrementing number or letter code also known as revision number or revision.
The simplest usage of versioning is – you can easily go back to the previous working version of your files, should you mess something up with the latest changes.
There are numerous version control systems and git is one of them.
This git primer explains the steps needed to be up and running in using git for versioning your files.
How To Install Git
To start using git for version control, you will first have to install it on your machine. Fortunately, Git runs on Windows, Linux, Unix, and Mac OS X.
If you are using Fedora, you can install Git using the following command :
$ su -c 'yum install git gitk'
gitk is a Git revision tree visualiser.
If you are using any other Linux distribution, download it using its package management tool.
Now that you have git installed on your system, lets get started…
First Things First
Before you even begin using git to version your files, you need to introduce yourself to git using your name and public email address. This is done as follows.
Open a terminal and run the following commands.
$ git config --global user.name "your name" $ git config --global user.email [email protected]
Don’t forget to replace “your name” with your actual name, and [email protected] with your public email id.
A Scenario
I save all my blog posts as text files in the directory ~/Documents/posts and I am interested in versioning this directory. Let me show you how it is done.
Step 1: Initialize the working directory
Move into the directory and initialize the directory for versioning using git.
$ cd ~/Documents/posts $ git init
This will create a hidden directory called .git in your current directory.
Step 2: Create a snapshot of all the files in the directory.
Add all the files in the directory to a temporary staging area which git calls the “index”. This is called creating a snapshot.
$ git add .
Step 3: Permanently store the snapshot in the repository.
The name for permanently storing a snapshot in the repository is called committing. So you commit the snapshot.
$ git commit
From here on, each time I modify any file in the directory or save a new file to the directory, I will have to add the updated contents to the index (Create a snapshot – Step 1) and then permanently store the snapshot in the repository (Step 2).
Moving on…
I made some changes to the file git-tutorial.txt residing in my ~/Documents/posts folder. I should now add the changed file to the git index as follows.
$ git add git-tutorial.txt
At this point of time, if I want to get a brief summary of the status I should run the following command.
$ git status
The following is the truncated output of the git status command.
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: git tutorial.txt
#
The output shows that I have yet to commit the modified file “git tutorial.txt”
So I commit the file to the repository.
$ git commit
Note: Each time you run the “git commit” command, git will prompt you for a message describing the change, and then record a new version of the project.
You can also pass the message in the command line itself by using the -m switch as follows :
$ git commit -m "Revision 3 blah blah blah"
Alternately, you can combine the “git add” and “git commit” command into a single command as follows.
$ git commit -a
This will automatically notice any modified files, add them to the index, and commit, all in one single step.
At any point you can view the history of your changes as follows.
$ git log
The truncated output of the above command is as seen below.
commit b042fd5916eec3957ef6e8249bba8b3b43e04855
Author: Linux And Friends
Date: Sat Jan 14 11:05:47 2012 +0530
Git tutorial ver 1.0
This is a primer on how to use git for version con
To see the details of the commit …
$ git show b042fd5916eec3957ef6e8249bba8b3b43e04855
Where b042fd5916eec3957ef6e8249bba8b3b43e04855 is the unique name of the commit as seen in the output of the ‘git log’ command.
Note: You don’t have to enter the full string of characters. You can just use the first 10 or so characters in the name.
For instance,
$ git show b042fd5916
will also give the same output as the previous one. Now if you find it difficult to remember random strings, you can give them a human friendly name using the tag feature of git as follows.
$ git tag ver1.1 b042fd5916
Now the following command will give you the same result.
$ git show ver1.1
Here are a few other commands that are useful for peeking into your git repository.
$ git show HEAD
This will show the tip of the current branch. To see the previous branch replace HEAD with HEAD^.
HEAD^^ for one before that and so on. To see the great great grandparent of HEAD use HEAD~4.
This is useful if for instance, you have made a mistake in a document and you want to revert to a previous version of the document.
For example, if you run the following command …
$ git revert HEAD
It will create a new commit which undoes the change in HEAD. You will be given a chance to edit the commit message for the new commit.
And the following command will revert to an earlier change for example, next-to-last.
$ git revert HEAD^
How to recover a few files deleted from my directory
Lets say I accidentally deleted a few files in my directory and I wish to restore them. If I am using git for version control it is easy.
First I show the deleted files using the following command :
$ git ls-files -d
Next I retrieve the files.
$ git ls-files -d -z | xargs -0 git checkout --
The -z option is used to separate the paths with NULL character. And -0 option in xargs ensures you don’t encounter any problems with files which have white space in their names.
To retrieve individual files you can use the following command:
$ git checkout -- <file name>
If you messed up big time, you can reset your checkout using the following command as well.
$ git checkout -f
… and it will download a copy of all the files from the last commit you made.
End Note
This Git primer has covered the basic commands of git that you will be using often. However, git has a lot more commands at its disposal.
References – Git Cheatsheet [Link], man gittutorial, Online Git book [Link].