What is Git? - A Beginner's Guide

In this blog, I am introducing Git , one of the most widely used Version Control systems.





Git is a free and open sourced Version Control System you download in your computer that can keep track of the changes you make to your code/project. It is important to know Git is different from GitHub and GitLab. GitHub, GitLab are web-based repositories(hosting services) that help with code management and sharing local file changes with a remote repository. Many companies are using Git, that includes Google, Microsoft, Twitter, Linkedln, Facebook, NetFlix .

You can download and install Git from this link.

After installing or if already installed, you can check the version of Git using git --version command.


Getting started with Git:

We can use Git either with terminal or GUI. if you are using terminal, it requires you to have understanding of the commands to be used whereas GUI requires understanding of steps to be followed. But the basic understanding of the work flow remains same for both. You do not have to memorize all the commands except some, which are used often. You can explore all the commands in the documentation.

Here, we can see how we use terminal to prepare a project to use Git. You can use Git Bash(terminal).

Let us begin with preparing an existing project to use Git.


Step 1: Prepare the project

In the terminal, navigate to the project folder

Example:

cd "/C/Users/myFolder/myProject"

Step 2: Initialize a git Repository

Once you are in your project folder, you need to initialize a git repository using the git init command


User /C/Users/myFolder/myProject (main) git init

This will create a hidden folder .git inside your project folder in the background. You can see that using ls -a command. This is your local git repository. This repository will keep track of all the changes you make to the project over time.


Step 3: Commit the changes

This include two steps:

a) git add

Before committing, you have to explicitly tell Git about what changes you have made in the project. This means that a file won't be automatically included in the next commit just because it was changed. Instead, you need to use the git add command to mark the desired changes to include.

git add . 

Or you can explicitly mention the changes to be added using the following syntax:

git add index.html

After adding, you can commit the changes. A commit is the version of your project. It represents all the changes you made in your project which include all the files and folders that are part of the changes.


b) git commit

Now you can commit the changes into the local repository by using git commit command. You can also pass messages to the commit using git commit -m message.

git commit -m "First Commit"

What is happening behind the scene???

When you are creating a .git folder , Git will be just aware of the files and folders included in the project. It will not keep track of the changes until you commit it. In order to understand what is happening behind, you need to know about Stages of Git Life Cycle.

A project under Git Version Control System falls under three Git stages:

  • Working directory

  • Staging area

  • Git directory

These three stages are the core features of Git. You get great flexibility in tracking the files. Due to these stages, the files can reside under Git.

Let’s understand each of these states one by one.

Working directory:

The project residing in your local machine is the Working Directory. It may or may not be tracked by Git. When you initialize the git repository(.git), Git will be aware of the files and folders of the project but still will not be tracking the changes. This is also called as Working Directory. To track the changes you have to add the changes to the Staging area using git add.

Staging area:

You are in Working Directory and you want to track the changes in the project. In that case, you will add those files into the local repository. In other words, staging area is the area where you group or add the files to be committed.

Git Directory:

After grouping/ adding the files to the staging area, you can now commit the changes.

Apart from commit message, this step also records the author and time of the commit. Now, a snapshot of the files in the commit is recorded by Git. The information related to this commit (names of files committed, date and time of commit, author of commit, commit message) are stored in the Git directory. Now your project is ready to be pushed to the repository.


The next step is to push the local repository to the remote repository. For that we need to create a remote repository.

There are number of websites that allow hosting . Among them the most popular website is GitHub. GitHub is the website that stores Git repositories. You can access these repositories and manipulate them as required.

Lets see how you create your first repository:

Go to www.github.com and signup for a free account. Then go to github.com/new


1. Give a repository name, say myWebsite

2. Give a description, say 'This is my website using html , css , scss, and python'.

3.Now choose option if it need to be

  • public : Anyone can see the repository, but you can choose who can commit

  • private: You can choose who can see and commit

4. Click Create to create the repository


An empty repository is created. Now, you can push your project into this repository.


Step 4:

For the first time you are pushing the code into a remote repository, you have to add the remote repository path in Git where your repository is stored, using the command git remote add in the terminal


git remote add <name> <url>
For Example: 
git remote add origin https://github.com/Git_Username/myWebsite.git

You will get the url from the repository you created.

Before you continue further, we can discuss a little about git remote command and its uses.(You may skip to step 5 directly if you are already familiar with git remote)


git remote:

The git remote command lets you create, view, and delete connections to other repositories. Remote connections are more like bookmarks rather than direct links into other repositories. The git remote command is essentially an interface for managing a list of remote entries that are stored in the repository's ./.git/config file. The following commands are used to view the current state of the remote list.

  • To list all the remote connections to all the remote repositories:

git remote
  • To list all the remote connections with url:

git remote -v
  • To create a new connection to a remote repository. After adding a remote repository with name, you’ll be able to use this name as a convenient shortcut for other Git commands.

git remote add <name><url>
  • To remove the connection to the remote repository

git remote rm <name>
  • To rename a remote connection from to

git remote rename <old_name> <new_name>

Step 5: Before pushing the code , you can add a pointer to the commit. This is called branch.

By default , a branch called master is created when you create a repository. Here I am renaming it as main

git branch -M main

You can skip to Step 7 if you are already familiar with branches

What are Branches?

Branches are pointer to commit performed. Branches gives you the freedom to independently work on different modules and merge the modules when you finish developing them. The default branch in Git is master or main.

How branch works?