Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
8 min read
Git for Beginners: Basics and Essential Commands

In this article, we will learn the basic terminologies of the version control system known as Git. This article is intended for beginners who are just getting started with Git.

We will understand what is Git, why Git is used, how it helps developers manage code changes, and how it makes collaboration easier in real-world projects. By the end of this article, we will be familiar with essential Git concepts and commands that create the foundation for further learning.


What is Git

Git is a tool that helps us save and manage our code. It is a version control system that keeps record of changes made to files over time. Whenever we make changes to our code, Git keeps a record of those changes so nothing gets lost.

It allows us to save different versions of any project, each saved version gives us the state of the project at a specific point of time. If we make a mistake, Git allows us to go back to an earlier version of our code. This makes it safe to experiment and try new things without fear.

Git works locally on our computer and maintain a complete history of the project, also we can use websites like github, gitlab etc. which allow us to access our code on the go, these are the web based services that hosts Git repositories on the cloud, which allows developers to access the code remotely and make the collaboration easier.


Why Git is Used

Git is used to track changes in code, whenever a file is modified Git remembers what was changed. This helps developers to understand the history of a project.

Git is also helpful when many people work on the same project as a team. Everyone can work on their own part, and Git helps combine all the work properly.

Git is also used to avoid losing works, if something goes wrong, we can easily return to an older version of our code instead of starting from scratch.

Git allows developers to experiment safely, we can try new ideas, make changes and test features without affecting the main project.

In simple words, Git works like a time machine for your code, helping you track progress, avoid losing work, and work better with others.


Git Basics and Core Terminologies

Before learning about the Git commands, let’s understand some basic terms commonly used while working with Git:

Repository

It is also called as Repo in short, It is a place where we store our all the project files and their complete history, It can be exist in our local computer or on a remote server.

Working Directory

The working directory is the folder on our local computer where we actively work on our project files, any chnages we make to files happen here first.

Staging Area

The Stagging areas is a intermediate place where changes are kept before they are saved permanently, It allows us to choose which changes should be included in the next version of our code.

Commit

Commit is a kind of snapshot or backup of our project at a specific point of time, When we commit, Git saves the current changes, which are staged, along with a message describing what is that commit about or what are the changes made.

Branch

Branch is a seperate line of development, It allows us to work on a new feature or changes in parallel codebase without affecting the main project.

When to use Branch :

  • New feature development

  • Bug Fixing

  • Experiments with ideas

Main Branch (Master Branch)

Main branch is the default branch for our repository, If we don’t create any branch in our repo, everything will be stored in main branch. It usually contains the stable version of the project.

HEAD

HEAD points to the current commit or branch we are working on, It helps Git to know where we are int project history.

Push

Push means sending our local commits to remote repository, It updates the remote repository with the chnages we have made on our computer.

Pull

Pull means featching changes from a remote repository and merging them into our local repo.

Clone

Clone is the process of creating a local copy of an existing remote repo on our local computer.


Common Git Commands With Examples

In this section we will learn about the basic Git command, which are used commonly in personal and professional project development. These commands will help us to start a project, track changes and work with repositories.

Here is the list of basic commands used in Git :

git init

This command is used to initialize a new Git Repository in our project folder.

Example :

git init

This command creates a hidden .git folder that allows Git to track our project.

git status

This command shows the current state of our repository, It tells us which files are modified, staged or untracked

Example :

git status

git add

This command is used to add newly created or modified files to the staging area, we can stage a single file, multiple files, files from specific folder or all the untracked files with this command.

Example (Add a single file)

git add index.html

Example (Add Multiple Files) :

git add index.html style.css script.js

Example (Add all files from a folder) :

git add src/

Example (Add all files) :

git add .

After adding files, always check their status using git status command.

git commit

This command saves the staged changes as a commit with a message

Example :

git commit -m "First Commit"

This message should describe what changes are made which will be easy to understand.

git log

This command displays the commit history of the repository.

Example :

git log

git branch

This command is used to view or create branches in any repository.

git branch

Example (Create a new Branch) :

git branch new-feature

git checkout

This command is used to switch between branches.

Example :

git checkout new-feature

git clone

This command is used to create a local copy of any remote repository.

Example :

git clone https://github.com/username/repo-name.git

git pull

This command fetches and merges changes from a remote repository into our local folder.

Example :

git pull origin main

git push

This command uploads our local commits to a remote repository.

Example :

git push origin main

git remote -v

This command shows the connected remote repositories.

Example :

git remote -v

Git vs GitHub

Many beginners think Git and GitHub are the same, but they are different tools that work together.

What is Git ?

Git is a version control system, It runs on our local computer and helps us track changes in project files. Git works offline and is responsible for creating commits, managing branches and maintaining different versions of project.

What is GitHub ?

Github is a web based platform that stores Git repositories on the internet over some cloud storage, which allows developers to share code files, collaborate with each others, and manage the project remotely.

in simple words we can say that, Git is a tool or software installed in our local machine which helps us to manage our code, and GitHub provides us a platform to publish those local code files over internet to make it accessible for others.

A Real Life Example to understand Git and GitHub :

Let’s suppose Git as a camera app available on our mobile phone, and GitHub as an online gallery (e.g. Google Photos, Google Drive) where we upload and share those photos.


Basic Git Workflow and User Guide

We can understand the working of Git with below diagram :

As shown in above image, after initializing Git using git init in our working directory, we need to use git add command to move the modified code into staging area, after that we can use git commit to save that code into our local Git repo, after that using git push command, we can upload that code into remote repository like GitHub.

Here is a basic step by step user workflow of Git to understand the uses of various git commands :

Prerequisites : Git must be installed on computer.

Step 1 : Create or Clone Repository

Go to the any terminal which supports git, navigate to project folder path and use below command to initialize git in the project :

git init

or Clone any exisitng repository using below command :

git clone <repository-url>

Step 2: Make Changes in the Working Directory

Edit the project files using any code editor, All changes will be happen in working directory.

Step 3: Check File Status

Before saving changes, check the current state of all the files.

git status

This shows which files are modified, untracked, or staged.

Step 4: Add Changes to the Staging Areas

Select the files we want to include in the next commit.

git add <file-name>

or add all changes :

git add .

Step 5: Commit the Changes

Save the staged changes with a meaningful message

git commit -m "Describe the changes here"

Step 6: Push Changes to Remote Repository

If your project is connected to remote repository, upload the commits.

git push origin main

While working with remote repositories like Github, they provide a remote origin url, to connect our local project to GitHub, follow instructions given on their sites to connect local and remote repos.

Step 7: Pull Latest Changes (If Working in a Team)

To get updates made by others, pull the latest changes.

git pull origin main

Summary

In this article we learned about the fundamentals of version control system called as Git. We explored basics of Git, terminologies, few basic commands, a step by step workflow of Git and difference between Git and GitHub. These concepts will help us to understand the basics of Git and Github and will create a foundation required to work with Git confidently.

By Practising these basics regularly, We can build a strong foundation and be ready to move on to more advanced concepts.