DEV Community

Cover image for Git — The Complete Guide (From Internals to Daily Workflow)
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on • Edited on

Git — The Complete Guide (From Internals to Daily Workflow)

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

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

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

Git creates a blob object:

git hash-object file.txt
Enter fullscreen mode Exit fullscreen mode

Output:

a1b2c3...
Enter fullscreen mode Exit fullscreen mode

Blob contains:

"Hello Git\n"
Enter fullscreen mode Exit fullscreen mode

6. Tree Object (Directory Structure)

A tree maps:

  • Filenames
  • Permissions
  • Blob hashes

Example tree:

100644 blob a1b2c3 file.txt
Enter fullscreen mode Exit fullscreen mode

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

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

9. Content Addressing (SHA-1 / SHA-256)

Git uses hashing:

SHA1(content + metadata)
Enter fullscreen mode Exit fullscreen mode

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

Example:

ref: refs/heads/main
Enter fullscreen mode Exit fullscreen mode

12. Branches (What They Really Are)

A branch is just a pointer.

.git/refs/heads/main
Enter fullscreen mode Exit fullscreen mode

Contains:

<commit_hash>
Enter fullscreen mode Exit fullscreen mode

Creating a branch:

git branch feature
Enter fullscreen mode Exit fullscreen mode

No copy. No duplication. Just a pointer.


13. Detached HEAD Explained

Occurs when:

git checkout <commit_hash>
Enter fullscreen mode Exit fullscreen mode

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

Clone

git clone <repo>
Enter fullscreen mode Exit fullscreen mode

Status

git status
Enter fullscreen mode Exit fullscreen mode

Add

git add file.txt
git add .
Enter fullscreen mode Exit fullscreen mode

Commit

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

16. Git Diff (How Changes Are Shown)

git diff
git diff --staged
git diff HEAD
Enter fullscreen mode Exit fullscreen mode

Diff is computed on demand, not stored.


17. Git Log (History Inspection)

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

18. Undoing Changes (Critical)

Undo working directory

git checkout -- file.txt
Enter fullscreen mode Exit fullscreen mode

Unstage

git reset file.txt
Enter fullscreen mode Exit fullscreen mode

Amend commit

git commit --amend
Enter fullscreen mode Exit fullscreen mode

19. Git Reset (Soft, Mixed, Hard)

git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode
Type Effect
soft Keep staged
mixed Keep changes
hard Destroy changes

20. Git Revert (Safe Undo)

git revert <commit>
Enter fullscreen mode Exit fullscreen mode

Creates a new commit that reverses changes.


21. Git Merge (Internals)

Merge types:

  • Fast-forward
  • Three-way merge
git merge feature
Enter fullscreen mode Exit fullscreen mode

Git finds:

  • Common ancestor
  • Applies diffs

22. Merge Conflicts

Occurs when:

  • Same lines changed differently

Markers:

<<<<<<<
=======
>>>>>>>
Enter fullscreen mode Exit fullscreen mode

Resolve manually → commit.


23. Git Rebase (History Rewriting)

git rebase main
Enter fullscreen mode Exit fullscreen mode

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

25. Stash (Temporary Storage)

git stash
git stash pop
git stash list
Enter fullscreen mode Exit fullscreen mode

Stored internally as commits.


26. Remote Repositories

git remote add origin <url>
git fetch
git pull
git push
Enter fullscreen mode Exit fullscreen mode

Fetch vs Pull

  • fetch → download
  • pull → fetch + merge/rebase

27. Git Hooks (Automation)

Stored in:

.git/hooks/
Enter fullscreen mode Exit fullscreen mode

Examples:

  • pre-commit
  • pre-push
  • post-merge

Shell scripts executed automatically.


28. Git Ignore

.gitignore
Enter fullscreen mode Exit fullscreen mode

Rules:

node_modules/
.env
*.log
Enter fullscreen mode Exit fullscreen mode

Ignored files are not tracked.


29. Git Configuration

git config --global user.name "Name"
git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

Stored in:

  • ~/.gitconfig
  • .git/config

30. Git Internals Command Line

Inspect objects:

git cat-file -p <hash>
git ls-tree <tree_hash>
Enter fullscreen mode Exit fullscreen mode

31. Git Garbage Collection

git gc
Enter fullscreen mode Exit fullscreen mode
  • Compresses objects
  • Removes unreachable data

32. Git Packfiles

Optimized storage:

  • Delta compression
  • Binary format

Stored in:

.git/objects/pack/
Enter fullscreen mode Exit fullscreen mode

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)