Git (Distributed Version Control)

Introduction:-

Git is a distributed version control system. It is open-source and was developed by Linus Torvalds. It provides support for multiple platforms like macOS, Windows, and Linux.

Features of Git:-

#Git is distributed so have the following benefits.

✔No single point of failure because every developer has a local repository.

✔performance is more because checkout and comment operations are performed locally.

✔Developers can continue their work even without a network because they don't need to connect always to the remote repository.

#Staging Area. (also known as index, cache, logical, virtual area)

✔Git has a very good feature called Staging area other version control does not have this Staging area which has many benefits.

✔In between the working directory and local repository there is one special layer or virtual area to store our files before committing that place is nothing but a Staging area. The advantage of stating area is that we can cross-check or double-check our changes before committing. if something goes wrong we can remove the file from the Staging area to the working directory and again debug.

✔12 GB file we have to store in SVN but git automatically does hashing or takes snapshots of data by Cryptography to reduce the file size. (SHA-1 hash function is used in Git.)

#Branching and merging:-

✔We can create and work on multiple branches simultaneously and all these branches are isolated from each other. It enables multiple work flow and then we can merge all the branches to give rise to a single branch.

Git Architecture:-

# In Git there are four Areas.

✔Working Directory ✔Staging Area ✔Local Repository ✔Remote Repository.

# Git has two types of Repository

✔Local repository ✔Remote repository.

📄Usually, the total project code will be available in the remote repository.

📄The current work of the developer will be stored in the local repository.

📄New files will be created in the working directory Once work is completed we have to add these files in the Staging area for this we have to use the git add command.

👍Git clone means to create a new local repository from the remote repository. (cloning means creating an exactly duplicate copy) generally used by new developers who joined the organisation.

👍Git pull means to get updated files from the remote repository to the local repository.

👍Git push means to move files from the local repository to the remote repository.

👍Git commit means staged changes will move to the local repository. Every commit will generate a commit ID. For every commit git records the author name, email id, time stamp, and commit message.

Life Cycle of File in Git:-

Every file in git is one of the following 4 stages.

  1. Untracked(untracked by git) state

  2. Staged state

  3. Modified state

  4. In repository/committed state

Untracked state:-Every New file will be created in working directory, git does not aware this new file. such type of files are said to be in "untracked".(untracked will be removed when we bring the files in staging area with the help of git add coommand).

Staged state:-The files which are added to Staging area are said to be in staged state.

✔The staged file would also be available in working directory because git add command does not move the file .It just copy file's state and put in Staging area.

✔git add a.txt - To add only one file(a.txt) to Staging Area.

✔git add . - To add all the files to Staging Area.dot means all.

✔git add a.txt b.txt c.txt - To add multiple files to Staging Area.

✔git add *.txt - To add all .txt files to Staging Area.

In Repository/Committed state:- File which is committed is said to be in repository(Local) state or committed state but when we do git commit the first time it will ask username and email for this be we will be using git config --global user.email "your email" and git config --global user.name "your user name". It will also ask commit message for this we will use git commit -m "followed by a commit message".

Modified state:- File which is already tracked by git but is modified in the working directory is said to be in a modified state.And which files are tracked by git ?? Always remember the files which are added to the Staging area or committed are only tracked by git.

Git practical(for Windows):-Download git for windows from Google and go for (64 bit git for windows setup),Install it now create a working directory( folder) in any drive of your PC after that we have to request git that we need version control for this folder so for this we will open git bash by right Click on PC screen. Now from CLI we will use git init command.This command actually means we are requesting git to provide an empty local repository inside our working directory folder so that version control can be implemented.

After this you can use "git add" command to add the files in Staging area and from there you can use "git commit" to commit the stage changes to the local repository. I am not completing every aspects of git here ,just trying to clear some basic knowledge about git that paralyze begineeers to understand git but I am going to define some daily used command which will help you to understand git more easily.

git init-We are requesting git to provide local repository in working directory so that version control will be applicable for that working directory.

git add:-To add files from working directory to Staging area. once we added files to Staging area then git tracks these files and ready for commit.

git commit:-Saged changes will move to the local repository. Every commit will generate a commit ID(40 characters). For every commit git records the author name, email id, time stamp, and commit message.

git status:- It shows the current status of all files in each area like untracted files,modified files, and staged files.

git log:- It provides detailed log information like history of all commits.Eg. Commit ID(40 Character),commit message,author name and email and time stamp.