An introduction to Git

What is Git?

Git is an open-source distributed version control system used for tracking and logging source code changes. It was created by Linus Torvalds in 2005 for the development of Linux kernel.

Earlier, before the inception of Git, source code versioning was done via a central server mechanism rather than a distributed system. A central server is basically a standalone server hosting the source code which is available for developers to work upon to build and fix the product. This approach had number of drawbacks as listed below:

  1. There was no tracking of the changes
  2. Since there was no tracking, developers wouldn’t know if someone else changed something on the code while they were working on it
  3. Developers would directly work on the main code in the central server having no local copy for testing

In case of loss of connectivity or server crash the developers can’t continue to work and in case of a data loss, there is no way to quickly restore the code from the backup

Example of central version control system

A distributed version control system is where the code is distributed to each developer to have a local copy to work upon. This approach was considered to overcome the drawbacks from the central version control system and therefore in came the existence of Git.

Below are some of the advantages of Git:

  1. Each developer has their own local working copy of the code
  2. Every code change is tracked and logged by Git to know what change was made by who and when and why
  3. Developers work independently but are still aware of changes
  4. Since each developer has their own local copy of the code, they don’t need to connect to internet unless they want to push the changes to the origin
  5. In case of data loss or corruption, any developer can push the entire repository and source code back to Git origin making restoration quick and seamless
Example of distributed version control system

Git workflow:

A typical Git workflow consists of a Git origin repository which is cloned by the developer locally and then is worked upon to make changes. Following are the different states of a Git workflow

  1. Origin: An origin is also called as the remote repository where the project originally resides
  2. Git directory: A hidden folder (.git) which stores all the information about your project such as commits, remote repo location, change tracking and logging
  3. Working area: A location where all your code resides and can be modified to fix bugs or incorporate new features
  4. Staging area: A location in between the working copy and the git directory where you can verify your changes before committing them to the repository

Example of typical git workflow

Git branching:

Typically, a Git project would have a master branch which is the main branch from which your production would be running from.

To develop any new feature a develop would create a feature branch from the master to work on, so that any changes made there does not impact the master/production.

Similarly, for further development of features, a develop branch can be created or a release branch or hotfix for any bugs. Once the changes in these branches are validated and have gone through various tests and is considered safe, they are merged back into the master branch to have a new production milestone. The below diagram illustrates the git branching.

Example of git branching

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *