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:

Working Tree    →  git add  →   Index (Stage)   →  git commit  →   Repository
(your files) (snapshot draft) (commit history)
↑ |
└─────────────────── git checkout / git restore ──────────────────┘
  • 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:

  • Basicsadd, commit, push, fetch/pull, status/diff
  • Branching — creating branches, merging, rebasing, conflict resolution
  • History & Rewritingcherry-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.