DEV Community

Cover image for Git Commands Explained: A Beginner's Guide to Version Control
SATYA SOOTAR
SATYA SOOTAR

Posted on

Git Commands Explained: A Beginner's Guide to Version Control

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

Visual diagram showing the flow of Git from Working Directory to Staging Area to Repository

Detailed visual representation of Git workflow showing commands and state transitions

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

4. git commit

Saves a permanent snapshot of everything in the staging area to your repository.

git commit -m "Initial Commit"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This creates a new branch called feature-login.

To see all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

7. git switch

Switches you from one branch to another.

git switch feature-login
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This shows all unstaged changes in your working directory. You will see lines marked with:

  1. + (plus) for added lines, shown in green
  2. - (minus) for deleted lines, shown in red

To see changes that are staged for commit:

git diff --staged
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Visual diagram illustrating the difference between git reset and git revert


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
Enter fullscreen mode Exit fullscreen mode

Using the terminal, we created a project folder named git-commands.

Terminal showing creation of git-commands directory and navigation into it

To list all files, including hidden ones, in Windows PowerShell:

ls -Force
Enter fullscreen mode Exit fullscreen mode

PowerShell output showing empty directory listing

The folder is currently empty, so nothing appears in the output.

STEP-2: Initialize Git Repository

We initialize an empty Git repository.

git init
Enter fullscreen mode Exit fullscreen mode

Terminal output showing successful Git repository initialization

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
Enter fullscreen mode Exit fullscreen mode

Git status output showing no commits yet on main branch

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
Enter fullscreen mode Exit fullscreen mode

VS Code showing newly created index.js file with sample JavaScript code

STEP-5: Check Status After Creating File

Let's check the Git status again.

git status
Enter fullscreen mode Exit fullscreen mode

Git status showing index.js as an untracked file

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
Enter fullscreen mode Exit fullscreen mode

Terminal showing git add command being executed

Check the status again:

git status
Enter fullscreen mode Exit fullscreen mode

Git status showing index.js in staging area under

"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"
Enter fullscreen mode Exit fullscreen mode

Terminal showing successful commit with message

To view the commit history:

git log
Enter fullscreen mode Exit fullscreen mode

Git log showing commit details including hash, author, date, and message

This shows the commit hash, author, date, and commit message.

Check the status again:

git status
Enter fullscreen mode Exit fullscreen mode

Git status showing clean working tree with nothing to commit

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
Enter fullscreen mode Exit fullscreen mode

Directory listing showing .git/objects folder with subdirectories

Folders with two-character names represent Git objects: commits, trees, and blobs.

Focus on a folder such as 11:

cd 11
ls
Enter fullscreen mode Exit fullscreen mode

Contents of the objects/11 directory showing hash file

Combine the folder name and file name to get the full commit hash 115d4fcd38e4baef75ee4aa824952678a46a237d

Inspect the commit object:

git cat-file -p <commit-hash>
Enter fullscreen mode Exit fullscreen mode

This shows metadata about the commit and a reference to a tree object.

Inspect the tree object:

git cat-file -p <tree-hash>
Enter fullscreen mode Exit fullscreen mode

Output of git cat-file showing tree object with blob reference

The tree object references blob objects, which store file contents.

Inspect the blob object:

git cat-file -p <blob-hash>
Enter fullscreen mode Exit fullscreen mode

Output showing the actual file content stored in the blob object

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.

VS Code showing modified index.js file with additional code

VS Code showing newly created index.css file

M indicates a modified file, and U indicates an untracked file.

Check the status:

git status
Enter fullscreen mode Exit fullscreen mode

Git status showing modified index.js and untracked index.css

STEP-9: Add All Files

Add all files at once:

git add .
Enter fullscreen mode Exit fullscreen mode

Terminal showing git add . command execution

STEP-10: Make Second Commit

Commit the changes:

git commit -m "2nd commit"
Enter fullscreen mode Exit fullscreen mode

Terminal showing successful second commit

STEP-11: View Commit History

View the commit history:

git log
Enter fullscreen mode Exit fullscreen mode

Git log showing two commits with HEAD pointing to latest commit on main branch

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>
Enter fullscreen mode Exit fullscreen mode

Git diff output showing line-by-line changes between commits

STEP-13: List All Branches

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Terminal showing only main branch exists

Currently, there is only one branch called main.

STEP-14: Create a New Branch

Create a new feature branch:

git branch feature-login
Enter fullscreen mode Exit fullscreen mode

Terminal showing creation of feature-login branch

STEP-15: Switch to Feature Branch

Switch to the feature branch:

git switch feature-login
Enter fullscreen mode Exit fullscreen mode

Terminal showing successful branch switch to 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"
Enter fullscreen mode Exit fullscreen mode

Terminal showing staging and committing changes on feature branch

VS Code showing login.js file with login implementation code

File explorer showing login.js file exists in feature branch

Switch back to main:

Terminal showing switch back to main branch

File explorer showing login.js is not present on main branch

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
Enter fullscreen mode Exit fullscreen mode

Terminal showing successful merge of feature-login into main

Git log showing merged commits from both branches

File explorer showing login.js now present on main branch after merge

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
Enter fullscreen mode Exit fullscreen mode

VS Code showing modifications to index.css file

Git status showing index.css with unstaged changes

Terminal and status showing changes discarded after git restore

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"
Enter fullscreen mode Exit fullscreen mode

Terminal showing 4th commit being made

VS Code showing file content at 4th commit

Git log showing all four commits in history

Reset to the previous commit:

git reset HEAD~
Enter fullscreen mode Exit fullscreen mode

Terminal showing git reset command execution

Git log showing 4th commit removed from history, changes back in working directory

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)