An introduction to version control systems using git.
This tutorial is born out of a session I took recently. The class
had mostly final year IT students under Anna University, Coimbatore.
They will be doing a project for the next few months. The session was
very basic and lasts less than 30 minutes. It is meant for students
who are totally new to the concept of version control and
collaborative development.
Aim: Introduce version control to students who have no prior
knowledge, in less than 30 minutes.
Prerequisites:
1.git should be installed
2.Familiarity with linux command line.
Version Control System ?
Projects are generally done as a team consisting of two or more
members. Based on role each member may add or modify a part of the
software. Keeping track of all the changes done and ultimately
producing a working version requires a lot of effort. Version Control
Systems help us in this task.
Version Control in simple terms means keeping track of the changes
done to file(s), with features like
- Show Log of changes
- Show differences
- Facility to revert the changes
- Allow multiple users to maintain their own repository with
features to merge them.
Let us start
Create a project directory using mkdir command
ram@eluvaram:~$ mkdir a1
Change to the directory
ram@eluvaram:~$ cd a1
Initialize a git repository
ram@eluvaram:~/a1$ git init
Initialized empty Git repository in /home/ram/a1/.git/
Run ls to see files
ram@eluvaram:~/a1$ ls -l
total 0
No file is seen. Run ls with -a option to show hidden file
ram@eluvaram:~/a1$ ls -la
total 20
drwxr-xr-x 3 ram ram 4096 Aug 21 16:53 .
drwxr-xr-x 77 ram ram 12288 Aug 21 16:53 ..
drwxr-xr-x 7 ram ram 4096 Aug 21 16:53 .git
ram@eluvaram:~/a1$
From the above you can observe that git has created a directory
name .git.
Adding a file to git
Open an editor, create a file and save it in this directory. You
can use any editor. I will be using vi.
ram@eluvaram:~/a1$ vi first.pl
The content of the file is (using cat command)
ram@eluvaram:~/a1$ cat first.pl
print "Welcome to git\n";
ram@eluvaram:~/a1$
To add this file i.e first.pl to git we should use git add command
like this
ram@eluvaram:~/a1$ git add first.pl
After running this command git starts keeping track of the file.
Checking status of the repository.
We can use git status to check status of our repository.
ram@eluvaram:~/a1$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: first.pl
#
You can see from the above that git tells us that file first.pl is
a new file and not yet committed.
Committing a change
Commit means updating git repository with changes we have done.
While committing a message should be entered. To commit a change
issue git commit like this
ram@eluvaram:~/a1$ git commit
The command will open an editor where commit messages can be typed like this
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
#
# Committer: ram
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: first.pl
#
~
We can type the message and save. Once saved git displays
information about the commit.
[master (root-commit) 6c7c12e] This is the first file I have committed to git
Committer: ram
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config –global user.name “Your Name”
git config –global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit –amend –author=’Your Name ‘
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 first.pl
ram@eluvaram:~/a1$
Now git status will display like this
ram@eluvaram:~/a1$ git status
# On branch master
nothing to commit (working directory clean)
Modifying a tracked file and committing the changes.
In the previous section we added a new file. Now edit the file by
adding few lines. I have added two lines to the file. I have also
modified the first line as ‘Welcome to git – the Version
control System’. Now run git status
ram@eluvaram:~/a1$ vi first.pl
ram@eluvaram:~/a1$ git status
# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: first.pl
#
no changes added to commit (use "git add" and/or "git commit -a")
ram@eluvaram:~/a1$
As you can see git found out that first.pl has been modified.
To know what is the difference, we can use git diff.
ram@eluvaram:~/a1$ git diff
diff --git a/first.pl b/first.pl
index 626893a..dba4b3b 100644
--- a/first.pl
+++ b/first.pl
@@ -1 +1,3 @@
-print "Welcome to git\n";
+print "Welcome to git - the version control system\n";
+print "Vanakkam \n";
+print "Swagatham\n";
Now can commit the changes using git commit. As in previous
example git will open an editor for typing the commit message.
ram@eluvaram:~/a1$ git commit first.pl
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
#
# Committer: ram
#
# On branch master
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
# modified: first.pl
#
Committer: ram
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name '
1 files changed, 3 insertions(+), 1 deletions(-)
Keeping track of changes – git log
git log will give us all commits that have taken place.
ram@eluvaram:~/a1$ git log
commit 44ccd72b6dae0405a98610e9bfc0d1e7e9336d1d
Author: ram
Date: Sun Aug 21 19:56:32 2011 +0530
Modified the first line, added two new lines
commit 6c7c12ed89f23fd8d9131648cc4fc4aad493e052
Author: ram <ram@eluvaram.(none)>
Date: Sun Aug 21 18:57:25 2011 +0530
This is the first file I have commited to git
COLLABORATIVE DEVELOPMENT
If another user joins the project the new user can clone this
repository and start addition/modification. This user can work in the
same system or on a remote system. For simplicity we can create a new
directory and use it.
ram@eluvaram:~/a1$ cd ..
ram@eluvaram:~$ mkdir a2
ram@eluvaram:~$ cd a2
Copying project files from main repository can be done using git
clone. This will create a local copy of the repository.
ram@eluvaram:~/a2$ git clone ../a1
Cloning into a1...
done.
ram@eluvaram:~/a2$
After this git will create a directory a1 under current directory
i.e a2. The new user can use the directory a2/a1 for further
development. You can check the contents by using ls command.
ram@eluvaram:~/a2$ cd a1
ram@eluvaram:~/a2/a1$ ls -la
total 16
drwxr-xr-x 3 ram ram 4096 Aug 28 09:31 .
drwxr-xr-x 3 ram ram 4096 Aug 28 09:31 ..
-rw-r--r-- 1 ram ram 97 Aug 28 09:31 first.pl
drwxr-xr-x 8 ram ram 4096 Aug 28 09:31 .git
ram@eluvaram:~/a2/a1$
As you can see above the file first.pl is copied in the new
directory.
Adding a new file in the cloned repository.
The cloned repository is an independent one. Hence, the adding,
committing etc., can be done just as in previous examples.
To learn let us create a new file second.pl in the a2/a1
repository. Use any editor and create the file. Use git-add and
git-commit . This time we will use -m flag to give the commit flag in
the command line itself.
ram@eluvaram:~/a2/a1$ git add second.pl
ram@eluvaram:~/a2/a1$ git commit -m 'This is my second file' second.pl
[master 3673823] This is my second file
Committer: ram
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name '
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 second.pl
ram@eluvaram:~/a2/a1$
Now using git-status we can check status of this repository.
ram@eluvaram:~/a2/a1$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
ram@eluvaram:~/a2/a1$
Notice from the above that this new repository is ahead of
original one by 1 commit.
Changing an existing file
Now we will make changes to first.pl in this repository. Open the
file first.pl and add following line
print "This is a line added from a2/a1 \n";
As usual commit changes.
ram@eluvaram:~/a2/a1$ git commit -m 'changed first.pl' first.pl
[master 77de400] changed first.pl
Committer: ram
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name '
1 files changed, 1 insertions(+), 0 deletions(-)
ram@eluvaram:~/a2/a1$
MERGING CHANGES
The important and interesting part of git is merging the changes
done by a1 and a2. Before doing let us just review the status of both
repository.
|
FILE/Details
|
Repository a1
|
Repository a2/a1
|
Comments
|
|
first.pl
|
Available and has only three
line
|
Available. Has four lines
|
Fourth line was added by a2
|
|
second.pl
|
Not available
|
Available.
|
Created exclusively in a2
|
|
Total number of files
|
1
|
2
|
|
Now let us update repository a1 with all the changes done by a2.
Pull operation can be used to get changes done by a2.
First change to a1 and run command git-pull.
ram@eluvaram:~/a2/a1$ cd ~/a1
ram@eluvaram:~/a1$ git pull ../a2/a1
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From ../a2/a1
* branch HEAD -> FETCH_HEAD
Updating 44ccd72..77de400
Fast-forward
first.pl | 1 +
second.pl | 1 +
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 second.pl
ram@eluvaram:~/a1$
Now git pulls changes from a2 and merges with a1. As you can see
above 2 files were changed.
Now run ls -l and cat to check files and their contents
ram@eluvaram:~/a1$ ls -l
total 8
-rw-r--r-- 1 ram ram 139 Aug 28 10:19 first.pl
-rw-r--r-- 1 ram ram 42 Aug 28 10:19 second.pl
ram@eluvaram:~/a1$ cat first.pl
print "Welcome to git - the version control system\n";
print "Vanakkam \n";
print "Swagatham\n";
print "This isa line added from a2/a1\n";
ram@eluvaram:~/a1$ cat second.pl
print "this is my second perl program\n";
ram@eluvaram:~/a1$
In the present case there is no conflict between changes and hence
git committed them all without any message. When merger has conflict
– say same line modified by both – git will flag them and
let us resolve and then commit.
PDF Version of this document
Licence: This document is released under Creative Commons-By Attribution-Share alike