What is Git ? ⚡️
It is a free and open source distributed version control system. Git is known for its speed, simple design and support for non-linear development. Keep reading to fall in love with this amazing tool .
The story behind Git’s creation is quite interesting and inspiring. Have you ever heard about “Creative destruction”? It is a term coined by economist Joseph Schumpeter in 1940’s : “Creative destruction describes the deliberate dismantling of established processes in order to make way for improved methods of production.” Well, Git indeed was a result of creative destruction and fierce controversy. In 2002, the Linux kernel project began using a proprietary DVCS called BitKeeper. But in 2005, the commercial company that developed Bitkeeper broke down and its free-of-charge status was revoked. This prompted the Linux team, particularly Linus Torvalds to create an open source tool for Source code management. And hence Git came into existence.
Why use version control : Ready to make Tea?
Imagine, you are hosting a tea party 😃. And your goal is to make the perfect tea for your friends. Now, the flavour of tea 🍵 depends on the proportion of contents, as well as the order in which they are mixed, while making the tea. Lets assume you need four steps in your recipe: Boil water, add sugar, add tea, add ginger and cinnamon. You would have to experiment making tea many times before getting that perfect taste. All this requires a lot of effort besides wasting resources. Only if time travel was possible, all this could have been avoided!
Then you could just go back in time and adjust the amount and order of ingredients at each stage. But unfortunately, the time machine hasn’t been invented yet. So, we can save this for another time…
But wait! In software development, time travel is possible . And that is all due to Git version control!
What is a Snapshot in Git?
Lets say, you are using Git to maintain a project folder. Each time you make some significant change, you have an option to commit your progress with a commit message . This commit gets a unique hash key using which you can go back to previous versions of your project. This commit is also known as a “Snapshot”. But what exactly is a snapshot?
Unlike other Version control systems, which stores data as “diffs” (delta versioning),Git stores data as snapshots. A snapshot is a copy of the entire working directory after a particular event/change. Once the snapshot is created with a message it is impossible to loose your work. With every commit a new snapshot is created. This is the basis of version control. The entire project development history is a sequence of snapshots. It is fast. However, storing all the files every time takes up a lot of space. This can be a problem, but git has a work around. By storing files in a object tree and eliminating the duplicate parts , it only saves the files which have changed.
How does Git differentiates between snapshots?
As git is a distributed version control system, it is possible that commits from different users working on the same project may have different content and still have same commit messages. To differentiate between different snapshots ,git uses a 40 bit hex key generated using the file content, author and time, which gives a unique signature to each commit, using the SHA1 Algorithm.
Working Tree, Staging area and Git directory:
Locally or on cloud, a project which uses git has three stages:
a. The Working Tree : Any changes made at this stage are marked as modified but not tracked.
b. The Staging Area : Any files in this stage are tracked . Here, we can select which files to commit. The remaining files can be selected in future commits. Any file can be unstaged if a user feels it is not yet ready to be committed
c. Git Repository :The committed files reside here and are available for checkout.
How to use Git?
Step 1: Install the latest version of git from https://git-scm.com/
Step 2:Check to see the version of git installed. Use command git — — version
Step 3: Get into the directory which you want to have version control and use command git init. This will initiate an empty git repository.
Step 4: Create some files and use git status to check files are being tracked by git. As you will see files are not being tracked by git yet. We have to move them from working tree to staging area using the command git add .
Step 5: Use git add command to add files in staging area and run git status command again. Now you will see the files are staged and ready for commit.
Step 5: Make your first commit using git commit -m “first commit, added file1 and file2”. Then use git log to see the hash code associated with this commit.
This was a short introduction to git. In the next article, we will discuss the coolest feature of Git that outshines others: “Git Branching and Merging”.
Have a nice and adventurous day! 😉