Git for an absolute beginners

Before I started using Git on my personal and then on commercial projects, I was using for many years  Subversion – svn in short. SVN is still very popular but Git earned its strong position as well by being used in large and small projects for more than 10 years already.

Installing Git

The official website of Git has detailed information about installing on Linux, Mac, or Windows. In this case, I will be using Ubuntu 16.04 for demonstration purposes, and installing git in Linux (Ubuntu) is simple as typing:

marom@marom-E7440:~$ sudo apt-get install git

Before we dive into configuration and will start using Git a bit of terminology introduction is necessary.

Here’s the git terminology:

  • master – the repository’s main branch. Depending on the workflow it is the one people work on or the one where the integration happens
  • clone – copies an existing git repository, normally from some remote location to your local environment.
  • commit – submitting files to the repository (the local one); in other VCS it is often referred to as “checkin”
  • fetch or pull – is like “update” or “get latest” in other VCS. The difference between fetch and pull is that pull combines both, fetching the latest code from a remote repo as well as performs the merging.
  • push – is used to submit the code to a remote repository
  • remote – these are “remote” locations of your repository, normally on some central server.
  • SHA – every commit or node in the Git tree is identified by a unique SHA key. You can use them in various commands in order to manipulate a specific node.
  • head – is a reference to the node to which our working space of the repository currently points.
  • branch – is just like in other VCS with the difference that a branch in Git is actually nothing more special than a particular label on a given node. It is not a physical copy of the files as in other popular VCS.

Initial Configuration

We need to configure our name and email. You can do it as follows, replacing the values with your own name and email.

marom@marom-E7440:~$ git config --global user.name "Courious Programmer"
marom@marom-E7440:~$ git config --global user.email "blog@programmerscuriosity.com"

It is important to note that if you do not set your name and email, certain default values will be used.

Create a new Git Repository

Before starting, let’s create a new directory where the git repository will be created and ‘move’ into it.

marom@marom-E7440:~/SmallProjects$ mkdir blogGitIntroduction
marom@marom-E7440:~/SmallProjects$ cd blogGitIntroduction/
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ 

Now we can initialize our first git repository.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git init
Initialized empty Git repository in /home/marom/SmallProjects/blogGitIntroduction/.git/

To check the status of our repository git status command can be used.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)

Create and commit a new file

The next step is to create a new file and add some content to it.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ touch blogpost.txt

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ echo "Hello, Git World!" > blogpost.txt

Let’s check what happen in our git repository.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git status
On branch master

Initial commit

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

	blogpost.txt

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

To “register” the file for committing we need to add it to git using:

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git add blogpost.txt 

And again, let’s check what happen in our git repository.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git add secondFile.txt 
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -m "add second file to the project"
[master 763b757] add second file to the project
 1 file changed, 1 insertion(+)
 create mode 100644 secondFile.txt

 

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git status
On branch master

Initial commit

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

	new file:   blogpost.txt

We can now commit it to the repository.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -m "add first file to the project"
[master (root-commit) 55db76b] add first file to the project
 1 file changed, 1 insertion(+)
 create mode 100644 blogpost.txt

Let’s add another file to the project.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ echo "this is text from second file in project" > secondFile.txt
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git add secondFile.txt 
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -m "add second file to the project"
[master 763b757] add second file to the project
 1 file changed, 1 insertion(+)
 create mode 100644 secondFile.txt

 

Create a (feature) branch

Branching and merging is what makes Git so powerful and for what it has been optimised, being a distributed version control system (VCS). Indeed, feature branches are quite popular to be used with Git. Feature branches are created for every new kind of functionality you’re going to add to your system and they have usually deleted afterwards once the feature is merged back into the main integration branch (normally the master branch). The advantage is that you can experiment with new functionality in a separate, isolated “playground” and quickly switch back and forth to the original “master” branch when needed. Moreover, it can be easily discarded again (in case it is not needed) by simply dropping the feature branch. But let’s get started and create the new feature branch:

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git branch new-feature-branch

To see a list of all branches we need to use git branch command.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git branch
* master
  new-feature-branch

A star next to master branch indicates that this is a current branch.

Let’s switch to this newly created branch and modify one of the files.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git checkout new-feature-branch 
Switched to branch 'new-feature-branch'
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git branch
  master
* new-feature-branch
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ echo "Let's modify file in the branch" >> blogpost.txt
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ cat blogpost.txt 
Hello, Git World!
Let's modify file in the branch

And then add and commit it to the branch.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git add blogpost.txt 
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -m "modify file on the branch"
[new-feature-branch 3a30916] modify file on the branch
 1 file changed, 1 insertion(+)

 

Ok, it’s time to complicate it and cause some conflicts. To do this we need to switch to ‘master’ branch and modify the blogpost.txt file.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git checkout master
Switched to branch 'master'
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git branch
* master
  new-feature-branch
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ echo "This change was done on master branch" >> blogpost.txt
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ cat blogpost.txt 
Hello, Git World!
This change was done on master branch

 

If you need to commit a file that was already added to the git repository you can use command ‘commit -a -m’

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -a -m "change blogpost file"
[master 1a139b5] change blogpost file
 1 file changed, 1 insertion(+)

Merge and resolve conflicts

The next step would be to merge our feature branch back into ‘master’. This is done by using the merge command.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git merge new-feature-branch 
Auto-merging blogpost.txt
CONFLICT (content): Merge conflict in blogpost.txt
Automatic merge failed; fix conflicts and then commit the result.
marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ cat blogpost.txt 
Hello, Git World!
<<<<<<< HEAD
This change was done on master branch
=======
Let's modify file in the branch
>>>>>>> new-feature-branch

Edit file and resolve conflict and commit changes to the branch.

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ cat blogpost.txt 
Hello, Git World!

This change was done on master branch

Let's modify file in the branch

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git commit -a -m "resolve merge conflict"
[master 7e2abc0] resolve merge conflict

 

Rollback

What if we want to undo everything back to the state before the commit?

First, we need to get SHA identifiers (unique ‘id’ for each node in git tree).

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git log
commit 4afb2d5caf73f48be16cc78875b8fe58c72ce4dd
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 10:02:16 2016 +0200

    prepare for rollback

commit 7e2abc0dc4f56672bf0f209551698c942dbc7071
Merge: 1a139b5 3a30916
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:57:50 2016 +0200

    resolve merge conflict

commit 1a139b5dd7a9f42e4f7f570081b65dbc4d27529b
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:37:40 2016 +0200

    change blogpost file

commit 3a309165db700a00fbe1a0f2451700c59532d629
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:26:56 2016 +0200

 

and then move back in time to the desired node/commit

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git reset --hard 7e2abc0
HEAD is now at 7e2abc0 resolve merge conflict

 

marom@marom-E7440:~/SmallProjects/blogGitIntroduction$ git log
commit 7e2abc0dc4f56672bf0f209551698c942dbc7071
Merge: 1a139b5 3a30916
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:57:50 2016 +0200

    resolve merge conflict

commit 1a139b5dd7a9f42e4f7f570081b65dbc4d27529b
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:37:40 2016 +0200

    change blogpost file

commit 3a309165db700a00fbe1a0f2451700c59532d629
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sun Sep 11 09:26:56 2016 +0200

    modify file on the branch

commit 763b75761c9d95a68d1d1e56bd4d2b0390c03379
Author: Courious Programmer <blog@programmerscuriosity.com>
Date:   Sat Sep 10 19:13:28 2016 +0200

 

And that’s it. It is everything that is needed to start using git. I have covered here only simple commands. For more advanced commands and explanations I highly recommend reading this book: Pro GIT.

Leave a Reply