GIT is the distributed version control system and it will help us to track changes in the source code while developing the software. The GIT is basically Designed to co-ordinate developer to finish the software development. GIT is most used and powerful among other Version Control Systems. So, in this article, we will see the GIT Basics and Beginners Guide.
Table of Contents
How to Install GIT Core On your local.
Let’s start with installing GIT on your local. Considering Mac OSX, Windows and Linux are the most popular operating system used by various developers, we will see how to install GIT on any of this operating system one by one.
- Mac OS X – Download .dmg file and install the Git core.
- Windows – Download .exe file and install.
- Linux – For Linux, we have many ways and multiple package managers are allowing us to install GIT core easily. Those are.
- Debian/Ubuntu –
apt-get install git
- Fedora – up to Fedora 21
yum install gitand for Fedora 22 and later
dnf install git
- FreeBSD –
pkg install git
- openSUSE –
zypper install git
- Debian/Ubuntu –
Concept of GIT.
As we already discussed, GIT is the distributed version control system which means the main copy of the code will be stored in a remote repository (GIT server). And, every time when the developer wants to work on the code, the developer will clone the code to the local repository (GIT client). Then the developer will create a branch from the main codebase and make changes. Once changes are done, Developer will update the code in a remote repository.
GIT Local (GIT Client)
We can say the GIT Local or GIT client as three major parts called
- The Development Working directory
- The Staging Area
- The Local Repository
The Development Working Directory
When a developer clones the codebase from the remote repository, it will be copied to the working directory of the local system. While cloning the codebase from the remote repository,
.git folder will also be cloned from the remote repository. This
.git folder is having the following folders and files inside
.git/config– File which has main settings and information of the GIT (eg., remote).
.git/HEAD– which is having information of which branch you were working on.
.git/index– This is a binary file which will act as a checkpoint of the work progress. The Index file will have the information like the list for all files, SHA-1 checksums, timestamps, and name of the files.
.git/description– File which will have the description of the repository
.git/hooks/– This is Directory which will have scripts for web-hooking.
.git/logs/– This is Directory of log files which stores all the event of the GIT
.git/refs/– A Directory contains files and its SHA-1 value of references.
All the code changes and other events of the codebase in this directory will be captured in
The Staging Area
The Staging area is a phase in GIT workflow where changes of the code will be stored in the index. The Index will carry only the changes that happened in the codebase since the last time. When you pass the add command, GIT will collect all the changes in the files and keeps it in the HEAD.
The Local Repository
Local Repository is the phase or the place where all the changes and the information regarding the changes are stored in the local repository. This local repository is been created from the remote repository which is the main copy of the codebase. This local repository is created from the remote repository by cloning though GIT client tool.
GIT Remote (GIT Server)
Remote Repository is stored in GIT remote server or centralized server, where the main copy of the code is been stored. After the commit to the local repository, Developer will send the changed code to the remote server by passing push command of GIT Client tool.
As part of the GIT Basics and Beginners Guide, we will discuss the important process flow of GIT called GIT workflow. GIT workflow is the GIT process cycle of versioning and version control. Where it starts from getting the code from the remote (master) repository to pushing back the changed code from local to the remote repository. Let’s see all in detail.
GIT clone, GIT pull and GIT Fetch
GIT clone is the process of creating a working copy of the remote or local repository by passing the following command.
git clone /path_of_repository
git clone username@git_server_hostname:/path_of_repository
If we have already cloned the repository and need to update local (only code) respect to the remote server, we need to get pull from the remote server by passing following command
git pull origin master
When in the above command, git pull is the command, the origin is the remote reference/URL of remote server and master is the branch name.
GIT fetch is the process of updating (only git information) the local GIT structure and information from remote repository. By passing the following command, we can fetch the remote repository.
Git checkout is the event of getting or changing the current state of the git branch to another. When we want to create a branch and move to the created branch, we use the following command.
git checkout -b <new_branch>
this will create a branch called
<new_branch> and current HEAD will move to the newly created branch. Which means, the changes after this command will be captured in the newly created branch.
GIT Add and GIT Commit
When we want to add the changes of code to the index of the GIT, we will pass the following command.
git add <file1> <file2> <file3>
git add *
Basically, this is the first step of the GIT workflow.
GIT commit is the event of adding the index to the HEAD of the local repository. This will be done by passing following command
git commit -m “your commit message”
But, this commit will be done only on the local repository. Which mean, we need to push the commits and updated HEAD to the remote repository.
When we commit changes to the local repository, it will have the information about changes in the codebase and its change message with it. So, if we want to update or send the changes to the remote repository, we need to pass the following command.
git push origin <new_branch>
Like we have seen in GIT Pull, the origin is the alias name of the location of the remote server. When we point the
<new_branch>, GIT push command will create a new branch in the remote server and store the changes.
This is the basic workflow, but this is not the end of the workflow. We need to merge the
<new_branch> to the master branch.
GIT Merge will merge the update in
<new_branch> to the master branch or whatever the branch we need to merge with. To merge the branch with the current branch. Checkout to the working branch and pass the following command
git merge <master_or_destination_branch>
This will merge and add the changes that are present in the
<master_or_destination_branch>. Ideally, this process will be taken care of by hosting service tools like GitHub or BitBucket.
In this GIT Basics and Beginners Guide, we have discussed the basics concept and workflow of the GIT tool. We will discuss more on GIT and advanced concepts and workflows in our upcoming articles. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.