What is Git and How it works?
Let’s shallow dive into inarguably the most essential tool for a programmer or a developer nowadays - Git!
Part 1
So what is Git? and Why is it so necessary?
Git is a free, open-source and distributed Version Control Systems (VCS). It tracks and manages any changes to your programs, documents, websites, or any other information format. Now there are many VCSs out there, like Subversion, Mercurial, CVS, etc. But Git inarguably has the monopoly in the industry. All the more reason to learn to Git!
Now to understand what I just said, consider that you are writing a novel. You wrote the first chapter and you seem satisfied with how it has turned out for now. After a few days, you feel that the first chapter is not compelling enough, and you ended up making another document and wrote a different first chapter. You didn't delete the earlier document as you are unsure which one you will finalize. This arduous process of writing, rewriting, making a document, making another copy of the document goes on. Imagine writing a whole book now! You will end up with many documents, not knowing where you started and which ones to refer.
This is an extremely inefficient way of maintaining and tracking your work. Now imagine if you could have a checkpoint for every change you made along your way of writing your first novel. Every change corresponds to “every change of mind” you ever had. So whenever you wish to go back, just revisit that particular checkpoint. This eliminates need to make multiple copies and is more memory efficient.
Now the Flowchart of how Git works is as follows:
So in simple terms, you make changes (add content to your files, create a new file, etc.) in your working directory, then you use the command as follows:
git add
By doing so, you have put your changes onto the staging area (again, this is not a physical location, so don’t worry about it) Once the changes you made are staged, it’s ready to be committed! To commit your changes to your .git directory (a hidden directory), you use the following command in your terminal:
git commit
If you haven’t followed through everything till now. The fog will clear out slowly. The git add
and git commit
comes after one crucial step, which is to create a git repository. The following are the steps needed to follow to initiate a git repo
in a directory of your choice.
- Have Git installed in your machine (I use mac and it comes pre-installed)
- In the event you have installed Git, check the version of Git installed in your system by running the following command.
git --version
- You then need to configure your
name
and youremail ID
with Git. This is to identify yourself with the system.git config user.name git config --global user.email [email protected]
The
git config user.name
command is to check whether you have a name already configured, if not then configure it using the next command and replace email with name and add your name in the end. - Now it’s time to create our first repo! cd to a directory/folder of your choice and run the following:
git init
Then go to the directory, you won’t see anything yet, but the repository has been created. It’s just hidden, and hence the
.
in front of.git
directory. And while you are in your directory, if you want to check the status of the git, run the following command in the terminal:git status
- This shows you the status of your current repository.
- Do not delete the directory in which you have initiated your git repository, unless you do not care to have any tracking record of all the changes you made to your work in that directory. Once deleted, it will be lost. Do not initiate another git repo inside a repo. This is highly discouraged to do so.
- Now if you create any file, add content to it, or delete it, or do any changes in your working directory, git is tracking it and later on you can stage the changes that you want to commit, or have a checkpoint made of, by using
git add
and thengit commit
to commit your change to your .git directory. - It’s best practice to add a short message describing what changes you want to commit when you are committing your changes. Run the following command for that:
git commit -m "created text file and wrote a secret in it"
- The
-m
is a flag for message.
This blog has been only to introduce you to the terminology of Git. In part 2 we will dive into it with a little bit more understanding.