If you've ever searched:
- “How to undo git reset?”
- “What is detached HEAD in Git?”
- “Git merge vs rebase difference?”
- “Recover lost commits using reflog?”
This guide is for you.
Most developers use Git daily but don’t fully understand how Git works internally. That’s why commands like git reset, git rebase, or git reflog feel dangerous.
In this deep dive, we’ll break down:
- How Git stores commits
- What branches really are
- How HEAD works
- The difference between
git checkout,git reset,git revert - Merge vs Rebase (with diagrams)
- How to fix merge conflicts
- How to recover lost commits using
git reflog
This is a technical but conversational guide designed for intermediate developers who want to stop memorizing commands and start understanding Git.
Git Internals Explained: Git Is a Snapshot Database
At its core, Git is a content-addressable database of snapshots.
Each Git commit contains:
- A full snapshot of your project
- Metadata (author, timestamp, commit message)
- A pointer to its parent commit
- A SHA hash (unique identity)
Unlike other version control systems, Git does not store diffs as history. It stores full project states.
That’s why switching commits is instant — Git already has the snapshot.
How Git Commit History Forms a DAG (Directed Acyclic Graph)
Every commit points backward to its parent.
This structure is called a Directed Acyclic Graph (DAG).
- Directed → Commits point to parents
- Acyclic → No loops
- Graph → Nodes connected by references
Understanding this Git commit graph is essential for understanding git merge, git rebase, and git reset.
What Is a Git Branch Really?
Here’s the mental shift:
A Git branch is just a pointer to a commit.
It is not a copy of your code.
It’s a label.
When you commit:
Git creates a new commit and moves the branch pointer forward.
That’s it.
This is why Git branches are lightweight and fast.
Understanding HEAD in Git
HEAD is a special pointer that tracks where you currently are.
Normally:
HEAD → main → Commit C
When you run:
git checkout feature
HEAD moves to that branch.
What Is Detached HEAD in Git?
If you checkout a specific commit:
git checkout abc123
HEAD now points directly to that commit.
You are in a detached HEAD state.
If you commit now, no branch will track your commit.
This is how developers accidentally “lose” commits.
To save your work:
git checkout -b new-branch-name
Git’s Three Trees: Working Directory, Staging Area, Repository
Understanding Git reset requires understanding Git’s three internal states:
| Layer | Description |
|---|---|
| Working Directory | Your actual files |
| Staging Area (Index) | Files prepared for next commit |
| Repository | Permanent commit history |
Workflow example:
echo "Hello" > file.txt
git add file.txt
git commit -m "Add greeting"
Flow:
- Modify file → Working directory
-
git add→ Staging area -
git commit→ Repository
This is critical for understanding git reset soft vs mixed vs hard.
Git Reset Explained (Soft, Mixed, Hard)
Many developers misuse git reset.
Let’s clarify it completely.
git reset --soft
git reset --soft HEAD~1
- Moves branch pointer
- Keeps changes staged
- Does NOT modify working directory
Use case: Combine commits.
git reset (Mixed)
git reset HEAD~1
- Moves branch pointer
- Unstages changes
- Keeps working directory changes
Use case: Reorganize commits.
git reset --hard
git reset --hard HEAD~1
- Moves branch pointer
- Clears staging area
- Resets working directory
- Deletes uncommitted changes
⚠️ This is destructive.
Never use hard reset casually.
Git Revert vs Git Reset
| Command | Rewrites History? | Safe for Shared Branch? |
|---|---|---|
| git reset | Yes | ❌ No |
| git revert | No | ✅ Yes |
git revert creates a new commit that undoes a previous commit.
git revert abc123
It is the safest way to undo changes on public branches.
Git Merge vs Git Rebase (Complete Visual Explanation)
Scenario:
Feature branch:
- B
- C
Main branch:
- X
- Y
Git Merge
git merge feature
- Preserves history
- Shows true parallel development
- Safe for shared branches
Git Rebase
git rebase main
Git:
- Replays commits
- Creates new commit hashes
- Rewrites history
Golden Rule:
Never rebase commits that others have already pulled.
How Git Merge Conflicts Happen
Conflicts occur when:
- Two branches modify the same lines
- One deletes a file the other modifies
- Branches diverge for too long
You’ll see:
<<<<<<< HEAD
Your code
=======
Their code
>>>>>>> feature
To resolve:
- Edit the file
- Remove markers
git addgit commit
Merge conflicts are normal in collaborative Git workflows.
Recover Lost Commits with Git Reflog
If you accidentally run:
git reset --hard
Or mess up a rebase:
Run:
git reflog
Example:
abc123 HEAD@{0}: commit
def456 HEAD@{1}: reset
Recover:
git checkout -b recovery abc123
Git rarely deletes data immediately.
git reflog is your safety net.
Best Git Workflow for Teams
For scalable Git workflows:
- Create feature branches
- Push regularly
- Open Pull Requests
- Merge into main
- Avoid force pushing shared branches
Keep branches short-lived to reduce merge conflicts.
Frequently Asked Questions (FAQ)
What is Git internally?
Git is a distributed version control system that stores snapshots of your project in a Directed Acyclic Graph (DAG).
What is the difference between git reset and git revert?
git reset rewrites history.
git revert creates a new commit to undo changes.
When should I use git rebase instead of merge?
Use rebase for cleaning up local commits.
Use merge for integrating shared work safely.
How do I recover lost commits in Git?
Use git reflog to find previous HEAD positions and recreate a branch.
Final Thoughts
Understanding Git internals changes everything.
Instead of memorizing commands, you’ll:
- Think in commit graphs
- Understand pointer movement
- Choose the right undo strategy
- Recover safely from mistakes
When you understand Git’s internals —
you stop fearing Git.
You start controlling it.








Top comments (1)
This is one of the clearest explanations of Git internals I have seen. Most tutorials focus on what commands do, but this breaks down why they behave that way by connecting everything to the commit graph, pointers, and the three tree architecture. That mental model is the real turning point in mastering Git. Thanks, Buddy. ❤️