Cheat SheetsGitBeginner

Beginner — Cheat Sheet

Git · 5 topics. Download the PDF or the Instagram carousel and share it.

Cheat Sheet · AiCanCode.org
Beginner
Git5 topicsQuick revision reference
1

What is Git & Why Version Control?

Git is a distributed version control system that tracks every change to your codebase, lets multiple developers collaborate safely, and makes any version of your project recoverable.

  • Git records snapshots of your entire project, not diffs — this makes switching branches instant
  • Every developer has a full local copy — work offline, commit, branch, and merge without a server
  • The three areas: Working Directory (edit) → Staging Area (git add) → Repository (git commit)
  • git status shows you exactly where every changed file lives across the three areas
  • Git was created in 2005 by Linus Torvalds to manage the Linux kernel — built for massive scale
The chaos Git replaces
// Without Git ❌
project-v1.zip
project-v1-final.zip
project-v1-final-FINAL.zip
project-v1-ACTUALLY-FINAL-johns-changes.zip

// With Git ✅
git log --oneline
a3f8c12  Add payment gateway integration
9d2e441  Fix null pointer in checkout flow
3b7f920  Implement shopping cart
1a2b3c4  Initial commit
2

Git Internals — How Git Really Works

Git stores everything as four object types in a content-addressable store: blobs (file content), trees (directories), commits (snapshots), and tags. Every object is identified by its SHA-1 hash.

  • Four object types: blob (file content), tree (directory), commit (snapshot + metadata), tag (named pointer)
  • Every object is identified by SHA-1 hash of its content — same content = same hash, stored once
  • Commits form a DAG — each commit points to parent(s), never backward
  • Branches and HEAD are just files containing a 40-character SHA-1 hash — that's why branching is instant
  • git cat-file -t <hash> and git cat-file -p <hash> let you inspect any object directly
Inspecting Git objects directly
// Look inside Git's object store
ls .git/objects/
# 4a/ 9c/ f2/ info/ pack/

# See the type and content of any object by its hash
git cat-file -t 4a7f3b2c   # → blob
git cat-file -p 4a7f3b2c   # → "function login() { ... }"

git cat-file -t 9c2d8e1f   # → tree
git cat-file -p 9c2d8e1f
# 100644 blob 4a7f3b2c  auth.js
# 100644 blob 7e9f1a0b  index.js
# 040000 tree 2b4c8d6e  components/

git cat-file -t f2a1c9b3   # → commit
git cat-file -p f2a1c9b3
# tree    9c2d8e1f
# parent  3a5b2c1d
# author  Akshay <a@example.com> 1704067200 +0530
# committer Akshay <a@example.com> 1704067200 +0530
#
# Add login validation
3

Core Workflow — init, add, commit, log

The daily Git loop: initialise a repo, stage changes precisely with git add, commit with a meaningful message, and explore history with git log. Mastering these five commands covers 80% of day-to-day Git usage.

  • git add stages changes — it controls exactly what goes into the next commit, not just which files
  • git add -p (patch mode) lets you stage individual hunks — make surgical, single-purpose commits
  • Write commit messages in imperative mood: "Fix bug" not "Fixed bug" or "Fixing bug"
  • git diff shows unstaged changes; git diff --staged shows what is about to be committed
  • git log --oneline --graph --all is the best quick overview of branch history
Initialise or clone
# Start a new project
mkdir my-project && cd my-project
git init
# Initialised empty Git repository in .git/

# Clone an existing repository (full history included)
git clone https://github.com/user/project.git
git clone https://github.com/user/project.git my-folder  # custom name

# After cloning, the remote is already configured:
git remote -v
# origin  https://github.com/user/project.git (fetch)
# origin  https://github.com/user/project.git (push)
4

Branching — Create, Switch, Merge

Branches are free in Git — creating one takes microseconds. They let you isolate work (features, fixes, experiments) from the main codebase and merge back when ready.

  • A Git branch is a 41-byte file — creating, switching, and deleting branches is instant
  • Fast-forward merge: no divergence, Git moves the pointer — clean linear history
  • Three-way merge: both branches moved, Git creates a merge commit using the common ancestor
  • Use git switch -c <name> (modern) or git checkout -b <name> (classic) to create and switch
  • git merge --abort cancels an in-progress merge and returns to the pre-merge state
Branch operations
# List all branches (* = current)
git branch
# * main
#   feature/user-auth

# Create a branch and switch to it (preferred modern syntax)
git switch -c feature/payment-gateway

# Older syntax (still works everywhere)
git checkout -b feature/payment-gateway

# Switch between branches
git switch main
git switch feature/payment-gateway

# Create branch from a specific commit or tag
git switch -c hotfix/login-crash origin/main
git switch -c release/v2.0 v1.9.0

# Delete a branch (safe — checks for unmerged work)
git branch -d feature/payment-gateway
git branch -D feature/payment-gateway  # force delete
5

Working with Remotes — push, pull, fetch

Remotes are named URLs to other copies of your repository. push sends your commits upstream; fetch downloads remote changes without merging; pull = fetch + merge. Understanding the difference between fetch and pull prevents nasty surprises.

  • origin/main is a local snapshot of the remote — it only updates when you run fetch or pull
  • git fetch is always safe — it never modifies your working directory or local branches
  • git pull = git fetch + git merge — it can trigger merge commits or conflicts
  • git pull --rebase avoids merge commits — prefer this for keeping linear history
  • git push --force-with-lease is the safe way to force-push — it fails if the remote has new commits
Managing remotes
# View configured remotes
git remote -v
# origin  git@github.com:user/project.git (fetch)
# origin  git@github.com:user/project.git (push)

# Add a remote (e.g., upstream for open-source forks)
git remote add upstream https://github.com/original/project.git

# Rename/remove a remote
git remote rename origin old-origin
git remote remove upstream

# Change remote URL (e.g., switching HTTPS → SSH)
git remote set-url origin git@github.com:user/project.git
Learn this free with Aria, your AI tutor → AiCanCode.org/learn/git