Git Cheat Sheet

Updated · 8 min read
easyGit 2.53gitcheat sheetcommandsreference

A cheat sheet is a one-page summary to keep next to you while you work, like the list of phone numbers by a hotel telephone. This one lists every command taught in this course, grouped by the job it does, with the options you will use most and a link to the lesson that explains it. Use your browser's Print command to keep a paper copy.

The Daily Loop in Ten Commands

StepCommand
Get the latest workgit switch main and git pull
Start a branchgit switch -c feature/<name>
See what changedgit status and git diff
Stagegit add <file> or git add -p
Commitgit commit -m "<message>"
Share the branchgit push -u origin feature/<name>
Update the branchgit pull --rebase origin main
Fix the last commitgit commit --amend
Put work asidegit stash and git stash pop
Find a lost commitgit reflog

Setup and Help

CommandIt is used toCommon options
git config (lesson)read and write settings--global, --local, --system, --list, --show-origin, --unset, --edit
git help (lesson)open the manual for a command-a (all commands), -g (guides), <command>
git version / git --version (lesson)show the installed version

Creating and Getting Repositories

CommandIt is used toCommon options
git init (lesson)create a new repository-b <branch>, --bare, --object-format=sha256, --ref-format=reftable
git clone (lesson)copy a repository and check out a branch--depth, --branch, --filter=blob:none, --recurse-submodules, --bare, --mirror

Everyday Changes

CommandIt is used toCommon options
git status (lesson)show the state of the working tree and index-s, -b, --porcelain, --ignored
git add (lesson)stage changes., -A, -u, -p, -N, --dry-run
git commit (lesson)record staged changes as a commit-m, -a, -v, --amend, --no-edit, --fixup, -S, --allow-empty
git rm (lesson)remove files from the working tree and index--cached, -r, -f
git mv (lesson)move or rename a tracked file-f
git restore (lesson)restore files in the working tree or index--staged, --worktree, --source=<commit>, -p, --ours, --theirs
git clean (lesson)delete untracked files-n, -f, -d, -x, -i
git check-ignore (lesson)explain why a path is ignored-v

History and Comparison

CommandIt is used toCommon options
git log (lesson)list commits--oneline, --graph, --all, --stat, -p, --author, --since, --grep, -S, -G, -L, --follow, --pretty=format:
git show (lesson)show a commit, tag or file at a commit--stat, --name-only, <commit>:<path>
git diff (lesson)compare working tree, index and commits--staged, HEAD, a..b, a...b, --stat, --name-only, --word-diff, --color-words
git shortlog (lesson)summarize commits by author-s, -n, -e
git blame (lesson)show who last changed each line-L, -w, -C, --ignore-revs-file
git grep (lesson)search tracked files (in any commit)-n, -i, -w, -c, <commit>
git bisect (lesson)binary-search history for a bad commitstart, good, bad, skip, run, reset, log
git describe (lesson)name a commit after the nearest tag--tags, --always, --dirty
git range-diff (lesson)compare two versions of a series of commits
git cherry (lesson)find commits not yet applied upstream-v

Branches and Merging

CommandIt is used toCommon options
git branch (lesson)list, create, rename and delete branches-v, -vv, -a, -m, -d, -D, --merged, --no-merged, -u
git switch (lesson)switch branches-c, -C, --detach, - (previous branch)
git checkout (legacy for these jobs) (lesson)switch branches or restore files-b, --, --ours, --theirs
git merge (lesson)join histories--no-ff, --ff-only, --squash, --abort, --continue, -X ours/theirs, -s <strategy>
git mergetool (lesson)resolve conflicts in a merge tool--tool=<name>
git rebase (lesson)replay commits on a new base-i, --onto, --continue, --skip, --abort, --autosquash, --update-refs, -x, --autostash
git cherry-pick (lesson)apply existing commits on the current branch-x, -n, -m, --continue, --abort
git revert (lesson)create a commit that undoes another-m 1, --no-edit, -n
git reset (lesson)move the branch and optionally the index and working tree--soft, --mixed, --hard, --keep, <path>
git rerere (lesson)reuse recorded conflict resolutionsstatus, diff, forget
git merge-base (lesson)find the best common ancestor--is-ancestor, --fork-point
git merge-tree (lesson)merge without touching the working tree--write-tree

Remotes and Sharing

CommandIt is used toCommon options
git remote (lesson)manage remote connections-v, add, rename, remove, set-url, prune
git fetch (lesson)download objects and refs--all, --prune, --tags, --depth, --unshallow
git pull (lesson)fetch and integrate--rebase, --ff-only, --no-rebase
git push (lesson)upload commits and update remote refs-u, --force-with-lease, --force-if-includes, --tags, --delete, --dry-run
git ls-remote (lesson)list refs on a remote--heads, --tags

Saving Work and Releases

CommandIt is used toCommon options
git stash (lesson)save and restore work in progresspush -m, -u, -a, -p, list, show -p, apply, pop, drop, clear, branch
git tag (lesson)create, list and delete tags-a, -m, -s, -d, -l, -n
git archive (lesson)export a tree as zip or tar--format, --prefix, -o
git bundle (lesson)pack a repository into one filecreate, verify, list-heads
git format-patch (lesson)write commits as email-style patches-n, -o, --cover-letter
git am (lesson)apply email-style patches as commits--continue, --abort, -3
git apply (lesson)apply a diff to the working tree--check, --stat, -3
git notes (lesson)attach notes to commitsadd, show, list

Recovery and Maintenance

CommandIt is used toCommon options
git reflog (lesson)show where HEAD and branches have beenshow <ref>, expire
git fsck (lesson)check integrity; find unreachable objects--unreachable, --lost-found, --full
git gc (lesson)pack objects and clean up--aggressive, --prune=now
git maintenance (lesson)schedule background optimizationstart, stop, run --task=…
git prune (lesson)remove unreachable loose objects-n
git count-objects (lesson)show object counts and disk use-v, -H

Bigger Setups

CommandIt is used toCommon options
git worktree (lesson)manage several working foldersadd, list, remove, prune, lock
git submodule (lesson)manage nested repositoriesadd, update --init --recursive, status, foreach, sync
git subtree (lesson)merge another project into a subfolderadd, pull, push, split, --squash
git sparse-checkout (lesson)check out only part of a repositoryset, add, list, disable, --cone
git lfs (extension) (lesson)store large files outside the repositoryinstall, track, ls-files, migrate
scalar (lesson)set up very large repositories for speedclone, register, run

Signing and Verification

CommandIt is used toCommon options
git commit -S / git tag -s (lesson)sign a commit or tagconfig: gpg.format ssh, user.signingKey, commit.gpgSign
git verify-commit / git verify-tag (lesson)check a signature-v

Plumbing

CommandIt is used to
git hash-object (lesson)compute an object's hash (and store it with -w)
git cat-file (lesson)show an object's type (-t), size (-s) or content (-p)
git ls-files (lesson)list files in the index (--stage shows modes and hashes)
git ls-tree (lesson)list a tree object
git update-index (lesson)change the index directly (--add, --cacheinfo, --chmod=+x)
git write-tree (lesson)write the index as a tree object
git commit-tree (lesson)create a commit object from a tree
git update-ref (lesson)set a ref safely
git symbolic-ref (lesson)read or set a symbolic ref such as HEAD
git rev-parse (lesson)turn names into hashes (--show-toplevel, --abbrev-ref HEAD)
git rev-list (lesson)list commits reachable from refs
git show-ref / git for-each-ref (lesson)list refs (with a custom --format)
git name-rev (lesson)name a commit relative to refs
git verify-pack (lesson)inspect a packfile

Safety Net

If you need toRun
Cancel a merge, rebase or cherry-pick in progressgit merge --abort, git rebase --abort, git cherry-pick --abort
Keep a copy before a risky commandgit branch backup
Undo the last commit, keep the changesgit reset --soft HEAD~1
Undo a pushed commitgit revert <commit>
Force-push your own branch safelygit push --force-with-lease --force-if-includes
Get back anything you committedgit reflog, then git branch rescue <id>

For explanations and examples, see the lessons linked in each table, and for practice questions, 50 Git Interview Questions and Answers.

Was this tutorial helpful?