DEV Community

Bhupesh Chandra Joshi
Bhupesh Chandra Joshi

Posted on

Git for Beginners: Basics and Essential Commands

What is Git?

Git is a version control system , a tracking software that tracks the version of your code.

A developer opens a folder on their machine. The code is small. The files are few. Each change feels harmless. When something breaks, they simply press Undo or restore a backup named final_v2.So we will use command.

Undoing Mistakes in Git: Reset, Revert, and More
git reset HEAD~1
Removes the commit and unstages changes (keeps in working directory).

  1. Why Git is Used?

1) Version Control and code Tracking

a)git init Initializes a new Git repo in the current directory.

git init creates a . git hidden folder in your repo, and git init is responsible for modifying untracked files to trackable.

b)git status Displays modified, staged, and untracked files. git status.

2) collaboration How collaboration happen, you can clone the repo using this command.
git clone Copies a remote repo to your local machine. git clone https://github.com/user/repo.git
3) Backup and History
4) Experimentation

Core Git Concepts: Repository, Commit, Branch, and HEAD

  1. Repository (Repo) A Git repository is the container for your entire project: it includes all your files, folders, and the complete history of changes. When you initialize Git in a directory (git init), a hidden .git folder is created—this stores all the metadata, commits, branches, etc.

Git has a three-stage architecture:

1-Working Directory: Your actual files where you edit code.
Staging Area (Index): A preparation zone where you select changes to include in the next commit.
Repository: The committed history (stored in .git).

  1. Commit
    A commit is a snapshot of your repository at a specific point in time. Unlike some version control systems that store differences (deltas), Git saves full snapshots of the staged files. Each commit has a unique hash (e.g., abc1234), a message, author info, and points to parent commits—forming a chain of history.
    Commits are immutable: once created, they can't be changed (though you can create new ones to "undo").

  2. Branch
    A branch is a lightweight, movable pointer to a specific commit. It allows parallel development: you can work on new features or fixes without affecting the main code.

The default branch is usually main (formerly master).
Creating a branch (git branch new-feature) makes a new pointer.
Switching branches (git checkout new-feature) moves your working directory to that snapshot.

  1. HEAD-

HEAD is a special pointer that points to your current location in the repository—typically the tip of the current branch (the latest commit you're on).

When you switch branches, HEAD moves to point to that branch's tip.
You can also "detach" HEAD to point directly at a commit (for inspecting old states).
References like HEAD~1 mean "one commit before HEAD."

Branches enable safe experimentation—merge them later when ready.

Best Practices

1- Always check git status before committing.
so, how do we commit? Asking questions are important in learning
They need to check git log --oneline

2-Use Branches for Every Feature or Fix
Checking for which branch we are

We obtain error because we are in untracked stage, so for reaching to
tracked stage. let's run git init and see the magic.

It will initialize the git repository in current working directory(folder).

The Three Core Git Stages

1) Working Directory - This is the folder where you are actively creating, modifying and deleting code /files etc.

Changes are considered untracked or modified untill you didn't send them into next stage.

a) When we create a folder /repo on local, it is intially untracked, after running git init, the github can track it.

2)Staging Area (Index): git add . / git add index.html app.js

This command moves files to staging area , you can think like a table, you select the books and pick them and keep the selected one
for adding them to table.

It is a single file-located at .git/index that stores information what will go into your next commit screensort/snapsot.

No, the staging area (also known as the index) is not permanent storage in Git. It is a temp storage.

3)Write Meaningful Commit Messages

A commit message should explain why, not just what.

We will use git add command that will allocate memory on github for your repo,but it's temporary memory. You can consider it just like allocating memory for your file.

git add . ,but it is not recomended everytime.

git add file_change.txt , that is the recommended approach.

git commit -m "you need to write message here"

Let's create git branches ,we will use git checkout -b feature
git branch => It will provide you the information which branch you are.

You need to prepare the table ( stage ) for commit.

steps 1-
git add hello.txt

step2-
git commit -m "I have fixed the bug"

Now I will introduce the merge conflict and I will solve it.

We obtain merge conflict when two person are working on same project but they have done changes in same line.

so,let's checkout to main / navigate to main.

When we are trying to merge ,we obtain the merge conflict, in past experience ,Ai has taken 2 hrs to resolve this ,currently I am resolving manually.

Now we need to work from vs code.Here we will fix the merge conflict.

git merge feature

after merging , we will delete the branch using

git branch -d feature.

git push command sends the code to github repo and

git push -u upstream main, you need to setup upsteam for first time.

second time , git push will work.

Getting Started Step-by-Step

1- Install Git: Download from https://git-scm.com/downloads.

2-Configure Git (first time only)

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

3-Create a New Repository:

mkdir my-project
cd my-project
git init

Add files, then git add . and git commit -m "Initial commit"

4-Work with a Remote (e.g., GitHub)
1-Create a repo on GitHub.
2-Add remote: git remote add origin https://github.com/username/your_url.git
3-Push: git push -u origin main

5- Want to oneline history of commits.

or

git log - shows the author information

commit 2422d08321bdb5d25f87873238d3f8c4284a9aef (HEAD -> main)
Merge: 157448e 793a55d
Author: bhupeshjoshi
Date: Mon Dec 29 15:56:14 2025 +0530

short commit : this is hello from me
Enter fullscreen mode Exit fullscreen mode

git diff vs git diff main

What is git diff

git diff shows what text changed, where it changed, and how it changed.

It never changes anything.
It only shows differences.

Git diff main
It shows how your local file is differ from main branch.

git revert HEAD - DO this if you want to undo commit ,if it is already published/pushed.
It is similar to git revert 793a55d , here we will shift to this commit.

git revert and git reset the main difference I noted git reset is for local repo,it doesn't perform commit, but git revert performs one commit for you.

Pro Tip: Configure once with git config --global user.name "Your Name" and user.email "email@example.com". Practice in a test repo, and explore git help for details. For more, visit git-scm.com. Happy coding! 🚀

Top comments (0)