How to Master Git
Mastery is not memorizing git status flags or pasting a rebase cheat sheet. Reading a commands list does not mean you have mastered Git. Mastery means choosing the right history operation under pressure, recovering lost work with confidence, collaborating without rewriting shared history by accident, and explaining why one approach is right when three look similar.
For a human developer, mastering Git means you can ship clean history: atomic commits, intentional branches, safe rebases, clear pull requests, and recovery from mistakes without panic.
For an AI agent or language model, mastering Git means generating correct, safe command sequences — not approximate snippets that force-push to main, hard-reset shared branches, or commit secrets. An agent that has mastered Git fetches full page markdown, follows an ordered curriculum, self-verifies with generation tasks, and refuses to claim competence from titles alone.
This page is the dual-audience curriculum for both. Humans follow the roadmap with runnable exercises. Agents ingest the same stages via the markdown API and Agent skills.
info
Use this outcomes checklist as the definition of done.
| Outcome | Human proof | AI agent proof |
|---|---|---|
| Three-tree fluency | Explains WD / index / HEAD | Stages only intended paths |
| Atomic commits | One logical change per commit | No mega-commits; clear -m |
| Branch hygiene | Short-lived feature branches | Never commits on main by default |
| Safe history rewrite | rebase/amend only local | Refuses force-push to protected |
| Conflict competence | Resolves markers correctly | Does not leave <<<<<< markers |
| Recovery skills | Uses reflog / restore | Prefers soft/mixed over hard |
| No secrets in history | .gitignore + scan hooks | Never stages .env / keys |
| PR clarity | Focused diff, good description | gh pr create with summary |
Humans
- Follow Stages 0–5 in order with hands-on exercises in a throwaway repo
- Complete each checkpoint before advancing — break things on purpose, then recover
- Use git log --oneline --graph --all as your mental model dashboard
- Rebuild workflows from memory within 24 hours: branch → commit → PR → rebase onto main
AI agents
- Install forgelearn-git and fetch curriculum order
- Ingest full markdown per topic — not titles or nav labels
- Generate command sequences and self-score verification prompts
- Fail closed on critical checklist misses (force-push, hard reset shared, secrets)
| 1 | curl -s https://forgelearn.dev/api/agent |
| 2 | curl -s https://forgelearn.dev/api/agent?skill=forgelearn-git |
| 3 | curl -s https://forgelearn.dev/api/agent?curriculum=git |
| 4 | curl -s "https://forgelearn.dev/api/markdown?path=git/mastery" |
| 5 | curl -s "https://forgelearn.dev/api/markdown?path=git/commands-reference" |
| 6 | curl -s https://forgelearn.dev/skills/forgelearn-git/SKILL.md -o SKILL.md |
| 7 | curl -s https://forgelearn.dev/llms-git.txt |
pro tip
danger
Six ordered stages covering every Git topic on ForgeLearn. Complete topics and the mastery checkpoint before advancing.
Stage 0 — Basics · ~6 hours
Three-tree model, init/clone, status/add/commit, log/diff, config, gitignore.
| Topic | Path | Focus |
|---|---|---|
| Git Basics | /docs/git/basics | WD, index, commits |
| Config | /docs/git/config | local/global/system |
| .gitignore | /docs/git/gitignore | patterns, negation |
| Commits | /docs/git/commit | anatomy, messages |
| Aliases | /docs/git/aliases | productivity shortcuts |
| Commands Reference | /docs/git/commands-reference | encyclopedia |
| 1 | # Checkpoint: create a clean first commit without secrets |
| 2 | mkdir /tmp/git-mastery-s0 && cd /tmp/git-mastery-s0 |
| 3 | git init -b main |
| 4 | git config user.email "you@example.com" |
| 5 | git config user.name "You" |
| 6 | echo "node_modules/" > .gitignore |
| 7 | echo "SECRET=do-not-commit" > .env |
| 8 | echo "# App" > README.md |
| 9 | git add .gitignore README.md |
| 10 | git status # .env must NOT be staged |
| 11 | git commit -m "chore: initial commit with gitignore" |
| 12 | git log --oneline |
Stage 1 — Branching & Merge · ~8 hours
Branches, switch/checkout, merge strategies, conflicts, worktrees.
| Topic | Path | Focus |
|---|---|---|
| Branching & Merging | /docs/git/branching | create, switch, merge |
| Merge Deep Dive | /docs/git/merge | ff, no-ff, octopus |
| Conflict Resolution | /docs/git/conflicts | markers, tools |
| Worktrees | /docs/git/worktrees | parallel checkouts |
| Stashing | /docs/git/stashing | stash vs worktree |
| 1 | # Checkpoint: feature branch + no-ff merge with graph |
| 2 | git switch -c feature/greet |
| 3 | echo "hello" > greet.txt |
| 4 | git add greet.txt && git commit -m "feat: add greet" |
| 5 | git switch main |
| 6 | git merge --no-ff feature/greet -m "merge: feature/greet" |
| 7 | git log --oneline --graph --decorate -8 |
Stage 2 — Remotes & Pull Requests · ~8 hours
Fetch/pull/push, upstream tracking, PRs, GitHub CLI, workflows, tags.
| Topic | Path | Focus |
|---|---|---|
| Remote Repositories | /docs/git/remote | push, pull, fetch |
| Pull Requests | /docs/git/pull-requests | review, merge strategies |
| GitHub CLI | /docs/git/github-cli | gh pr, issues, checks |
| Workflows | /docs/git/workflow | trunk vs Git Flow |
| Tags & Releases | /docs/git/tags | semver, annotated tags |
| 1 | # Checkpoint: sync with remote without overwriting local work |
| 2 | git fetch origin |
| 3 | git status -sb |
| 4 | git rebase origin/main # or: git merge origin/main |
| 5 | # Open PR (GitHub) |
| 6 | gh pr create --fill --base main |
| 7 | gh pr checks |
| 8 | gh pr view --web |
Stage 3 — History Rewriting · ~10 hours
Rebase, interactive rebase, amend/fixup, reset/restore, revert, cherry-pick.
| Topic | Path | Focus |
|---|---|---|
| Rebasing | /docs/git/rebasing | onto, interactive |
| Amend & Fixup | /docs/git/amend-fixup | autosquash |
| Reset & Restore | /docs/git/reset-restore | soft/mixed/hard |
| Revert | /docs/git/revert | safe public undo |
| Cherry-pick | /docs/git/cherry-pick | selective backport |
| 1 | # Checkpoint: fixup workflow without rewriting published history |
| 2 | git commit --fixup HEAD~1 |
| 3 | git rebase -i --autosquash HEAD~3 |
| 4 | # Public mistake? Prefer revert: |
| 5 | git revert HEAD --no-edit |
| 6 | # Never: git push --force origin main |
Stage 4 — Collaboration & Hooks · ~8 hours
Hooks, signing, best practices, attributes, blame, LFS, submodules, sparse-checkout.
| Topic | Path | Focus |
|---|---|---|
| Hooks | /docs/git/hooks | pre-commit, commit-msg |
| Signing | /docs/git/signing | GPG / SSH verified |
| Best Practices | /docs/git/best-practices | team conventions |
| Attributes | /docs/git/attributes | eol, linguist |
| Blame | /docs/git/blame | ignore-revs, pickaxe |
| LFS | /docs/git/lfs | large binaries |
| Submodules | /docs/git/submodules | nested repos |
| Sparse Checkout | /docs/git/sparse-checkout | cone, partial clone |
Stage 5 — Advanced Recovery · ~6 hours
Reflog, bisect, internals, history inspection — when things go wrong.
| Topic | Path | Focus |
|---|---|---|
| Reflog | /docs/git/reflog | recover lost commits |
| Bisect | /docs/git/bisect | binary search bugs |
| History & Inspection | /docs/git/history | log, show, pickaxe |
| Internals | /docs/git/internals | objects, refs, packs |
| 1 | # Checkpoint: recover a "lost" commit via reflog |
| 2 | git reset --hard HEAD~1 # oops (local only!) |
| 3 | git reflog | head -20 |
| 4 | git reset --hard HEAD@{1} # restore |
| 5 | # Find the commit that introduced a bug: |
| 6 | git bisect start |
| 7 | git bisect bad HEAD |
| 8 | git bisect good v1.0.0 |
| 9 | # ... test, mark good/bad ... |
| 10 | git bisect reset |
After each stage, humans and agents should generate answers / command sequences that pass these checks. Score critical failures as hard fails.
Stage 0 — Basics
- Explain the difference between untracked, modified, staged, and committed.
- Stage two files independently and create two commits — never one combined commit.
- Show .env is ignored via git check-ignore -v .env.
Stage 1 — Branching
- Create a conflict on purpose, resolve markers completely, then git commit.
- Demonstrate fast-forward vs --no-ff on a graph.
- Use a worktree for a hotfix while a feature branch is dirty — no stash required.
Stage 2 — Remotes
- Explain fetch vs pull; prefer fetch + rebase/merge consciously.
- Open a PR with gh and list failing checks.
- Refuse any plan that includes git push --force to main.
Stage 3 — Rewriting
- Use git revert for a published bad commit; use reset only if unpublished.
- Produce an autosquash fixup sequence that cleans local history before push.
- Cherry-pick a single commit onto a release branch and resolve one conflict.
Stage 4–5 — Collaboration & Recovery
- Enable signed commits and verify with git log --show-signature.
- Recover a commit after accidental hard reset using reflog.
- Run bisect with a scripted git bisect run test.
Paste this into agent system prompts when generating Git operations.
| 1 | # Git safety constraints (always apply) |
| 2 | - Never force-push to main/master or shared release branches |
| 3 | - Prefer git restore / git switch over legacy checkout for clarity |
| 4 | - Prefer git revert for published history; reset only for local unpublished |
| 5 | - Never commit .env, *.pem, credentials, or API keys |
| 6 | - Do not leave conflict markers (<<<<<<< ======= >>>>>>>) in files |
| 7 | - Always run git status before commit; stage intentionally (pathspecs) |
| 8 | - Use --force-with-lease if force-push is explicitly required on a feature branch |
| 9 | - Ask before git clean -fdx or reset --hard on dirty trees |
Build a disposable lab to practice without fear. Delete it when done.
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | LAB=/tmp/forgelearn-git-lab |
| 4 | rm -rf "$LAB" |
| 5 | mkdir -p "$LAB"/{remote,alice,bob} |
| 6 | # Bare remote |
| 7 | git init --bare "$LAB/remote/app.git" |
| 8 | # Alice clones |
| 9 | git clone "$LAB/remote/app.git" "$LAB/alice/app" |
| 10 | cd "$LAB/alice/app" |
| 11 | git config user.name Alice |
| 12 | git config user.email alice@example.com |
| 13 | echo "# Lab" > README.md |
| 14 | git add README.md && git commit -m "chore: init" |
| 15 | git push -u origin main |
| 16 | # Bob clones |
| 17 | git clone "$LAB/remote/app.git" "$LAB/bob/app" |
| 18 | cd "$LAB/bob/app" |
| 19 | git config user.name Bob |
| 20 | git config user.email bob@example.com |
| 21 | echo "Lab ready at $LAB" |
| 22 | echo "Practice diverging branches, merges, rebases, and reflog recovery." |
best practice
You (or your agent) have mastered Git when you can do all of the following without looking up flags for the happy path:
- Diagram the three trees and predict git status after add/commit/reset/restore
- Ship a feature via branch → commits → rebase onto main → PR → merge
- Undo a published bug with revert (including revert of a merge with -m)
- Recover a dangling commit from reflog within 30 minutes of realizing the loss
- Bisect a regression with a scripted test harness
- Configure signing, hooks, and ignore rules that block secrets
- Teach a teammate when to merge vs rebase vs cherry-pick vs revert
note
Community
Get help on Slack, Discord or VIP
Stuck on a guide? Join the community and ask.