Git basics

Submitted on Fri, 08/14/2020 - 16:47

History

GIT is a version control application which helps us to take snapshots of our file system. This becomes a very useful feature during application development process. What makes GIT unique is that it is a distributed version control system.

Linus Torvalds, lead developer of Linux Kernel is responsible for development of GIT. It was the power of GIT which propelled the development of Linux Kernel to new heights.


Theory:

Basic operation:

When a git repository (repo in short) is initialized in a folder, a folder named .git is created in the folder. The folder in which .git folder is created is identified as 'working directory'. The existing files in the directory are then identifiied as untracked files. Hence, the first thing we need to do is make GIT track these files. In order to do so, we need to push these files to 'staging area'. We can choose to add all the files to staging area or just the selected ones. 

Next, we take a snapshot of the staging area. In terms of GIT, it is called a 'commit'. After commit, the snapshot of the staging area is safely stored in a 'version database'.

If a file is modified after the commit, GIT identifies the file as modified. Now if the modified file is again pushed to the staging area, the file is shortlisted for next commit. Now the modified file is said to be staged.

Next, we take another snapshot of the staging area. Now, the version database has both versions of the modified file.

Later we can recall any version of that file from the version database to the working directory. This process is called checkout.

 

Thus we can say that Git identifies files as basically two types:

  • Untracked files: These are the files which GIT will ignore. Thus any changes made to these files are also ignored by GIT. 
  • Tracked files: These are the files which GIT will track for changes.

There are three categories of tracked files:

  • modified: ​This is the file which is modified after the last commit but yet to be staged.
  • staged: This is the file which has been modified and pushed to the staging area. Thus the file is ready to be part of the next commit.
  • commited: This is the file which is already part of a snapshot and no further modifications are made to this file.

 

We can also summarize the above mentioned processes as follows:

  • When a file is moved from working directory to staging area, the process is called stage.
  • When a file is moved from staging area to working directory, the process is called unstage.
  • When a file is moved from staging area to version database, the process is called commit.
  • When a file is moved from version database to working directory, the process is called checkout.

​We can visualize the above processes as follows:

git-lifecycle

Now, let us dive into a real scenario.


Installation

For Linux:

# apt-get install git

For Windows:

Go to the https://git-scm.com/download/win

Double click the downloaded file and proceed with the installation.

Note: In case of Windows, you can either use bash or command prompt to run GIT commands which are identical for Linux and Windows.


Scenario:

Folder:

/var/www/html/pond

Files:

/var/www/html/pond/frogs

/var/www/html/pond/snakes


Step 1. Initialize a git repository in the folder.

# git init

Output: 

Initialized empty Git repository in /var/www/html/pond/.git/

 

Step 2. Perform global configuration

# git config --global user.email apurwa@gmail.com

Here we are configuring apurwa@gmail.com as the email of the GIT author for future purpose.

 

Step 3. Check global configuration

# git config -l

Output:

user.email=apurwa@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

This command helps us verify the recent configurations. We can see that user.email parameter is set as apurwa@gmail.com.

 

Step 4. Check status of git repo

# git status

Output:

On branch master

Initial commit

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

    frogs
    snakes

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

When GIT sees a file for the first time, it is identified as untracked file.

Note: GIT provides us with hint regarding relevant commands in the given scenario. Here GIT is informing us that there are untracked files which can be tracked by using git add <file> command.

 

Step 5. Start tracking the files and check status again

# git add .

# git status

Output: 

On branch master

Initial commit

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

    new file:   frogs
    new file:   snakes

git add command is used to push files to the staging area. In this case, frogs and snakes files have been added to staging area for the first time. If we only wanted GIT to track frogs file we could have typed git add frogs. However, in this case we are adding all the files to the staging area which is marked by '.' after git add command.

 

Step 6. Take a snapshot of the folder

# git commit -m "first commit"

Output: 

[master (root-commit) 8d5e761] first commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 frogs
 create mode 100644 snakes

git commit command is used to take snapshot of the staging area. 

 

Step 7. Check log to verify the snapshot 

# git log

Output:

commit 8d5e7611dd8eb2e43078a8e7dfe56e9e3cff3e8b
Author: root <apurwa@gmail.com>
Date:   Mon Feb 20 11:57:56 2017 +0545

    first commit

git log command is used to list out all the commits. 

Step 8. Make changes to the file frogs and check status again

# vim frogs

this is the first frog.

save and exit

# git status

Output: 

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:   frogs

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

As the content of frogs file has been modified since last commit, GIT identifies the file as modified. Since the modified is yet to be pushed to staging area, the message says ... Changes not staged for commit.

 

Step 9. Add the modified file to staging area and check status again

# git add frogs

# git status

Output:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   frogs

git add command is again used to stage the modified file frogs. Hence the message, ... Changes to be committed.

Step 10. Take snapshot for the folder and check the logs

# git commit -m "changes to frogs"

Output: 

[master b0a2b9b] changes to frogs
 1 file changed, 1 insertion(+)

# git log

Output: 

commit b0a2b9b16ed8bced2e04c3f88838479d28553a34
Author: root <apurwa@gmail.com>
Date:   Mon Feb 20 12:35:47 2017 +0545

    changes to frogs

commit 8d5e7611dd8eb2e43078a8e7dfe56e9e3cff3e8b
Author: root <apurwa@gmail.com>
Date:   Mon Feb 20 11:57:56 2017 +0545

    first commit

A second snapshot of the staging area was taken. We can verify this by running the command git log.


Next: The art of undoing in GIT