Git is not just a version control tool. It is a distributed content-addressable filesystem with a powerful history model built on immutable data structures.
This article explains Git completely, from internal architecture to every major concept and command, with clear explanations and code examples.
1. What Git Really Is (Mental Model)
Most developers think:
“Git tracks file changes.”
This is incorrect.
Git actually does:
- Tracks snapshots, not diffs
- Stores objects, not files
- Uses hashes, not filenames
- Works like a mini filesystem + database
Git is:
- Distributed
- Immutable
- Content-addressed
- Snapshot-based
2. Git Architecture (High Level)
Git consists of three layers:
Working Directory
Staging Area (Index)
Repository (.git)
2.1 Working Directory
- Your actual project files
- Editable
- Not tracked unless added
2.2 Staging Area (Index)
- A buffer between working directory and repository
- Defines what goes into the next commit
- Unique to Git (many VCS tools do not have this)
2.3 Repository (.git)
- Stores all history
-
Contains:
- Objects
- References
- Config
- Hooks
3. The .git Directory (Internals)
Inside every Git repository:
.git/
├── objects/
├── refs/
├── HEAD
├── index
├── config
├── hooks/
├── logs/
Each component has a purpose.
4. Git Objects (Core Internal System)
Git has four object types:
| Object | Purpose |
|---|---|
| blob | File content |
| tree | Directory structure |
| commit | Snapshot metadata |
| tag | Named reference |
5. Blob Object (File Content)
A blob stores:
- File content
- No filename
- No permissions
Example:
echo "Hello Git" > file.txt
git add file.txt
Git creates a blob object:
git hash-object file.txt
Output:
a1b2c3...
Blob contains:
"Hello Git\n"
6. Tree Object (Directory Structure)
A tree maps:
- Filenames
- Permissions
- Blob hashes
Example tree:
100644 blob a1b2c3 file.txt
Trees can reference:
- Blobs
- Other trees (subdirectories)
7. Commit Object (Snapshot)
A commit references:
- A tree
- Parent commit(s)
- Author
- Committer
- Message
Structure:
tree <tree_hash>
parent <parent_hash>
author ...
committer ...
commit message
Commits are immutable.
8. Tag Object
Two types:
- Lightweight tag
- Annotated tag
Annotated tags are objects.
git tag -a v1.0 -m "Release 1.0"
9. Content Addressing (SHA-1 / SHA-256)
Git uses hashing:
SHA1(content + metadata)
Benefits:
- Integrity
- Deduplication
- Immutability
Same content → same hash → stored once.
10. Git Index (Staging Area Internals)
The index:
- Binary file:
.git/index -
Stores:
- File path
- File hash
- Permissions
- Timestamp
Used to:
- Build the next tree
- Compare working directory changes
11. HEAD and References
HEAD
Points to:
- Current branch
- Or a commit (detached HEAD)
cat .git/HEAD
Example:
ref: refs/heads/main
12. Branches (What They Really Are)
A branch is just a pointer.
.git/refs/heads/main
Contains:
<commit_hash>
Creating a branch:
git branch feature
No copy. No duplication. Just a pointer.
13. Detached HEAD Explained
Occurs when:
git checkout <commit_hash>
HEAD points directly to a commit.
Changes can be lost unless a branch is created.
14. Git Workflow States
File states:
| State | Description |
|---|---|
| Untracked | New file |
| Modified | Changed |
| Staged | Ready to commit |
| Committed | Stored |
15. Basic Git Commands (With Meaning)
Initialize
git init
Clone
git clone <repo>
Status
git status
Add
git add file.txt
git add .
Commit
git commit -m "message"
16. Git Diff (How Changes Are Shown)
git diff
git diff --staged
git diff HEAD
Diff is computed on demand, not stored.
17. Git Log (History Inspection)
git log
git log --oneline
git log --graph --all
18. Undoing Changes (Critical)
Undo working directory
git checkout -- file.txt
Unstage
git reset file.txt
Amend commit
git commit --amend
19. Git Reset (Soft, Mixed, Hard)
git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>
| Type | Effect |
|---|---|
| soft | Keep staged |
| mixed | Keep changes |
| hard | Destroy changes |
20. Git Revert (Safe Undo)
git revert <commit>
Creates a new commit that reverses changes.
21. Git Merge (Internals)
Merge types:
- Fast-forward
- Three-way merge
git merge feature
Git finds:
- Common ancestor
- Applies diffs
22. Merge Conflicts
Occurs when:
- Same lines changed differently
Markers:
<<<<<<<
=======
>>>>>>>
Resolve manually → commit.
23. Git Rebase (History Rewriting)
git rebase main
Replays commits one by one.
Pros:
- Clean history
Cons:
- Dangerous on shared branches
24. Git Cherry-Pick
Apply a specific commit:
git cherry-pick <hash>
25. Stash (Temporary Storage)
git stash
git stash pop
git stash list
Stored internally as commits.
26. Remote Repositories
git remote add origin <url>
git fetch
git pull
git push
Fetch vs Pull
- fetch → download
- pull → fetch + merge/rebase
27. Git Hooks (Automation)
Stored in:
.git/hooks/
Examples:
- pre-commit
- pre-push
- post-merge
Shell scripts executed automatically.
28. Git Ignore
.gitignore
Rules:
node_modules/
.env
*.log
Ignored files are not tracked.
29. Git Configuration
git config --global user.name "Name"
git config --global user.email "email"
Stored in:
- ~/.gitconfig
- .git/config
30. Git Internals Command Line
Inspect objects:
git cat-file -p <hash>
git ls-tree <tree_hash>
31. Git Garbage Collection
git gc
- Compresses objects
- Removes unreachable data
32. Git Packfiles
Optimized storage:
- Delta compression
- Binary format
Stored in:
.git/objects/pack/
33. Git Security Model
- Hash-based integrity
- Tamper detection
- Full history verification
34. Common Git Workflows
Feature Branch Workflow
- main
- feature/*
- PR merge
Gitflow
- main
- develop
- release
- hotfix
35. Final Mental Model (Important)
Think of Git as:
- A database
- Of snapshots
- Linked by hashes
- Navigated by pointers
Once you understand this:
- Git stops being confusing
- Advanced commands make sense
- You stop fearing mistakes
Conclusion
Git is simple in concept, but deep in implementation.
Understanding Git internals transforms you from:
“Someone who uses Git”
into
“Someone who understands Git”
This knowledge makes you:
- Faster
- Safer
- More confident
Top comments (0)