Deep Dive in Git & GitHub for DevOps Engineers.

Deep Dive in Git & GitHub for DevOps Engineers.

Git & Git-hub

Git is a modern and widely used distributed version control system in the world. It is developed to manage projects with high speed and efficiency. The version control system allows us to monitor and work together with our team members at the same workspace.

What is Git ?

Git is an open-source distributed version control system. It is designed to handle minor to major projects with high speed and efficiency. It is developed to co-ordinate the work among the developers. The version control allows us to track and work together with our team members at the same workspace.

Features of Git

  • OpenSource-
    Git is an open-source tool. It is released under the GPL (General Public License) license.

  • Scalable
    Git is scalable, which means when the number of users increases, the Git can easily handle such situations.

  • Distributed
    One of Git's great features is that it is distributed. Distributed means that instead of switching the project to another machine, we can create a "clone" of the entire repository. Also, instead of just having one central repository that you send changes to, every user has their own repository that contains the entire commit history of the project. We do not need to connect to the remote repository; the change is just stored on our local repository. If necessary, we can push these changes to a remote repository.

  • Branching and Merging
    Branching and merging are the great features of Git, which makes it different from the other SCM tools. Git allows the creation of multiple branches without affecting each other. We can perform tasks like creation, deletion, and merging on branches, and these tasks take a few seconds only. Below are some features that can be achieved by branching:

    • We can create a separate branch for a new module of the project, commit and delete it whenever we want.

    • We can have a production branch, which always has what goes into production and can be merged for testing in the test branch.

    • We can create a demo branch for the experiment and check if it is working. We can also remove it if needed.

    • The core benefit of branching is if we want to push something to a remote repository, we do not have to push all of our branches. We can select a few of our branches, or all of them together.

What is GitHub?

GitHub is a Git repository hosting service. GitHub also facilitates with many of its features, such as access control and collaboration. It provides a Web-based graphical interface.

GitHub is an American company. It hosts source code of your project in the form of different programming languages and keeps track of the various changes made by programmers.

It offers both distributed version control and source code management (SCM) functionality of Git. It also facilitates with some collaboration features such as bug tracking, feature requests, task management for every project.

Git Version Control System

A version control system is a software that tracks changes to a file or set of files over time so that you can recall specific versions later. It also allows you to work together with other programmers.

The version control system is a collection of software tools that help a team to manage changes in a source code. It uses a special kind of database to keep track of every modification to the code.

Developers can compare earlier versions of the code with an older version to fix the mistakes.

Types of Version Control System

  • Localized version Control System

  • Centralized version control systems

  • Distributed version control systems

Distributed Version Control System

Centralized Version Control System uses a central server to store all the database and team collaboration. But due to single point failure, which means the failure of the central server, developers do not prefer it. Next, the Distributed Version Control System is developed.

In a Distributed Version Control System (such as Git, Mercurial, Bazaar or Darcs), the user has a local copy of a repository. So, the clients don't just check out the latest snapshot of the files even they can fully mirror the repository. The local repository contains all the files and metadata present in the main repository.

DVCS allows automatic management branching and merging. It speeds up of most operations except pushing and pulling. DVCS enhances the ability to work offline and does not rely on a single location for backups. If any server stops and other systems were collaborating via it, then any of the client repositories could be restored by that server. Every checkout is a full backup of all the data.

Git Index

The Git index is a staging area between the working directory and repository. It is used to build up a set of changes that you want to commit together. To better understand the Git index, then first understand the working directory and repository.

There are three places in Git where file changes can reside, and these are working directory, staging area, and the repository. To better understand the Git index first, let's take a quick view of these places.

Working directory:

When you worked on your project and made some changes, you are dealing with your project's working directory. This project directory is available on your computer's filesystem. All the changes you make will remain in the working directory until you add them to the staging area.

Staging area:

The staging area can be described as a preview of your next commit. When you create a git commit, Git takes changes that are in the staging area and make them as a new commit. You are allowed to add and remove changes from the staging area. The staging area can be considered as a real area where git stores the changes.

Repository:

In Git, Repository is like a data structure used by GIt to store metadata for a set of files and directories. It contains the collection of the files as well as the history of changes made to those files. Repositories in Git is considered as your project folder.

Git Repository

In Git, the repository is like a data structure used by VCS to store metadata for a set of files and directories.

Getting a Git Repository

There are two ways to obtain a repository. They are as follows:

  • Create a local repository and make it as Git repository.

  • Clone a remote repository (already exists on a server).

Git Init

The git init command is the first command that you will run on Git. The git init command is used to create a new blank repository. It is used to make an existing project as a Git project. Several Git commands run inside the repository, but init command can be run outside of the repository

The git init command creates a .git subdirectory in the current working directory. This newly created subdirectory contains all of the necessary metadata

Create a Repository for a Blank (New) Project:

To create a blank repository, open command line on your desired directory and run the init command as follows:

  1. $ git init

To create a file, run the cat or touch command.

  1. $ touch <file Name>

To add files to the repository, run the git add command.

  1. $ git add <file name>

We can list all the untracked files by git status command.\

  1. $ git status .

Git Add

The git add command is used to add file contents to the Index (Staging Area).very time we add or update any file in our project, it is required to forward updates to the staging area.

The git add command is a core part of Git technology. It typically adds one file at a time, but there some options are available that can add more than one file at once. The "index" contains a snapshot of the working tree data. This snapshot will be forwarded for the next commit.

Git add command is a straight forward command. It adds files to the staging area. We can add single or multiple files at once in the staging area. It will be run as:

  1. $ git add <File name>

Git Add All

We can add more than one files in Git, but we have to run the add command repeatedly.

  1. $ git add -A

Or

  1. $ git add .

 Removing Files from the Staging Area

The git add command is also used to remove files from the staging area. If we delete a file from the repository, then it is available to our repository as an untracked file. The add command is used to remove it from the staging area.

 Git Undo Add

We can undo a git add operation. However, it is not a part of git add command, but we can do it through git reset command. To undo an add operation, run the below command:

  1. $ git reset <filename>

Git Commit

It is used to record the changes in the repository

  1. Git commit -m “comment”

How to remove file from staging area to untracked file;

  1. Git rm –cached <file name>

Git Clone

In Git, cloning is the act of making a copy of any target repository. The target repository can be remote or local. You can clone your repository from the remote repository to create a local copy on your system

Git Clone Command

The git clone is a command-line utility which is used to make a local copy of a remote repository. It accesses the repository through a remote URL.

Syntax : $ git clone <repository URL\>

Git Clone Branch

Git allows making a copy of only a particular branch from a repository. You can make a directory for the individual branch by using the git clone command. To make a clone branch, you need to specify the branch name with -b command. Below is the syntax of the command to clone the specific git branch:

Syntax:

  1. $ git clone -b <Branch name\><Repository URL\>

Undoing changes

Git Checkout

The git checkout command is used to switch between branches in a repository.

Git Checkout

1) To demonstrate available branches in repository, use the below command:

  1. $ git branch

2) To switch between branches, use the below command.

Syntax:

  1. $ git checkout <branchname>

Create and Switch Branch

The git checkout commands let you create and switch to a new branch. You can not only create a new branch but also switch it simultaneously by a single command

Syntax:

  1. $ git checkout -b <branchname>

Git Revert

In Git, the term revert is used to revert some changes. The git revert command is used to apply revert operation. It is an undo type command.

Git Rm

In Git, the term rm stands for remove. The key function of git rm is to remove tracked files from the Git index. Additionally, it can be used to remove files from both the working directory and staging index.

the removing process can be complex, and sometimes it will not happen. But it can be done forcefully by -f option.

The git rm command

The git rm command is used to remove the files from the working tree and the index.

If we want to remove the file from our repository. Then it can be done by the git rm command.

  1. $ git rm <file Name>

The git rm command removes the file not only from the repository but also from the staging area.

How to remove file from staging area to untracked file;

  1. Git rm –cached <file name>

Undo the Git Rm Command

Execution of git rm command is not permanent; it can be reverted after execution. These changes cannot be persisted until a new commit is made on the repository. We can undo the git rm command. There are several ways to do so. The most usual and straight-forward way is git reset command. The git reset command will be used.

$ git reset HEAD

Git log

Git log command is one of the most usual commands of git. It is the most useful command for Git. Every time you need to check the history, you have to use the git log command. The basic git log command will display the most recent commits and the status of the head

  1. $ git log

Git Diff

When it is executed, it runs a diff function on Git data sources. These data sources can be files, branches, commits, and more. It is used to show changes between commits, commit, and working tree

Scenerio1: Track the changes that have not been staged.

The usual use of git diff command that we can track the changes that have not been staged

Scenerio2: Track the changes that have staged but not committed:

The git diff command allows us to track the changes that are staged but not committed. We can track the changes in the staging area.

  1. $ git diff --staged

Scenerio3: Track the changes after committing a file:

$ git diff HEAD

Git Diff Branches

Git allows comparing the branches. If you are a master in branching, then you can understand the importance of analyzing the branches before merging. Many conflicts can arise if you merge the branch without comparing it. So to avoid these conflicts, Git allows many handy commands to preview, compare, and edit the changes.

  1. $ git diff <branch 1> < branch 2>

Git Status

The git status command is used to display the state of the repository and staging area. It allows us to see the tracked, untracked files and changes. This command will not show any commit records or information.

Mostly, it is used to display the state between Git Add and Git commit command. We can check whether the changes and files are tracked or not.

Git Branch

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes.

Git Master Branch

The master branch is a default branch in Git. It is instantiated when first commit made on the project. When you make the first commit, you're given a master branch to the starting commit point

Operations on Branches

The git branch command allows you to create, list, rename and delete branches. Many operations on branches are applied by git checkout and git merge command. So, the git branch is tightly integrated with the git checkout and git merge commands

Create Branch

$ git branch <branch name\>

List Branch :

$ git branch --list

Delete Branch:

You can delete the specified branch. It is a safe operation. In this command, Git prevents you from deleting the branch if it has unmerged changes.

  1. $ git branch -d<branch name\>

Delete a Remote Branch

you can delete a remote branch from Git desktop application.

$ git push origin -delete <branch name\>

Switch Branch:

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command

  1. $ git checkout<branch name\>

Rename Branch

We can rename the branch with the help of the git branch command.

$ git branch -m <old branch name\><new branch name\>

Merge Branch

Git allows you to merge the other branch with the currently active branch

  1. $ git merge <branch name\>

Git Merge Conflict

Git Fork: -

A fork is a rough copy of a repository.

When to Use Git Fork

Generally, forking a repository allows us to experiment on the project without affecting the original project

Fork vs. Clone

Both commands are used to create another copy of the repository. But the significant difference is that the fork is used to create a server-side copy, and clone is used to create a local copy of the repository.