Before we begin diving into Git commands, let us first understand what Git is and why it is important.
Git is a version control system that tracks changes and maintains the history of a project. It allows you to create checkpoints so you can return to and view previous states of your code. Let us take the example of a game. In a story-based game, you save the state of the game at checkpoints. If you lose, you do not have to start from the beginning; instead, you return to the last checkpoint and continue playing. The same analogy applies to Git.
Without version control, the world of software development would be chaotic. If a developer is unable to track a project's history or changes, it creates many problems. Developers would not be able to collaborate effectively with each other. For a more detailed understanding, visit this blog.
What Git does is not magic; it is pure engineering. Everything that Git does happens inside the .git folder. This is a hidden folder that is created at the root of the project when you initialize Git. This folder contains a subfolder called objects. Inside the objects folder, the entire history of your codebase is stored. The data is compressed and stored efficiently. The objects folder contains three main components: blob, tree, and commit. For a more detailed understanding of the contents of the .git folder, visit this blog.
Before we begin understanding Git commands, let us go through some important terminologies and their meanings.
Important Terminologies
Repository
A repository (or "repo") is the complete collection of files, folders, and the entire history of changes for your project. Think of it as a database that stores every version of your project you have ever saved.
Working Directory
The working directory is the folder on your computer where you can see and edit your project files. It is your actual workspace where you write code, create new files, delete old ones, and make changes.
Staging Area
The staging area (also called the "index") is an intermediate space between your working directory and your repository. It is where you prepare changes before permanently saving them. Think of it like packing a box before shipping it. You decide which items (changes) to put in the box (staging area) before sealing and sending it (committing). This allows you to be selective about what gets saved together.
Commit
A commit is a permanent snapshot of your staged changes. When you commit, Git saves the current state of all files in the staging area to the repository, along with a message describing what changed and why.
Each commit has a unique identifier (a hash) and includes information about who made the change and when. Commits form the history of your project, a timeline of snapshots you can always return to. Think of commits as save points in a video game. You can always load a previous save if something goes wrong.
Branch
A branch is an independent line of development in your repository. The default branch in Git is usually called main (or master in older repositories).
Branches allow you to work on new features, bug fixes, or experiments without affecting the main codebase. You can create a new branch, make commits to it, and later merge those changes back into the main branch when you are ready.
Imagine branches as parallel universes of your project. You can explore different directions without losing your original work.
HEAD
HEAD is a pointer that indicates where you currently are in your repository. It typically points to the latest commit on your current branch.
When you switch branches, HEAD moves to point to that branch. When you make a new commit, HEAD moves forward to point to the new commit. HEAD essentially answers the question, "What am I looking at right now?"
Understanding HEAD is crucial because many Git commands operate relative to where HEAD is pointing.
Visual Representation of Git Flow
Basic Git Commands
1. git init
Creates a new Git repository in your current folder. When you run this command, you are telling Git to start tracking your files.
git init
2. git status
Shows the current state of your working directory and staging area. It is used to see which files have been modified, which are staged for commit, and which are untracked.
git status
3. git add
Moves changes from the working directory to the staging area. When you use this command, you are telling Git to prepare the changes for a commit.
git add index.js
This adds the file index.js to the staging area. If you want to add all changed files at once, you can use:
git add .
4. git commit
Saves a permanent snapshot of everything in the staging area to your repository.
git commit -m "Initial Commit"
The -m flag lets you add a message describing what changed. This message is important because it helps you and others understand what each commit does when reviewing the project history.
5. git log
Shows the history of commits in your repository.
git log
This displays a list of commits, starting with the most recent one. For each commit, you will see:
- The author's name and email
- The date and time of the commit
- The commit message
- A unique identifier (the commit hash)
6. git branch
Creates a new branch, lists existing branches, or deletes branches. Use this command when you want to start working on a new feature or check which branches exist in your repository.
git branch feature-login
This creates a new branch called feature-login.
To see all branches:
git branch
7. git switch
Switches you from one branch to another.
git switch feature-login
This moves you to the feature-login branch. Any commits you make now will be on this branch, not on main.
You can also create and switch to a new branch in one step:
git switch -c new-feature
8. git merge
Combines changes from one branch into the current branch. It is used when you have finished working on a feature branch and want to bring those changes into the main branch.
git switch main
git merge feature-login
First, you switch to the branch you want to merge changes into, usually main. Then you merge the feature branch. Git combines the commits from feature-login into main.
9. git clone
Creates a copy of an existing repository on your machine. It is used when you want to work on a project that already exists on GitHub or another hosting service.
git clone https://github.com/username/project-name.git
This downloads the entire repository, including all its history, and creates a folder on your computer. You only need to do this once per project.
10. git push
Uploads your local commits to a remote repository. It is used after you have made commits locally and want to share them with others or back them up online.
git push origin main
11. git pull
Downloads commits from a remote repository and merges them into your current branch. It is used to get the latest changes that others have pushed to the remote repository.
git pull origin main
This fetches changes from the main branch on origin and merges them into your current branch. It is good practice to pull before starting work to ensure you have the latest code.
12. git diff
Shows the exact lines that have changed in your files. It is used to review what you have modified before staging or committing.
git diff
This shows all unstaged changes in your working directory. You will see lines marked with:
-
+(plus) for added lines, shown in green -
-(minus) for deleted lines, shown in red
To see changes that are staged for commit:
git diff --staged
13. git restore
Discards changes in your working directory, returning a file to its last committed state. It is used when you have made changes to a file but want to discard them and start over.
git restore index.html
14. git reset
Moves your current branch backward, undoing commits. Use this command carefully, as it can rewrite history and cause confusion if misused.
git reset <hash>
Warning: Be very careful when using git reset in shared repositories. Rewriting history can cause problems for collaborators. When in doubt, use git revert instead.
15. git revert
Creates a new commit that undoes the changes from a previous commit. Use this command when you want to undo a commit that has already been pushed to a shared repository. Unlike git reset, it does not rewrite history.
git revert HEAD
This creates a new commit that reverses the changes from your most recent commit. It is safer than git reset when working with others because it preserves the project history.
Demonstration of the Commands
STEP-1: Setting Up the Project
We need to set up a project to run all the commands.
mkdir git-commands
cd git-commands
Using the terminal, we created a project folder named git-commands.
To list all files, including hidden ones, in Windows PowerShell:
ls -Force
The folder is currently empty, so nothing appears in the output.
STEP-2: Initialize Git Repository
We initialize an empty Git repository.
git init
After initializing the repository, a hidden .git folder is created.
STEP-3: Check Repository Status
We check the status of the repository using the git status command.
git status
The output shows that there are no commits yet.
STEP-4: Create a File
Now let's create a file and add some text to it.
touch index.js
STEP-5: Check Status After Creating File
Let's check the Git status again.
git status
You can see an untracked file named index.js. This means Git is not tracking the file yet. And at the bottom you can see it's written "nothing added to commit but untracked files present (use "git add" to track)"
STEP-6: Add File to Staging Area
Now we add the file to the staging area using the git add command.
git add index.js
Check the status again:
git status
"Changes to be committed" means the file is now in the staging area and ready to be committed.
STEP-7: Make First Commit
We make our first commit.
git commit -m "Initial commit"
To view the commit history:
git log
This shows the commit hash, author, date, and commit message.
Check the status again:
git status
We are on the main branch, and there is nothing to commit.
Exploring the .git Folder (Deep dive into how git actually stores history)
Navigate to the objects folder:
cd .git/objects
ls
Folders with two-character names represent Git objects: commits, trees, and blobs.
Focus on a folder such as 11:
cd 11
ls
Combine the folder name and file name to get the full commit hash 115d4fcd38e4baef75ee4aa824952678a46a237d
Inspect the commit object:
git cat-file -p <commit-hash>
This shows metadata about the commit and a reference to a tree object.
Inspect the tree object:
git cat-file -p <tree-hash>
The tree object references blob objects, which store file contents.
Inspect the blob object:
git cat-file -p <blob-hash>
This is the actual content of the file stored by Git.
STEP-8: Modify Files and Add New Ones
Now let's modify an existing file and add a new one.
M indicates a modified file, and U indicates an untracked file.
Check the status:
git status
STEP-9: Add All Files
Add all files at once:
git add .
STEP-10: Make Second Commit
Commit the changes:
git commit -m "2nd commit"
STEP-11: View Commit History
View the commit history:
git log
There are now two commits, and HEAD points to the latest commit on the main branch.
STEP-12: View Differences Between Commits
View changes between commits:
git diff <commit-hash>
STEP-13: List All Branches
List all branches:
git branch
Currently, there is only one branch called main.
STEP-14: Create a New Branch
Create a new feature branch:
git branch feature-login
STEP-15: Switch to Feature Branch
Switch to the feature branch:
git switch feature-login
STEP-16: Implement Feature and Commit
Implement the login feature and commit the changes.
git add .
git commit -m "3rd commit from feature branch"
Switch back to main:
The login feature is not visible because it exists only in the feature branch.
STEP-17: Merge Feature Branch into Main
Merge the feature branch into main.
Important: You must be on the main branch.
git merge feature-login
Now the login feature is part of the main branch.
STEP-18: Discard Uncommitted Changes
Discard uncommitted changes using git restore.
git restore index.css
The uncommitted changes are removed.
STEP-19: Reset Committed Changes
git restore works only for uncommitted changes. If changes are already committed, use git reset.
After committing changes:
git commit -m "4th commit to see how to roll back to previous state"
Reset to the previous commit:
git reset HEAD~
The commit is removed, and the files can be restored if needed.
Final Thoughts
Git can feel overwhelming at first, but once you understand what is happening under the hood, the commands start to make sense instead of feeling like magic. By learning how Git tracks changes, stores data, and manages history, you gain confidence to experiment, collaborate, and recover from mistakes without fear. This foundation will make every future Git workflow easier, whether you are working alone or with a team. Keep practicing, explore the internals, and let Git work for you, not against you.












































Top comments (0)