DEV Community

Cover image for Git for Beginners: From “What Is This?” to Your First Commit
Subhrangsu Bera
Subhrangsu Bera

Posted on

Git for Beginners: From “What Is This?” to Your First Commit

Have you ever worked on a project, made a mistake, and wished you could just “Control + Z” your entire life back to two hours ago?
Or collaborated on a group project where everyone emailed files named:

final_v2_REALLY_FINAL.docx
Enter fullscreen mode Exit fullscreen mode

That frustration is exactly why Git exists.

What Is Git?

Git is a Version Control System (VCS).

Think of it as a high-tech “Save Game” system for your code.
Instead of overwriting files, Git records snapshots of your project over time.

This allows you to:

  • Go back in time
  • Experiment safely
  • Work with others without breaking each other’s code

📌 Git is the tool.
GitHub / GitLab are places where Git repositories are stored remotely.


Why Do We Use Git?

Git solves real developer problems:

  • History – See what changed, when, and why
  • Safety – Broke something? Revert instantly
  • Collaboration – Multiple developers, zero chaos

Core Git Terminologies (Very Important)

Before touching commands, you need Git’s mental model.


1. Repository (Repo)

A repository is a project tracked by Git.

  • Local repository → on your computer
  • Remote repository → on GitHub / GitLab / Bitbucket

When you run:

git init
Enter fullscreen mode Exit fullscreen mode

Git creates a hidden folder:

.git/
Enter fullscreen mode Exit fullscreen mode

📌 This .git folder is the brain of Git
Everything lives here: commits, branches, history, and HEAD.


2. Working Directory

This is where you:

  • Write code
  • Edit files
  • Delete files

Example:

index.html
app.js
style.css
Enter fullscreen mode Exit fullscreen mode

⚠️ These files are not tracked automatically.


3. Staging Area (The Most Important Concept)

Git does not commit directly from your files.

Instead, it uses a middle layer:

Working Directory
       ↓ git add
Staging Area
       ↓ git commit
Repository (.git)
Enter fullscreen mode Exit fullscreen mode

Why does the staging area exist?

  • Lets you choose what to save
  • Prevents accidental commits
  • Gives fine-grained control

4. Commit

A commit is a snapshot of your project at a moment in time.

Each commit:

  • Has a unique hash
  • Stores file snapshots
  • Points to the previous commit

Think of it as:

🎮 “Save checkpoint with a message”


5. Commit Hash

Example history:

commit 1 → hash-01
commit 2 → hash-02
commit 3 → hash-03
Enter fullscreen mode Exit fullscreen mode

A hash is used to:

  • Compare changes
  • Revert versions
  • Reset history

6. HEAD (Internal but Critical)

HEAD points to the current branch, which points to the latest commit.

commit-01 ← commit-02 ← commit-03
                              ↑
                             HEAD
Enter fullscreen mode Exit fullscreen mode
  • New commit → HEAD moves forward
  • Reset → HEAD moves backward

The Git Workflow: How It Works Inside

Every Git workflow follows this exact path:

  1. Modify files in the Working Directory
  2. Add selected changes to the Staging Area
  3. Commit them to the Repository

Once this clicks, Git becomes easy.


Essential Git Commands (90% Daily Use)


1. Start a Project

git init
Enter fullscreen mode Exit fullscreen mode

Initializes a Git repository.

To see the hidden folder:

ls -a
Enter fullscreen mode Exit fullscreen mode

2. Track Changes

git status
Enter fullscreen mode Exit fullscreen mode

Shows:

  • Untracked files (U)
  • Modified files (M)
  • Staged files
git add filename
Enter fullscreen mode Exit fullscreen mode

Or add everything:

git add .
Enter fullscreen mode Exit fullscreen mode

Moves files:

Working Directory → Staging Area
Enter fullscreen mode Exit fullscreen mode

3. Commit Changes

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

This:

  • Saves staged changes
  • Creates a new commit
  • Moves HEAD forward

4. Inspect History

git log
Enter fullscreen mode Exit fullscreen mode

Short version:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Example:

a1b2c3d Added feature
b2c3d4e Fixed bug
c3d4e5f Initial commit
Enter fullscreen mode Exit fullscreen mode

5. See What Changed

git diff
Enter fullscreen mode Exit fullscreen mode

Compare commits:

git diff <hash1> <hash2>
Enter fullscreen mode Exit fullscreen mode

Understanding Git Internals

Commit Chain (Linked List)

Commit-01 ← Commit-02 ← Commit-03 ← Commit-04
                                         ↑
                                        HEAD
Enter fullscreen mode Exit fullscreen mode

Each commit stores:

  • Snapshot of files
  • Reference to the previous commit

This is why Git is fast and reliable.


Peeking Inside Git Objects with git cat-file

Git stores everything (commits, files, folders) as objects inside the .git directory.

The command that lets us inspect these objects is:

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

Example: Inspect a Commit Object

git cat-file -p 2b3f9a
Enter fullscreen mode Exit fullscreen mode

(Git allows short hashes as long as they uniquely identify the object.)

Output looks like:

tree a3f5c9...
parent 91ab2d...
author John Doe <john@email.com>
committer John Doe <john@email.com>

Initial project setup
Enter fullscreen mode Exit fullscreen mode

What You’re Seeing

  • tree → snapshot of files
  • parent → previous commit
  • author / committer → who made the change
  • message → commit message

📌 Git commits don’t store diffs — they store snapshots + references.


Branching (Visualized)

A branch is just a pointer to a commit.

main:     A → B → C → D
                 \
feature:           E → F
Enter fullscreen mode Exit fullscreen mode

HEAD switches between branches.

git branch
Enter fullscreen mode Exit fullscreen mode

Check HEAD directly:

cat .git/HEAD
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

Safe Undo (Recommended)

git revert <hash>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that undoes a previous one
(best for shared projects).


⚠️ Dangerous Undo

git reset --hard <hash>
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Moves HEAD backward
  • Deletes commits after it
  • Resets files completely

Use only when you know what you’re doing.


A Typical Beginner Workflow

  1. Initialize → git init
  2. Create file → index.html
  3. Check → git status
  4. Stage → git add index.html
  5. Commit → git commit -m "Initial project setup"
  6. Review → git log --oneline

Git Working Areas (Final Summary)

[ Working Directory ]
        ↓ git add
[ Staging Area ]
        ↓ git commit
[ Repository (.git) ]
Enter fullscreen mode Exit fullscreen mode

This is the heart of Git.


Git Cheatsheet (Everything We Learned)

Repository & Setup

git init
ls -a
Enter fullscreen mode Exit fullscreen mode

Checking State

git status
Enter fullscreen mode Exit fullscreen mode

Staging

git add <file>
git add .
Enter fullscreen mode Exit fullscreen mode

Committing

git commit -m "message"
Enter fullscreen mode Exit fullscreen mode

History

git log
git log --oneline
Enter fullscreen mode Exit fullscreen mode

Differences

git diff
git diff <hash1> <hash2>
Enter fullscreen mode Exit fullscreen mode

Internals

git cat-file -p <hash>
cat .git/HEAD
Enter fullscreen mode Exit fullscreen mode

Branching

git branch
Enter fullscreen mode Exit fullscreen mode

Undo

git revert <hash>
git reset --hard <hash>
Enter fullscreen mode Exit fullscreen mode

If this cheatsheet makes sense —
you understand real Git, not just commands.

Conclusion

Git feels intimidating at first because of:

  • Hashes
  • Commands
  • Terminal usage

But once you understand:

Working Directory → Staging Area → Repository → HEAD

Git stops being scary and starts feeling powerful.

You’re no longer afraid of mistakes —
because Git remembers everything.

Top comments (0)