Git tutorial for beginners

In this tutorial, we will learn about Git, the most popular source code management and distributed version control system.

The main features of Git are different users or groups can maintain their own repositories, instead of working from a central repository. In Git the changes are stored as “change set ” or “patches”.

The best way to learn about a new tool or technology is to install it in your machine and start working on it.

Git installation on Linux machine.

Here I am installing Git on CentOS7 platform.

Execute the below command to install Git.

# yum install git

And to check the installed git version run below command

[root@techiescorner ~]# git --version
git version

Now we have installed git, in-order to work with this we need to configure it.

System-level configuration 

This configuration applies to every user in the system. This configuration detail is stored in the /etc/gitconfig file

User level configuration

This applies to a single user. So the configuration details will be in user home directory ~/.gitconfig.

Before starting user configuration I have created a new user (techies) for my system, to perform execute below command.

[root@techiescorner ~]# useradd techies
[root@techiescorner ~]# su - techies
Last login: Fri Feb 15 09:50:30 EST 2019 on pts/0
[techies@techiescorner ~]$

Let’s start the user level configuration.

[techies@techiescorner ~]$ git config --global "Techies Corner"
[techies@techiescorner ~]$ git config --global "admin@techiescorner"
[techies@techiescorner ~]$ git config --list Corner
[techies@techiescorner ~]$

All these details are stored in the user home directory.

[techies@techiescorner ~]$ ls -la
total 20
drwx------. 2 techies techies 101 Feb 15 09:56 .
drwxr-xr-x. 4 root    root     36 Feb 15 09:50 ..
-rw-------. 1 techies techies  63 Feb 15 09:52 .bash_history
-rw-r--r--. 1 techies techies  18 Apr 10  2018 .bash_logout
-rw-r--r--. 1 techies techies 193 Apr 10  2018 .bash_profile
-rw-r--r--. 1 techies techies 231 Apr 10  2018 .bashrc
-rw-rw-r--. 1 techies techies  59 Feb 15 09:56 .gitconfig
[techies@techiescorner ~]$ cat .gitconfig
        name = Techies Corner
        email = admin@techiescorner
[techies@techiescorner ~]$

These are the minimum configuration needs to work with git.

Code editor configuration

used to set default text editor for git

[techies@techiescorner ~]$ git config --global core.editor "vim"

Global color configuration

By enabling this the git output will be displayed in different color. It helps to understand git output easily.

[techies@techiescorner ~]$ git config --global color.ui true

Yes, we have installed and configured git, the next step is to initialize Git. To do this I am creating a new directory named “git-project”. To init Git execute below command

[techies@techiescorner git-project]$ git init
Initialized empty Git repository in /home/techies/git-project/.git/
[techies@techiescorner git-project]$

Now we set /home/techies/git-project/.git/ as our homebase or this is the Git repository and track all the files that come and go. The ‘.git’ directory is used to store all tracking information.

To understand the working of Git we will create a file called file1.txt with following content in the git-project directory.

“This is the first file”

Now execute git add . ( Don’t miss the dot) to add all the changes that we made inside the directory, that is our project.

[techies@techiescorner git-project]$ cat file.txt
This is the first file
[techies@techiescorner git-project]$ git add .

Then perform the commit to save it in the repository.

[techies@techiescorner git-project]$ git commit -m "First commit"
[master (root-commit) 4564c31] First commit
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
[techies@techiescorner git-project]$

-m stand for the message. Add some meaning full comment to understand it later, example support ticket number or bug tracking number. Here you can see we made 1 file change and 1 insertion.

Git commit log

git log command will help us to see the log of “git commit” that we just performed.

[techies@techiescorner git-project]$ git log
commit 4564c3181227e997cdb85acfee7a9a28d6d0babf
Author: Techies Corner <admin@techiescorner>
Date: Fri Feb 15 12:05:43 2019 -0500

First commit
[techies@techiescorner git-project]$

From the logs

commit: This is the commit ID, it is a unique code

Author: Who performed the commit

Date: Date and time, when the commit performed.

In the next page, we will understand about Git architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *