How does Git Work? An Introduction to basic Git Commands

Published on 2021-08-05

Do you wish to use Git as your Version Control System and are wondering how Git works? Keep reading! This article is a comprehensive guide for anyone who wants to understand the basic Git commands. Debugging, bug-fixing, enhancing the code are part and parcel of a developer’s life. For any developer, a version control system is a lifesaver!

Are you wondering what a Version Control System (VCS) means?

Well! A VCS is a system that allows you to track the changes you make in your code. It is a crucial tool for developers to collaborate on a project or work individually and track their progress.

So, how does Git fit in the context?

Git is an open-source, decentralized version control system. Decentralized symbolizes that each member involved in development has copies of their code. The prime purpose of Git is to allow teams to coordinate, collaborate on a project, and put in their contribution. Git records every version of the manipulation or change in the code, and you can retrieve it at any moment in time.

Now that you know what Git is, it’s time we move to the question: How does Git work?

Git stores all the information about the project and the project versions in the repository. There’s an option to Commit to the code. Commit symbolizes that the changes are final, and you would like to add them to the main branch called Master. You can track down a bug in the code by going through the commits and finding out the one resulting in the bug. You can upload the source code on GitHub or BitBucket to allow the users to view, push changes or pull code.

To get started with Git, you need to download the most compatible version of Git on your device. Once you successfully install Git, open Git Bash and create a working folder to build and test your project and track changes. Type in the command “cd” to enter your workspace.

Let’s understand some frequently used Git commands and their working.

Initialize a Git Repository:

Initialization is the first thing you will have to do before you start using other git commands. To initialize a Git repository, run the following command:

git init

This command converts the current directory into a Git working directory and enables you to start working on your project.

Add a change to the file and Commit changes:

Assume that you have added a piece of code to the ongoing project and you wish to add it to the repository. That’s when the add command comes into play.

This command also helps in tracking who made the changes and at what instance. The command is as follows:

git add <file name>

If you wish to add multiple files, replace the <file name> with a (.)

A git status command helps you understand the changes that are added and the ones that are left.

git status

Once you are satisfied with all the changes, you need to commit those changes. A commit command adds your changes to the repository. To commit to the changes, use the following command:

git commit -m "I have added the following changes:"

-m specifies the message that follows. This message is to let yourself or your team understand the changes made during the commit. You will also have to add some personal details on Git Bash before making any commits.

Track history of your project:

Maybe you lost track of the project and your progress since it’s been months that you have worked on the project. How can you track the last commit to that project?

A git log command provides you with an overview of all the commits throughout the project.

git log

Clone a Project:

If your project is already in the central repository and you would like a copy, you need to use the git clone command. The command is as follows:

git clone

Branch and Merge:

The git branch commands give you an overview of the branches that are being worked on locally.

git branch

On the other hand, the git merge combines the changes made to two distinct branches.

git merge

This article gives you an overview of some basic Git commands and how Git works. These functionalities come in very handy when you work in groups on a project.

You can even upload your project to GitHub or BitBucket to allow other users to view your code.

No matter what technology you are using, the Git commands remain the same.

Git helps in building transparency and providing flexibility to developers.