# Essential Git Commands for Beginners: A Simple Guide

In the previous blog, we saw how Git works internally – what is present inside the `.git` folder and what “blob”, “tree”, and “commit” actually mean. We also understood how these objects are formed and how Git creates them internally.

In this blog, we will focus on the basics of Git. We will understand what Git is, why Git is used, and learn core Git terminologies. We will also go through common Git commands and see a basic Git workflow that developers follow in real projects.

## What is Git?

Git is an **open-source distributed version control system** that helps developers track changes in their source code and other files during software development.

It allows multiple developers to work on the same project simultaneously without overwriting each other’s changes. Git maintains a complete history of all modifications made to the source code, which makes it easy to go back to a previous version if needed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768645656802/ac12b0be-24d4-463d-a865-badded3a8bdd.png align="center")

## Why is Git Used?

In the **“From Pen Drives to Git: Why Version Control Exists”** **blog**, we discussed the problems developers faced when they used pen drives or emails to share their source code. Some of the major problems were:

1. Files getting overwritten
    
2. Losing source code
    
3. No history of code changes
    
4. Confusion between different versions
    
5. No safety in team collaboration
    

Git solves all these problems in a structured way:

1. It prevents accidental file overwriting
    
2. It protects code from being lost
    
3. It maintains a complete history of changes
    
4. It eliminates confusion between versions
    
5. It enables safe and controlled team collaboration
    

Git helps in team collaboration by providing a central repository. Every developer can pull the source code into their local repository, make changes, and then push those changes back to the central repository.

Because of this approach, there is no need to use pen drives or emails to share code. The source code is stored in one central place, and developers do not have to depend on each other to manually share files.

## Git Basics & Core Terminologies

Before directly jumping into Git commands, it is important to understand some basic Git terminologies.

1. **Repository (Repo)**
    

A repository is a **storage space** where your project’s source code, files, and their entire history are kept.

There are two types of repositories:

1. **Local repository** – created on your own computer
    
2. **Remote repository** – hosted on a server (**for example:** GitHub, GitLab, Bitbucket, etc.)
    

You can understand a repository like a **warehouse** where everything related to your project is stored. A **real-life example** is an Amazon warehouse, where all products are kept and managed in one place.

![](https://mivegec.pages.ird.fr/dainat/malbec-git/pages/git/images/git_overview_remote.png align="left")

2. **Commit**
    

As we discussed in the **“How Git Works Internally” blog**, a commit is basically a **snapshot of your project** at a specific point in time.

1. Each commit has a **unique identifier** (created using a **SHA-1 hash**)
    
2. Contains a commit message that describes what changes were made
    

A commit helps developers understand what was changed, when it was changed, and why it was changed.

3. **Branch**
    

A branch is an independent line of development.

Branches allow developers to work on new features or bug fixes without affecting the main codebase (usually the **main** or **master** branch).

**You can understand it like this:**

Suppose a developer is working on a project and is asked to develop a new feature. Instead of making changes directly in the main codebase, the developer creates a new branch (**for example**, xyz\_feature). The developer works on this branch, tests the feature, fixes issues if any, and once everything works correctly, the branch is merged back into the main branch.

This keeps the main codebase safe and stable.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768645772394/fa136e39-5277-4ac3-9b44-b6666862e843.jpeg align="center")

4. **Merge**
    

Merge is the process of integrating changes from one branch into another branch.

**For example**, when a feature branch is completed and tested, it is merged into the main branch so that the new feature becomes part of the main codebase.

5. **Staging Area**
    

The staging area is an intermediate area between the working directory and the repository.

It allows you to select which file changes you want to include in your next commit. This gives you precise control over what is saved.

You can think of the staging area as a **checkpoint**, where you review and prepare changes before committing them.

6. **HEAD**
    

**HEAD** is a pointer that tells Git which commit or branch you are currently working on. It always points to the latest commit of the current branch, so Git knows your current position in the project history.

## Common Git Commands Every Beginner Should Know

Now we will move to the basic Git commands that every beginner developer should know.

1. **Get Started**
    

| **S.No.** | **Command** | **Description** | **Example** |
| --- | --- | --- | --- |
| 1. | git clone | Creates a local copy of an existing remote repository. | git clone &lt;repository-url&gt; |
| 2. | git init | Initializes a new Git repository in the current directory. | git init |
| 3. | git config | Sets configuration values like your name and email to associate commits with your identity. | git config --global [user.name](http://user.name) "Your Name" git config --global [user.email](http://user.email) "Your email" |

2. **Staging and Committing Changes**
    

| **S.No.** | **Command** | **Description** | **Example** |
| --- | --- | --- | --- |
| 1. | git status | Shows the state of the working directory (untracked, modified, and staged files). | git status |
| 2. | git add | Stages changes for the next commit. | git add &lt;filename&gt; **(single file)** OR git add . **(all files)** |
| 3. | git commit | Records the staged changes as a snapshot in project history. | git commit -m "**A descriptive message**" |
| 4. | git diff | Shows differences between modified files and the last commit. | git diff |

3. **Branch Management**
    

| **S.No.** | **Command** | **Description** | **Example** |
| --- | --- | --- | --- |
| 1. | git branch | Lists, creates, or deletes branches. | git branch **&lt;new branch name&gt;** |
| 2. | git checkout | Switches to another branch or commit. | git checkout **&lt;branch-name&gt;** |
| 3. | git merge | Merges changes from another branch into the current branch. | git merge **&lt;branch-name&gt;** |

4. **Syncing with Remote Repositories**
    

| **S.No.** | **Command** | **Description** | **Example** |
| --- | --- | --- | --- |
| 1. | git remote | Manages remote repository connections. | git remote add origin **&lt;server-url&gt;** |
| 2. | git pull | Fetches and merges changes from remote to local. | git pull origin **&lt;branch-name&gt;** |
| 3. | git push | Uploads local commits to the remote repository. | git push origin **&lt;branch-name&gt;** |
| 4. | git fetch | Downloads remote data without merging. | git fetch origin |

5. **Undoing Changes and Viewing History**
    

| **S.No.** | **Command** | **Description** | **Example** |
| --- | --- | --- | --- |
| 1. | git log | Displays commit history. | git log |
| 2. | git reset | Moves the branch pointer and optionally removes changes. | git reset --hard HEAD **(discard all local changes)** |
| 3. | git revert | Creates a new commit that undoes a previous commit. | git revert **&lt;commit-hash&gt;** |
| 4. | git stash | Temporarily saves changes for later use. | git stash |

## Practical Demo: Working with Git Step-by-Step

Now let’s see a basic Git workflow with a simple practical demo.

**Step 1:** Create Project Folder

1. Create a new folder named **git-demo**
    
2. Open this folder in VS Code
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768643496389/2d635f18-4360-486a-aef5-1416d2ff7a57.png align="center")

**Step 2:** Create a File

1. Inside the folder, create a file named demo.txt
    
2. Add text: This is a practical demo of Git.
    
3. Save the file and close VS Code.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768643589627/6aa5baa3-9507-48a6-8d06-22ebc82cbc7e.png align="center")

**Step 3:** Open Git Bash

1. Install Git from the **official Git website** (LTS version)
    
2. Right-click inside the git-demo folder → Open Git Bash Here
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768643733069/8d23b42b-2168-4389-b13a-2404c33d0a56.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768643854473/fd9cd22e-1ed5-4da3-a3c2-49adec378e1d.png align="center")

**Step 4:** Initialize Git Repository

Run: **git init**

1. Git creates a hidden `.git` folder inside your project directory.
    
2. When you open the folder in VS Code, you may notice the directory name appears in **green**, which indicates that the repository has been initialized successfully.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768643934552/b1a7fba9-6b56-4774-8c11-7dacf053ee02.png align="center")

**Step 5:** Check File Status

Run: **git status**

1. demo.txt appears as an untracked file.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644080024/c5b8c307-85dd-48ce-b8e4-604319580c2e.png align="center")

**Step 6:** Add File to Staging Area

Run: **git add** **demo.txt**

OR

Run: **git add .**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644124071/1c27952e-6314-4d7f-8a07-510c962c1e8c.png align="center")

**Step 7:** Verify Staged Files

Run: **git status**

demo.txt now appears in the staging area.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644138189/365a4d30-8542-4e9e-abf6-111bdbe2b173.png align="center")

**Step 8:** Commit Changes

Run: **git commit -m** "Initial commit"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644299427/6a69be63-729f-4dec-8f7e-ec5431b4fe9f.png align="center")

**Step 9:** View Commit History

Run: **git log**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644329238/6331537f-90f9-4f6d-8a0a-52ba80d5efef.png align="center")

**Step 10:** Push to GitHub

1. Create repo on GitHub
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644407849/3bf69f0d-bf1e-4261-baa6-fdc5244979aa.png align="center")

2. Connect remote.
    

* git remote add origin **&lt;repo-url&gt;**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644488948/10acf26a-b204-44c6-9e7f-7700795fae6b.png align="center")
    
* git push -u origin main
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644526226/133414cc-5cb7-44a0-9a14-a25bdee79246.png align="center")
    
    Now you can see that all files have been successfully pushed to the remote repository hosted on the server.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1768644575584/4a5cc828-7353-4d36-9d77-45873fbcbb01.png align="center")
    

## Conclusion

Git is an essential tool for modern software development. It helps developers track changes, manage different versions of their code, and collaborate safely with team members. Instead of manually sharing files through pen drives or emails, Git provides a structured and reliable way to store and manage project history.

By understanding basic Git concepts and commands, beginners can confidently start managing their projects and avoid common mistakes like losing code or overwriting files. Git not only makes development organized but also makes teamwork easier and more efficient.

With this foundation, you are now ready to explore more advanced Git features and use Git effectively in real-world projects.
