Sunday, 11 February 2018

What is Git ? List of basic Git commands and their use

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.Download git for windows - Download Git
what is git

So, let us take a step back to learn all about Version Control Systems (VCS).

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:
centralized version control system

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:
distributed version control system

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.

List of Basic Git Commands and their Use

  • git clone "url" => "git clone" command will clone the repository on your local machine
  • git status => "git status" command will let you know the difference between your local repository and remote repository
  • git add . => "git add ." command will add all the files
  • git add -A => "same as "git add . " command, will add all the files
  • git add "file" => this command will add particular file
  • git commit -m "message" => this command will commit your added files, you can write whatever changes you made so other developers see this file on Bitbucket or GitHub they will know what changes you made
  • git push => "git push" command will push the committed code to Bitbucket or GitHub(Whatever you are using)
  • git pull => "git pull"command is used to take remote repository code to your local machine
  • git => this command will list out all the commands
  • git branch => this command will list all branches(default branch will be master branch)
  • git branch "child" => this command will create new branch with "child" name
  • git checkout "child" => this command will active particular branch(here e.g. child)
  • git merge "child" => this command will merge branch "child" into branch "master" but to merge child branch into master branch you must active master branch and then you can perform this command
  • git branch -d "child" => this command will delete branch "child"
  • git push --set-upstream origin child => this command will create child branch on remote repository(Bitbucket or GitHub)
  • git config --global user.email "you email" && git config --global user.name "you name"=> both commands will configure your email id and name so you do not need to login every time you push the code to remote repository
  • git config --unset-all --global user.email "you email" && git config --unset-all --global user.name "you name"=> both commands will unset your email id and name 
Previous Post
Next Post

post written by: Dhaval Thakkar

Dhaval Thakkar is an enthusiastic web developer who is passionate about learning new things and always ready to help other developers.Just Web Code is a way to provide web solutions to web developers.

0 comments: