Cheat sheet: Git
Git history, undo, branches and remotes, every flag explained.
Each entry shows the command, a copy button, and what every part does: sub-command, flag, pipe, redirect and placeholder, in the order they appear. <placeholders> are the bits you replace.
Basics: set up, stage, commit, sync23
git config --global user.name "<name>"; git config --global user.email "<email>"Set your name and email (once per machine)What each part does
git configread/write Git settings--globalfor your user (~/.gitconfig), all reposuser.name / user.emailrecorded as the author of your commits
Tip Git 2.46+ also has: git config set --global user.name "<name>". Check with git config list --show-origin.
git init -b mainStart a new repoWhat each part does
initcreate a .git folder here-b main--initial-branch: name the first branch main
git clone <url>Copy a remote repoWhat each part does
clone <url>download the repo with full history into a new folder
git clone --depth 1 <url>Shallow clone (fast, latest commit only)What each part does
--depth 1only the most recent commit, no history
git statusWhat changed?What each part does
statusbranch, staged, not-staged and untracked files
git add <file>; git add -AStage one file / everythingWhat each part does
add <file>stage that file for the next commitadd -A--all: stage every change in the repo, including deletions (git add . = only under the current folder)
git add -pStage only parts of a fileWhat each part does
-p--patch: go through each change and choose y/n
git commit -m "<message>"Commit staged changesWhat each part does
commitrecord the staged changes-m "<message>"commit message (without -m an editor opens)
git commit -am "<message>"Stage tracked files and commit in one stepWhat each part does
-a--all: auto-stage modified and deleted tracked files (NOT new files)-mmessage
git commit --amend --no-editFix the last commit (not pushed yet)CautionWhat each part does
--amendreplace the last commit with one that includes what is staged now--no-editkeep the same message
git log --oneline -n 10Recent historyWhat each part does
--onelineone line per commit-n 10only the last 10
git diff; git diff --stagedSee the changesWhat each part does
git diffchanges not staged yet--stagedwhat is staged (alias --cached)
git show <commit>Show one commitWhat each part does
showmessage, author and diff of that commit (HEAD if omitted)
git branch -aList branchesWhat each part does
-a--all: local and remote-tracking branches; * marks the current one
git checkout -b <branch>Create and switch to a branchWhat each part does
checkout -bolder equivalent of git switch -c
git branch -d <branch>; git branch -D <branch>Delete a branchCautionWhat each part does
-ddelete if it is fully merged-Dforce delete even if not merged
git merge --no-ff <branch>Merge a branch into the current oneWhat each part does
merge <branch>bring <branch>'s commits into the current branch--no-ffalways create a merge commit, even when a fast-forward is possible
Tip Conflict? Fix the files, git add them, git commit, or give up with git merge --abort.
git remote -vWhich remotes are set up?What each part does
-vshow fetch and push URLs
git remote add origin <url>Add a remoteWhat each part does
remote add origin <url>name the remote "origin" (convention)
git pullGet the latest from the remoteWhat each part does
pullfetch + merge the upstream branch (git pull --rebase to rebase instead)
git pushUpload your commitsWhat each part does
pushsend commits to the upstream branch (first time: git push -u origin <branch>)
git rm --cached <file>Stop tracking a file but keep it on diskWhat each part does
rmremove from Git--cachedonly from the index; the file stays (then add it to .gitignore)
git mv <old> <new>Rename a tracked fileWhat each part does
git mvrename and stage the rename in one step
History & search7
git log --oneline --graph --decorate --allCompact graph of all branchesWhat each part does
git logshow commit history--onelineone line per commit: short hash + subject--graphASCII graph of branches and merges--decorateshow branch and tag names next to commits--allinclude every branch and tag, not just the current one
git log --oneline | grep -i <text>Find commits whose message mentions somethingWhat each part does
git log --onelineone commit per line| grep -i <text>keep lines containing <text>, any case
Tip Built-in: git log --oneline --grep=<text> -i
git log --oneline --author=<name> --since=<date> --until=<date>Commits by someone in a date rangeWhat each part does
--author=<name>author matches (regex)--since / --untildate range, e.g. "2 weeks ago", 2026-11-01
git log -S"<text>" -pWhen was this string added or removed?What each part does
-S"<text>""pickaxe": commits that changed how many times <text> appears-pshow the diff of each matching commit
git log --follow -p -- <file>History of one file, across renamesWhat each part does
--followkeep following the file when it was renamed-pwith diffs-- <file>"--" ends options; what follows is a path
git blame -L <start>,<end> -- <file>Who changed these linesWhat each part does
blamelast commit + author for every line-L <start>,<end>only that line range
git status -sb; git diff; git diff --stagedWhat is staged vs notWhat each part does
status -sbshort status with branch infogit diffunstaged changesgit diff --stagedwhat will go into the next commit
Undo7
git restore --staged <file>Unstage a file (keep the changes)What each part does
restorerestore files--stagedonly the index (staging area): the change goes back to "not staged"
git restore <file>Discard local changes to a fileDestructiveWhat each part does
restore <file>replace the working copy with the last committed/staged version
git reset --soft HEAD~1Undo the last commit, keep the changes stagedCautionWhat each part does
resetmove the current branch pointer--softonly move HEAD; index and files are untouchedHEAD~1one commit before HEAD
git fetch origin && git reset --hard origin/<branch>Throw away local commits and changes to match the remoteDestructiveWhat each part does
git fetch origindownload the latest remote state&&only if that workedreset --hardmove the branch AND overwrite index + working treeorigin/<branch>the remote version
git revert <commit>Undo a pushed commit safelyWhat each part does
revert <commit>create a new commit that undoes <commit>; history is not rewritten
git reflog -n 20Find a "lost" commitWhat each part does
reflogevery place HEAD has been (commits, resets, rebases)-n 20last 20 entries; then git branch rescue <sha>
git stash push -u -m "<message>"; git stash popPark work in progressWhat each part does
stash pushsave changes and clean the working tree-uinclude untracked files-m "<message>"label itstash popapply the latest stash and drop it
Branches & remotes10
git switch -c <branch>Create and switch to a branchWhat each part does
switchchange branches-c <branch>create it first
git branch --merged main | grep -vE '^\*|main' | xargs -r git branch -dDelete local branches already merged into mainCautionWhat each part does
git branch --merged mainbranches fully merged into maingrep -vE '^\*|main'-v: drop lines matching; -E: extended regex: the current branch (starts with *) and main itselfxargs -r git branch -ddelete each remaining branch; -d refuses unmerged ones
git fetch --pruneRemove stale remote-tracking branchesWhat each part does
fetchdownload from the remote--prunedelete local refs to branches that no longer exist on the remote
git pull --rebase origin mainRebase your branch on the latest mainWhat each part does
pullfetch + integrate--rebasereplay your commits on top instead of creating a merge commitorigin mainremote and branch
git push -u origin <branch>Push a new branch and track itWhat each part does
-u--set-upstream: later git push / git pull need no arguments
git push --force-with-lease origin <branch>Force-push safely after a rebaseCautionWhat each part does
--force-with-leaseoverwrite the remote branch only if nobody pushed since your last fetch
git push origin --delete <branch>Delete a remote branchDestructiveWhat each part does
--deleteremove that branch on the remote
git cherry-pick <commit>Copy one commit onto the current branchWhat each part does
cherry-pick <commit>apply the changes of <commit> as a new commit here
git tag -a <version> -m "<message>" && git push origin <version>Tag a release and push itWhat each part does
tag -a <version>annotated tag (has author, date, message)-m "<message>"tag messagegit push origin <version>tags are not pushed with branches; push it explicitly
git clean -nd; git clean -fdPreview / remove untracked filesDestructiveWhat each part does
cleanremove untracked files-ndry run: list what would be removed-dinclude untracked directories-factually delete
Rebase, bisect & advanced history12
git rebase -i HEAD~<n>Clean up your last commits before a PRCautionWhat each part does
rebase -iinteractive: reorder, squash (s), fixup (f), reword (r) or drop commitsHEAD~<n>the last n commits
Tip Rewrites history: only for commits not yet shared, or push with --force-with-lease.
git fetch origin && git rebase origin/mainRebase your branch on the latest mainCautionWhat each part does
git fetch originget the newest maingit rebase origin/mainreplay your commits on top of it
git rebase --continue; git rebase --abort; git merge --abortContinue, skip or abort a rebase / mergeWhat each part does
--continueafter fixing conflicts and git addrebase --abortgo back to before the rebasemerge --abortcancel a conflicted merge
git commit --fixup <commit> && git rebase -i --autosquash <commit>~1Squash a fix into an earlier commit automaticallyCautionWhat each part does
commit --fixup <commit>commit marked fixup! for that commitrebase -i --autosquashmoves and squashes it into place
git bisect start <bad> <good>; git bisect good; git bisect bad; git bisect resetFind the commit that introduced a bugWhat each part does
bisect start <bad> <good>binary search between a bad commit (e.g. HEAD) and a known good onebisect good / badmark each checked-out commit after testingbisect resetfinish and return to your branch
Tip git bisect run <test-script> automates it.
git log --oneline main..<branch>Commits on a branch that aren't on mainWhat each part does
main..<branch>reachable from branch but not from main
git diff main...<branch> --statCompare two branchesWhat each part does
main...<branch>changes on branch since it split from main (three dots)--statfiles and line counts only
git shortlog -sn --no-mergesWho committed how muchWhat each part does
shortlog -s -ncommit count per author, sorted--no-mergesskip merge commits
git grep -n '<text>' <commit-or-branch>Search the code in any commitWhat each part does
git grep -nfast search of tracked files with line numbers<commit-or-branch>search that version instead of the working tree
git restore --source=<commit> -- <file>Restore a file from another commit or branchCautionWhat each part does
--source=<commit>take the file's content from that commit-- <file>only that path
git describe --tags --alwaysDescribe the current version from tagsWhat each part does
describe --tagsnearest tag + commits since + short SHA, e.g. v1.4.0-3-g4f2a9c1--alwaysfall back to the SHA if no tag
git filter-repo --invert-paths --path <file>Remove a file from all history (leaked secret)DestructiveWhat each part does
git filter-reporewrite the whole history (separate tool, replaces filter-branch)--invert-paths --path <file>drop that file everywhere
Tip Rotate the secret first; everyone must re-clone after the force push.
Stashes, tags, remotes, worktrees & submodules12
git stash list; git stash show -p stash@{1}; git stash apply stash@{1}; git stash drop stash@{1}List, show, apply and drop stashesWhat each part does
stash listall stashesshow -pthe diff of oneapplyre-apply but keep it (pop removes it)dropdelete it
git tag -l 'v1.*'; git tag -d <tag> && git push origin --delete <tag>List and delete tagsCautionWhat each part does
tag -l 'v1.*'list matching tagstag -ddelete locallypush origin --delete <tag>delete on the remote
git push --tagsPush all tagsWhat each part does
--tagspush every local tag (git push --follow-tags pushes only annotated ones for pushed commits)
git remote set-url origin <url>Change a remote's URL (HTTPS to SSH, moved repo)What each part does
remote set-url originpoint origin at a new URL
git worktree add ../<dir> <branch>; git worktree list; git worktree remove ../<dir>Work on two branches at onceWhat each part does
worktree addcheck out another branch in a second folder, sharing the same repolist / removesee and clean up worktrees
git clone --recurse-submodules <url>; git submodule update --init --recursiveClone with submodules / update themWhat each part does
--recurse-submodulesalso clone nested repossubmodule update --init --recursivefetch submodules in an existing clone
git ls-files | grep <pattern>Which files does git track?What each part does
ls-filestracked files| grepfilter
git check-ignore -v <file>Why is this file ignored?What each part does
check-ignore -vthe .gitignore file and line that ignores it
git config --list --show-originSee all config and where it comes fromWhat each part does
--listevery setting--show-originwhich file set it (system, global, repo)
git config --global alias.lg "log --oneline --graph --decorate --all"Create a git aliasWhat each part does
alias.lgnew command git lg"log ..."what it expands to
git archive --format=zip -o <file>.zip <commit-or-tag>Make a zip of a commit without .gitWhat each part does
git archiveexport the tracked files of that version--format=zip -ooutput format and file
git switch -Switch back to the previous branchWhat each part does
-the branch you were on before (like cd -)
Nothing matches.
My DevOps Playbook by Angelo John Calleja