Back to All Sheets
Gitv2.45INTERMEDIATE

Git Modern Command Reference & Internal Architecture

High-density Git cheat sheet covering interactive staging, branch rebase workflows, reflog recovery, and visual branch architecture.

12 min read
Category: DevOps

Core Commands & Staging

Staging files, interactive hunk commits, and status checking.

Interactive Staging & Commits

Selectively stage hunks and format conventional commits.

Staging Command Reference
CommandScopeWorking Tree EffectUse Case
git add <path>Selected file or pathNoneStage a complete intentional change
git add -pSelected hunksNoneSplit mixed edits into focused commits
git restore --staged <path>Selected staged pathKeeps working-tree editsRemove a file from the next commit
git diffUnstaged changesNoneReview what remains outside the index
git diff --stagedStaged changesNoneReview the exact next commit
# Interactive hunk staging
git add -p
# Stage all tracked modifications
git add -u
# Commit with inline message
git commit -m "feat: add user authentication flow"
Code Annotations:
git add -p

Interactively prompts to stage individual code hunks instead of entire files

πŸ’‘
Conventional Commits

Use prefixes like feat:, fix:, docs:, refactor:, or test: for automated changelogs.

stagingcommitadd

Branching & Interactive Rebase

Branch management, rebasing onto main, and interactive squashing.

Branch Workflows & Interactive Rebase

Maintain a clean linear git history with interactive rebasing.

Visual Architecture & Flowchart
Mermaid.js
graph TD MainBranch["Main Branch: C1 -> C2 -> C3"] FeatureBranch["Feature Branch: C2 -> F1 -> F2"] RebaseResult["Rebased Linear: C1 -> C2 -> C3 -> F1' -> F2'"] MainBranch --> RebaseResult FeatureBranch --> RebaseResult
# Create and switch to feature branch
git checkout -b feature/login
# Rebase feature branch onto updated main
git fetch origin
git rebase origin/main
# Interactive rebase last 4 commits
git rebase -i HEAD~4
Code Annotations:
git rebase -i HEAD~4

Launches interactive editor to pick, reword, squash, or drop commits

⚑
Golden Rule of Rebasing

Never rebase a public branch that other team members are actively pulling from.

πŸ“Œ
Autostash On Rebase

Use git rebase --autostash to automatically stash uncommitted changes before rebasing.

branchingrebasemerge

Undo & Reflog Recovery

Recovering lost commits, soft/hard resets, and reflog time travel.

Git Reflog & Emergency Recovery

Recovering deleted branches or bad resets using HEAD history.

BASH
# Inspect HEAD movement reflog
git reflog
# Restore lost commit HEAD@{2}
git checkout -b recovered-branch HEAD@{2}
# Hard reset to specific reflog entry
git reset --hard HEAD@{1}
Code Annotations:
git reflog

Logs every single HEAD movement locally, even for deleted branches or resets

git reflogΒ»314f44c HEAD@{0}: commit: feat: initial DevSheets MVP
πŸ’‘
Reflog Expiration

Git keeps reflog entries for 90 days before garbage collection runs.

⚑
Hard Reset Danger

git reset --hard permanently discards uncommitted working directory changes.

reflogundorecovery
Related References

Recommended Next Sheets