GIT Introduction and Basic Commands

What is GIT?

Git is a free, open source distributed version control system tool designed to handle everything from small to very large projects with speed and efficiency. It was created by Linus Torvalds in 2005 to develop Linux Kernel. Git has the functionality, performance, security and flexibility that most teams and individual developers need.

Version Control is the management of changes to documents, computer programs, large websites and other collection of information.

There are two types of VCS:

  • Centralized Version Control System (CVCS)
  • Distributed Version Control System (DVCS)

Centralized VCS:

Centralized version control system (CVCS) uses a central server to store all files and enables team collaboration. It works on a single repository to which users can directly access a central server.

Please refer to the diagram below to get a better idea of CVCS:


The repository in the above diagram indicates a central server that could be local or remote which is directly connected to each of the programmer’s workstation.

Every programmer can extract or update their workstations with the data present in the repository or can make changes to the data or commit in the repository. Every operation is performed directly on the repository.

Even though it seems pretty convenient to maintain a single repository, it has some major drawbacks. Some of them are:

  • It is not locally available; meaning you always need to be connected to a network to perform any action.
  • Since everything is centralized, in any case of the central server getting crashed or corrupted will result in losing the entire data of the project.

This is when Distributed VCS comes to the rescue.

Distributed VCS

These systems do not necessarily rely on a central server to store all the versions of a project file.

In Distributed VCS, every contributor has a local copy or “clone” of the main repository i.e. everyone maintains a local repository of their own which contains all the files and metadata present in the main repository.

You will understand it better by referring to the diagram below:


As you can see in the above diagram, every programmer maintains a local repository on its own, which is actually the copy or clone of the central repository on their hard drive. They can commit and update their local repository without any interference.

They can update their local repositories with new data from the central server by an operation called “pull” and affect changes to the main repository by an operation called “push” from their local repository.

The act of cloning an entire repository into your workstation to get a local repository gives you the following advantages:

  • All operations (except push & pull) are very fast because the tool only needs to access the hard drive, not a remote server. Hence, you do not always need an internet connection.
  • Committing new change-sets can be done locally without manipulating the data on the main repository. Once you have a group of change-sets ready, you can push them all at once.
  • Since every contributor has a full copy of the project repository, they can share changes with one another if they want to get some feedback before affecting changes in the main repository.
  • If the central server gets crashed at any point of time, the lost data can be easily recovered from any one of the contributor’s local repositories.

What Is Git?

GIT is the most widely used open-source VCS (version control system) that allows you to track changes made to files. Companies and programmers usually use GIT to collaborate on developing software and applications.

A GIT project consists of three major sections: the working directory, the staging area, and the git directory.

The working directory is where you add, delete, and edit the files. Then, the changes are staged (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved into the git directory.

Everyone can use GIT as it is available for Linux, Windows, Mac, and Solaris.

Features of Git:

  • Free and open source
  • Speed
  • Scalable
  • Reliable
  • Secure
  • Economical
  • Supports non-linear development
  • Easy Branching
  • Distributed development
  • Compatibility with existing systems or protocol

Basic Commands:

  • git config

Used to set user-specific configuration values like email, username, file format, and so on. To illustrate, the command for setting up an email will look like this:

git config --global

The –global flag tells GIT that you’re going to use that email for all local repositories. If you want to use different emails for different repositories, use the command below:

git config --local
  • git init

Creates new local GIT repository. The following Git command will create a repository in the current directory:

git init

Alternatively, you can create a repository within a new directory by specifying the project name:

git init [project name]
  • git clone

Used to copy a repository. If the repository lies on a remote server, use:

git clone username@host:/path/to/repository

Conversely, run the following basic Git command to copy a local repository:

git clone /path/to/repository
  • git add

used to add files to the staging area. For example, the basic Git following command will index the temp.txt file:

git add temp.txt

Below command adds a file to the staging area:

git add [file]

Below command adds one or more files to the staging area.

git add *
  • git commit

Creates a snapshot of the changes and save it to the git directory.

git commit –m “Message to go with the commit here”

Note that any committed changes won’t make their way to the remote repository.

  • git diff

Used to show differences between files.

Below command shows the file differences which are not yet staged:

git diff

Below command shows the differences between the files in the staging area and the latest version present:

git diff -staged
  • git reset

Below command unstages the file, but it preserves the file contents.

git reset [file]

This command undoes all the commits after the specified commit and preserves the changes locally:

git reset [commit]

Below command discards all history and goes back to the specified commit.

git reset –hard [commit]
  • git status

This command lists all the files that have to be committed.

git status
  • git rm

This command deletes the file from your working directory and stages the deletion.

git rm [file]
  • git log

This command is used to list the version history for the current branch.

git log

Below command lists version history for a file, including the renaming of files also.

git log –follow[file]
  • git show

This command shows the metadata and content changes of the specified commit.

git show [commit]
  • git tag

This command is used to give tags to the specified commit.

git tag [commitID]
  • git branch

This command lists all the local branches in the current repository.

git branch

Below command creates a new branch:

git branch [Branch Name]

Below command deletes the feature branch:

git branch -d [Branch Name]
  • git checkout

This command is used to switch from one branch to another.

git checkout [Branch Name]

Below command creates a new branch and also switches to it.

git checkout -b [Branch Name]
  • git merge

This command merges the specified branch’s history into the current branch.

git merge [Branch Name]
  • git remote

This command is used to connect your local repository to the remote server.

git remote add [variable name] [Remote Server Link]
  • git push

Below command sends the committed changes of master branch to your remote repository:

git push [variable name] master

Below command sends the branch commits to your remote repository:

git push [variable name] [branch]

Below command pushes all branches to your remote repository:

git push -all [variable name]

Below command deletes a branch on your remote repository:

git push [variable name] : [brach name]
  • git pull

This command fetches and merges changes on the remote server to your working directory.

git pull [Repository Link]
  • git stash

Below command temporarily stores all the modified tracked files:

git stash save

Below command restores the most recently stashed files:

git stash pop

This command lists all stashed changes.

git stash list

Below command discards the most recently stashed changeset:

git stash drop
  • gitk

This command shows the graphical interface for a local repository. Simply run: gitk

  • git gc

Cleans unnecessary files and optimizes the local repository.

git gc
  • git prune

Deletes objects that don’t have any incoming pointers.

git prune
  • git fsck

Performs an integrity check of the git file system and identifies any corrupted objects.

git fsck
  • git rebase

Used to apply certain changes from one branch to another.

git rebase master

You may also be interested in...

Article By Kamlesh Bhor

Feel free to comment below about this article.

Discuss about post

Subscribe to my weekly newsletter