Git (1/6)

Introduction

Git is a distributed version control system. Its website is here.

Installing Git

Windows installation

Git was initially developed for use on Linux but it is now perfectly usable on Windows. There are many options available to use Git on Windows. In this tutorial we focus on using Git from the command line using Git Bash. GUI clients are also available but we won't dicuss them here (for now).

You can get Git Bash from the download page on the Git website: http://git-scm.com/downloads. If you simply keep the default options while running the installation package Git Bash will be installed. You could also get it directly from its creators at http://msysgit.github.io/index.html. The msysGit project's goal is to provide the tools to use Git on Windows and as part of that effort they created Git Bash.

After installation Git Bash should be available from the Start menu.

Creating a repository and adding files to it

Now that Git Bash is installed we are ready to create our first repository. We have chosen to place the new repository in the c:\GitRepositories\MyFirstRepository path.

We start Git Bash and create the c:\GitRepositories\MyFirstRepository directory. We then use the git init command to create the repository. The full sequence of commands is shown below.

Creating a new repository
Xavier Leclercq@PATHFINDER ~
$ cd /c

Xavier Leclercq@PATHFINDER /c
$ mkdir GitRepositories

Xavier Leclercq@PATHFINDER /c
$ cd GitRepositories/

Xavier Leclercq@PATHFINDER /c/GitRepositories
$ mkdir MyFirstRepository

Xavier Leclercq@PATHFINDER /c/GitRepositories
$ cd MyFirstRepository/

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository
$ git init
Initialized empty Git repository in c:/GitRepositories/MyFirstRepository/.git/

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

As a result of the git init command a subdirectory named .git has been created in c:\GitRepositories\MyFirstRepository. This is the Git repository and you should obviously not interfere with it unless you know what you are doing.

Some commands need to know the details of the user working with this repository. We use the following commands to set these details.

Setting the user details
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git config user.name "Xavier Leclercq"

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git config user.email "xavier.leclercq@needfulsoftware.com"

The working directory and the staging area

The working directory is the directory in which the files you work on are placed. When working with Git this is simply the directory in which the repository resides (c:\GitRepositories\MyFirstRepository in our case).

With most version control systems you would except the working directory to contain the changes that are going to be committed to the repository. This is not strictly true with Git. Git introduces the concept of a staging area. Changes you want to commit to the repository must be added to the staging area first. From there they can then be committed to repository. The staging area keeps a complete version of the changes added to it. A developer can therefore add changes to the staging area and then continue to work in the working directory. The changes added to the staging area will not be impacted by the work in the working directory until the developer decides to add those extra changes to the staging area too. This just gives extra flexibility to the developer to organize his work.

This is probably a bit confusing but we will look into this in more details right now.

At the moment the working directory for our example repository, MyFirstRepository, is empty since we just created it (except for the .git subdirectory). Let's now add a file to our repository. We simply add it directly to the c:\GitRepositories\MyFirstRepository directory since that is our working directory. Let's call it "HelloWorld.txt" and leave it empty for now.

If we use the git status command we get the following result.

Adding a new file
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        HelloWorld.txt

nothing added to commit but untracked files present (use "git add" to track)

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

Git informs you that there is an untracked file present. An untracked file is a file that was added to the working directory but not added to the staging area or committed to the repository yet. The git status command also informs you that nothing has been added to commit which means that the staging area is currently empty.

We would like to commit the new file to the repository. To do so we will first need to add it to the staging area and then commit the changes in the staging area.

We use the git add command to add the file to the list of changes to commit and then do git status to show the current state.

Adding a new file
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git add HelloWorld.txt

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   HelloWorld.txt


Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

The git add command added our new file to the staging area. As you can see from the git status output the file is not untracked anymore and is listed in the list of changes to be committed i.e. the contents of the staging area.

We now use the git commit command to commit our new file to the repository.

Committing the changes
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git commit -m "Initial commit"
[master (root-commit) fc7752f] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 HelloWorld.txt

The git log command can be used to show the list of changes made to the repository as shown below.

Viewing the commit log
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git log
commit fc7752fd9a3c2cd6985e24c9cbed4a1f1137d633
Author: Xavier Leclercq <xavier.leclercq@needfulsoftware.com>
Date:   Sat Jul 12 17:30:57 2014 +0100

    Initial commit

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

For reference if we hadn't set the user details previously with git config the commit would have failed with the following error message.

Trying to commit without user details
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git commit -m "Initial commit"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'Xavier Leclercq@Pathfinder.(non
e)')

Editing files

Now that we have a file in the repository let's make a change to it. Open the HelloWorld.txt file in a text editor and modify it as follows.

File: c:\GitRepositories\MyFirstRepository\HelloWorld.txt
Hello World!

As was the case when we added the file the changes are not added to the staging area until we use git add. If we use the git status command before doing so it will say that HelloWorld.txt has been modified but that no changes have been added to the list of changes to commit i.e. the staging area. This is shown below.

Editing a file
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ 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:   HelloWorld.txt

no changes added to commit (use "git add" and/or "git commit -a")

We can also use the git diff command to show the changes made to the working directory but not yet added to the staging area. This is a significant difference between Git and most other version control systems. By default git diff will not show the differences between the working directory and the repository but between the working directory and the staging area. This means that changes already added to the staging area are not displayed by git diff.

The output of git diff is shown below.

git diff output
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git diff
diff --git a/HelloWorld.txt b/HelloWorld.txt
index e69de29..c57eff5 100644
--- a/HelloWorld.txt
+++ b/HelloWorld.txt
@ -0,0 +1 @
+Hello World!
\ No newline at end of file

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

Now we use the git add command to add the changes to the staging area as shown below.

Adding the changes to the staging area
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git add HelloWorld.txt

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   HelloWorld.txt

git status now shows that the changes have been added to the staging area.

If we execute the git diff command the result will be empty since there are no differences between the working directory and the staging area. To get the list of differences between the staging area and the repository we use the git diff --staged command. To get the list of differences between the working directory and the repository we use the git diff HEAD command. In our case git diff --staged and git diff HEAD give identical results since the working directory and the staging area are identical i.e. there are no changes in the working directory that have not been added to the staging area.

The results of the diff command with these various options are shown below.

git diff results
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git diff

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git diff --staged
diff --git a/HelloWorld.txt b/HelloWorld.txt
index e69de29..c57eff5 100644
--- a/HelloWorld.txt
+++ b/HelloWorld.txt
@ -0,0 +1 @
+Hello World!
\ No newline at end of file

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git diff HEAD
diff --git a/HelloWorld.txt b/HelloWorld.txt
index e69de29..c57eff5 100644
--- a/HelloWorld.txt
+++ b/HelloWorld.txt
@ -0,0 +1 @
+Hello World!
\ No newline at end of file

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

And as before we can now use the git commit command to commit our change to the repository as shown below.

Committing the changes
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git commit -m "Update to HelloWorld.txt"
[master cc1c653] Update to HelloWorld.txt
 1 file changed, 1 insertion(+)

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

The log now show both the initial commit and the subsequent change.

Viewing the commit log
Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$ git log
commit cc1c6530eab31c8411c7555fe75e0f850a240a2e
Author: Xavier Leclercq <xavier.leclercq@needfulsoftware.com>
Date:   Sat Nov 29 20:00:59 2014 +0000

    Update to HelloWorld.txt

commit fc7752fd9a3c2cd6985e24c9cbed4a1f1137d633
Author: Xavier Leclercq <xavier.leclercq@needfulsoftware.com>
Date:   Sat Jul 12 17:30:57 2014 +0100

    Initial commit

Xavier Leclercq@PATHFINDER /c/GitRepositories/MyFirstRepository (master)
$

Further Reading

  1. vogel/a: Git - Tutorial

blog comments powered by Disqus

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search