Skip to main content

Command Palette

Search for a command to run...

Introduction to Git: A Beginner’s Guide

Published
4 min read

What is Git?

In the previous blog, we came to know why version control exists and what actually a version control system is.
In this blog, we will get to know about the most popular version control system – Git.

Git is one of the most prominent version control systems. Its main task is to track our project and keep the history of changes.
Git was developed by Linus Torvalds in 2005 when they were building the Linux kernel. To keep track of the files they created, they developed Git.

Why Git?

There may be many version control systems available, but we mostly use Git because:

  • It is very easy to use

  • It keeps track of every small change

  • You can undo changes and go back to previous versions whenever needed

Basic Terminologies and Commands

Before using Git, we first need to install Git.

After installation, we use the command:

git init

This command initializes Git in our working directory.

Repository (Repo)

A repository (also called repo) is like a project hub where all the files and changes related to a project are stored and managed.

There are two types of repositories:

  1. Local Repository
    This repository is stored on your computer. All changes are saved locally.

  2. Remote Repository
    This repository is stored on a server like GitHub.
    Changes are saved on the server, and you can also share your repository with many users so they can work together collaboratively.

Commit and Staging Area

After initializing your repository using git init, you can perform many actions using Git commands.
But before that, let’s understand what a commit is.

A commit is nothing but saving the current state of your work.
Whenever you use the git commit command, Git takes a snapshot of your work and saves it in the repository.
The repository keeps track of all the commits you make.

Before committing, you must add files to the staging area.

The staging area indicates that your files are ready to be committed.
You can add files to the staging area using:

git add

This command moves your files to the staging area.
After that, you can use git commit to save your work to the local repository.

To move your committed changes to a remote repository like GitHub, you use:

git push

Branch

One important concept in Git is branch.

A branch is a separate flow of your work that allows you to deviate from the main workflow to do experiments or add new features.
If the experiment is successful, you can merge it back into the main branch.

HEAD

HEAD is nothing but a pointer to the current saved commit.
You may have done many commits, but the latest commit is pointed to by HEAD.

Important Git Commands

  • git status
    Shows the current status of your directory:

    • which files are committed

    • which files are in the staging area

    • which files are not yet committed

  • git log
    Shows the complete history of commits, including:

    • who made the commit

    • when it was made

This is very useful when working in a team.

Developer Workflow (Simple Example)

Let’s see a simple developer workflow using Git commands.

1. Initialization

Run the following command in your project root:

git init

This creates a .git folder (we will learn about it in the next blog) which tracks all changes.


2. Status

Check the current status of your files:

git status

This tells you which files are committed and which are not.


3. Staging

Add files to the staging area:

git add <filename>

Or to add all files:

git add .

(The dot means all files.)


4. Commit

Commit the staged files:

git commit -m "my first commit"

Here, -m and the text inside quotes represent the commit message, which is useful for future reference.


5. Push

Push your committed changes to the remote repository:

git push

6. Branching

Create a new branch to add a new feature:

git checkout -b new

This creates a new branch called new.


7. Switching Branch

Switch to another branch:

git checkout new

This switches the HEAD to the new branch.


8. Merge

Merge the branch back into the main branch:

git merge new

Hope this blog helped you understand Git basics. See you in the next blog.