Skip to main content

Git Knowledge Base

A comprehensive reference for everything Git β€” from staging your first file to rewriting history, managing complex merges, and running team-wide workflows.

What is Git?

Git is a distributed version control system (VCS) created by Linus Torvalds in 2005. Every developer has a full copy of the repository β€” including its complete history β€” on their local machine. This means most operations are instant, offline-capable, and non-destructive.

Git tracks changes as a directed acyclic graph (DAG) of commits, where each commit is an immutable snapshot of the entire project tree at a point in time (not a diff). Branches are simply lightweight, movable pointers to commits.


Core Concepts at a Glance

ConceptWhat it is
RepositoryThe .git directory containing all history, objects, and refs
Working TreeYour actual files on disk β€” what you edit
Index / Staging AreaThe preparation zone between working tree and commits
CommitAn immutable snapshot of the staged tree plus metadata
BranchA movable pointer to a commit (refs/heads/name)
HEADA pointer to the currently checked-out commit or branch
RemoteA named URL to another repository (e.g., origin)
TagAn immutable pointer to a commit β€” usually a release marker

The Three Trees

Understanding Git's three trees is the key to understanding almost every Git command:

🌴Git Trees: STAGE

Working Tree(Your Files on Disk)Staging Area(Index Snapshot Draft)Local Repository(Immutable Commits)git addgit commitgit checkout / git restore

Staging Area (Index)

Syntax: git add <file>

Description: Prepares local working changes to be recorded in the next history commit.

  • Internal Mechanics:
    • Moves your changes from the Working Tree (local disk) to the Staging Area.
    • Computes file content SHA-1 hashes and creates blob objects in Git's index database.
    • Allows selective chunk/hunk commits using command flags like git add -p.

πŸ’‘ Use the tabs above to trigger file flows between Working Tree, Index/Stage, and Repository states.

  • git add moves changes from the working tree into the index
  • git commit moves changes from the index into the repository
  • git checkout / git restore moves changes from the repository or index back into the working tree

Quick Command Reference

TaskCommand
Stage all changesgit add .
Commit with messagegit commit -m "feat: description"
Push to remotegit push origin branch-name
Pull latest changesgit pull --rebase origin main
Create and switch branchgit switch -c feature/my-feature
Merge a branchgit merge --no-ff feature/my-feature
Rebase onto maingit rebase main
Interactive rebasegit rebase -i HEAD~5
Cherry-pick a commitgit cherry-pick <sha>
Squash last N commitsgit rebase -i HEAD~N
Stash work-in-progressgit stash push -m "wip: description"
Reset to previous commitgit reset --soft HEAD~1
Undo a commit safelygit revert <sha>
Find which commit introduced a buggit bisect start
View full history graphgit log --oneline --graph --all

How to Use This Documentation

Use the sidebar to navigate by topic area:

  • Basics β€” add, commit, push, fetch/pull, status/diff
  • Branching β€” creating branches, merging, rebasing, conflict resolution
  • History & Rewriting β€” cherry-pick, squash, fixup, reset, revert, reflog
  • Collaboration β€” remotes, tags, stash, submodules
  • Advanced β€” hooks, aliases, bisect, worktrees
  • Workflows β€” Git Flow, trunk-based development, conventional commits, PR best practices
Golden Rule of Git

Never rewrite history on shared branches. Rebase, squash, and fixup are safe on your own feature branches. Once a branch is pushed and shared with others, use git revert instead.

πŸ“–
Track Page Progress0 / 635 Read
Knowledge Base Completion0%