Tools
Tools: Mastering Git Internals: A Guide to Git Reset, Rebase, Merge & Reflog
2026-02-16
0 views
admin
Git Internals Explained: Git Is a Snapshot Database ## How Git Commit History Forms a DAG (Directed Acyclic Graph) ## What Is a Git Branch Really? ## Understanding HEAD in Git ## What Is Detached HEAD in Git? ## Git’s Three Trees: Working Directory, Staging Area, Repository ## Git Reset Explained (Soft, Mixed, Hard) ## git reset --soft ## git reset (Mixed) ## git reset --hard ## Git Revert vs Git Reset ## Git Merge vs Git Rebase (Complete Visual Explanation) ## Git Merge ## Git Rebase ## How Git Merge Conflicts Happen ## Recover Lost Commits with Git Reflog ## Best Git Workflow for Teams ## Frequently Asked Questions (FAQ) ## What is Git internally? ## What is the difference between git reset and git revert? ## When should I use git rebase instead of merge? ## How do I recover lost commits in Git? ## Final Thoughts If you've ever searched: 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: This is a technical but conversational guide designed for intermediate developers who want to stop memorizing commands and start understanding Git. At its core, Git is a content-addressable database of snapshots. Each Git commit contains: 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. Every commit points backward to its parent. This structure is called a Directed Acyclic Graph (DAG). Understanding this Git commit graph is essential for understanding git merge, git rebase, and git reset. Here’s the mental shift: A Git branch is just a pointer to a commit. It is not a copy of your code. Git creates a new commit and moves the branch pointer forward. This is why Git branches are lightweight and fast. HEAD is a special pointer that tracks where you currently are. HEAD moves to that branch. If you checkout a specific commit: 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. Understanding Git reset requires understanding Git’s three internal states: This is critical for understanding git reset soft vs mixed vs hard. Many developers misuse git reset. Let’s clarify it completely. Use case: Combine commits. Use case: Reorganize commits. ⚠️ This is destructive. Never use hard reset casually. git revert creates a new commit that undoes a previous commit. It is the safest way to undo changes on public branches. Never rebase commits that others have already pulled. Conflicts occur when: Merge conflicts are normal in collaborative Git workflows. If you accidentally run: Git rarely deletes data immediately. git reflog is your safety net. For scalable Git workflows: Keep branches short-lived to reduce merge conflicts. Git is a distributed version control system that stores snapshots of your project in a Directed Acyclic Graph (DAG). git reset rewrites history.
git revert creates a new commit to undo changes. Use rebase for cleaning up local commits.
Use merge for integrating shared work safely. Use git reflog to find previous HEAD positions and recreate a branch. Understanding Git internals changes everything. Instead of memorizing commands, you’ll: When you understand Git’s internals —
you stop fearing Git. You start controlling it. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK:
HEAD → main → Commit C Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
HEAD → main → Commit C CODE_BLOCK:
HEAD → main → Commit C COMMAND_BLOCK:
git checkout feature Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git checkout feature COMMAND_BLOCK:
git checkout feature COMMAND_BLOCK:
git checkout abc123 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git checkout abc123 COMMAND_BLOCK:
git checkout abc123 COMMAND_BLOCK:
git checkout -b new-branch-name Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git checkout -b new-branch-name COMMAND_BLOCK:
git checkout -b new-branch-name COMMAND_BLOCK:
echo "Hello" > file.txt
git add file.txt
git commit -m "Add greeting" Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
echo "Hello" > file.txt
git add file.txt
git commit -m "Add greeting" COMMAND_BLOCK:
echo "Hello" > file.txt
git add file.txt
git commit -m "Add greeting" COMMAND_BLOCK:
git reset --soft HEAD~1 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git reset --soft HEAD~1 COMMAND_BLOCK:
git reset --soft HEAD~1 COMMAND_BLOCK:
git reset HEAD~1 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git reset HEAD~1 COMMAND_BLOCK:
git reset HEAD~1 COMMAND_BLOCK:
git reset --hard HEAD~1 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git reset --hard HEAD~1 COMMAND_BLOCK:
git reset --hard HEAD~1 COMMAND_BLOCK:
git revert abc123 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git revert abc123 COMMAND_BLOCK:
git revert abc123 COMMAND_BLOCK:
git merge feature Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git merge feature COMMAND_BLOCK:
git merge feature COMMAND_BLOCK:
git rebase main Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git rebase main COMMAND_BLOCK:
git rebase main COMMAND_BLOCK:
<<<<<<< HEAD
Your code
=======
Their code
>>>>>>> feature Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
<<<<<<< HEAD
Your code
=======
Their code
>>>>>>> feature COMMAND_BLOCK:
<<<<<<< HEAD
Your code
=======
Their code
>>>>>>> feature COMMAND_BLOCK:
git reset --hard Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git reset --hard COMMAND_BLOCK:
git reset --hard COMMAND_BLOCK:
git reflog Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
abc123 HEAD@{0}: commit
def456 HEAD@{1}: reset Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
abc123 HEAD@{0}: commit
def456 HEAD@{1}: reset CODE_BLOCK:
abc123 HEAD@{0}: commit
def456 HEAD@{1}: reset COMMAND_BLOCK:
git checkout -b recovery abc123 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
git checkout -b recovery abc123 COMMAND_BLOCK:
git checkout -b recovery abc123 - “How to undo git reset?”
- “What is detached HEAD in Git?”
- “Git merge vs rebase difference?”
- “Recover lost commits using reflog?” - 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 - A full snapshot of your project
- Metadata (author, timestamp, commit message)
- A pointer to its parent commit
- A SHA hash (unique identity) - Directed → Commits point to parents
- Acyclic → No loops
- Graph → Nodes connected by references - Modify file → Working directory
- git add → Staging area
- git commit → Repository - Moves branch pointer
- Keeps changes staged
- Does NOT modify working directory - Moves branch pointer
- Unstages changes
- Keeps working directory changes - Moves branch pointer
- Clears staging area
- Resets working directory
- Deletes uncommitted changes - Preserves history
- Shows true parallel development
- Safe for shared branches - Replays commits
- Creates new commit hashes
- Rewrites history - Two branches modify the same lines
- One deletes a file the other modifies
- Branches diverge for too long - Edit the file
- Remove markers - Create feature branches
- Push regularly
- Open Pull Requests
- Merge into main
- Avoid force pushing shared branches - Think in commit graphs
- Understand pointer movement
- Choose the right undo strategy
- Recover safely from mistakes
how-totutorialguidedev.toaiswitchnodedatabasegit