DEV Community

Cover image for Mastering Git Internals: A Guide to Git Reset, Rebase, Merge & Reflog
Arijit Ghosh
Arijit Ghosh

Posted on

Mastering Git Internals: A Guide to Git Reset, Rebase, Merge & Reflog

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

When you run:

git checkout feature
Enter fullscreen mode Exit fullscreen mode

HEAD moves to that branch.


What Is Detached HEAD in Git?

If you checkout a specific commit:

git checkout abc123
Enter fullscreen mode Exit fullscreen mode

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

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

Flow:

  1. Modify file → Working directory
  2. git add → Staging area
  3. 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
Enter fullscreen mode Exit fullscreen mode
  • Moves branch pointer
  • Keeps changes staged
  • Does NOT modify working directory

Use case: Combine commits.


git reset (Mixed)

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Moves branch pointer
  • Unstages changes
  • Keeps working directory changes

Use case: Reorganize commits.


git reset --hard

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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

  • Preserves history
  • Shows true parallel development
  • Safe for shared branches

Git Rebase

git rebase main
Enter fullscreen mode Exit fullscreen mode

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

To resolve:

  1. Edit the file
  2. Remove markers
  3. git add
  4. git commit

Merge conflicts are normal in collaborative Git workflows.


Recover Lost Commits with Git Reflog

If you accidentally run:

git reset --hard
Enter fullscreen mode Exit fullscreen mode

Or mess up a rebase:

Run:

git reflog
Enter fullscreen mode Exit fullscreen mode

Example:

abc123 HEAD@{0}: commit
def456 HEAD@{1}: reset
Enter fullscreen mode Exit fullscreen mode

Recover:

git checkout -b recovery abc123
Enter fullscreen mode Exit fullscreen mode

Git rarely deletes data immediately.

git reflog is your safety net.


Best Git Workflow for Teams

For scalable Git workflows:

  1. Create feature branches
  2. Push regularly
  3. Open Pull Requests
  4. Merge into main
  5. 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)

Collapse
 
biru_mishra_4 profile image
Biru mishra

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. ❤️