Cheatsheets / Git

Git Cheatsheet

Complete Git reference. Hit Ctrl+P to print.

Setup & Config

git initInitialize a new repository in current directory
git init my-projectCreate new directory and initialize repo
git clone urlClone remote repository
git clone url dirClone into a specific directory
git clone --depth 1 urlShallow clone - only latest commit
git clone --branch main urlClone specific branch
git config --global user.name "Alice"Set global username
git config --global user.email "a@b.com"Set global email
git config --global core.editor "vim"Set default editor
git config --global init.defaultBranch mainSet default branch name
git config --listShow all configuration values
git config user.nameShow a specific config value
.gitignoreFile listing patterns to ignore - one per line
git check-ignore -v fileCheck why a file is ignored

Stage & Commit

git statusShow working tree and staging area status
git status -sShort status - compact format
git add file.txtStage a file
git add src/Stage all changes in directory
git add -pInteractively stage hunks
git add -uStage all tracked modifications and deletions
git restore --staged fileUnstage file (keep changes in working tree)
git restore fileDiscard working tree changes (irreversible)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git commit --amendModify the last commit message or add staged changes
git commit --amend --no-editAmend last commit keeping same message
git diffDiff working tree vs staging area
git diff --stagedDiff staging area vs last commit
git diff HEADDiff working tree vs last commit
git diff branch1..branch2Diff between two branches
git diff --statShow changed file names and line counts only
git showShow last commit details and diff
git show abc1234Show specific commit

Branching

git branchList local branches
git branch -aList all branches including remote-tracking
git branch -vShow last commit on each branch
git branch featureCreate branch
git switch featureSwitch to branch (modern)
git switch -c featureCreate and switch to new branch
git switch -Switch to previous branch
git checkout featureSwitch to branch (legacy)
git checkout -b featureCreate and switch to branch (legacy)
git branch -d featureDelete merged branch
git branch -D featureForce delete branch
git branch -m old newRename branch
git branch -m newRename current branch
git switch -c feature origin/featureCreate local branch tracking remote
git merge featureMerge branch into current branch
git merge --no-ff featureMerge with a merge commit (no fast-forward)
git merge --squash featureSquash all commits into one staged change
git merge --abortAbort in-progress merge
git cherry-pick abc1234Apply a specific commit to current branch
git cherry-pick a..bApply range of commits

Remote

git remote -vList remotes with URLs
git remote add origin urlAdd a remote
git remote remove originRemove a remote
git remote rename origin upstreamRename a remote
git remote set-url origin urlChange remote URL
git fetchDownload all remote changes - does not merge
git fetch originFetch from specific remote
git fetch --pruneFetch and remove deleted remote-tracking branches
git pullFetch and merge current branch
git pull --rebaseFetch and rebase instead of merge
git pushPush current branch to tracked remote
git push -u origin featurePush and set upstream tracking
git push origin --delete featureDelete remote branch
git push --force-with-leaseForce push - fails if remote has new commits
git push --tagsPush all tags

History & Inspection

git logShow commit history
git log --onelineOne line per commit
git log --oneline --graph --allVisualize all branches as a graph
git log -n 10Show last 10 commits
git log --author="Alice"Filter by author
git log --since="2 weeks ago"Filter by date
git log -- path/to/fileHistory for a specific file
git log --grep="bug"Filter commits by message content
git log -S "searchString"Find commits that added/removed a string
git log --format="%h %an %s" --no-mergesCustom format: short hash, author, subject
git log --diff-filter=D -- file.txtFind commit that deleted a file
git blame file.txtShow who changed each line and when
git blame -L 10,20 file.txtBlame specific line range
git shortlog -snCommit count per author
git reflogHistory of HEAD movements - useful for recovery
git reflog expire --expire=now --all && git gc --prune=nowExpire reflog and run garbage collection
git bisect startStart binary search for a regression
git bisect bad / git bisect good abcMark commit as bad/good during bisect
git bisect run npm testAutomate bisect with a test script

Undo & Reset

git revert HEADCreate new commit that undoes last commit
git revert abc1234Revert a specific commit
git revert abc1234..HEADRevert a range of commits
git reset --soft HEAD~1Undo last commit - keep changes staged
git reset --mixed HEAD~1Undo last commit - keep changes unstaged (default)
git reset --hard HEAD~1Undo last commit - discard all changes (destructive)
git reset --hard origin/mainReset local branch to match remote
git restore file.txtDiscard working tree changes to a file
git restore --source=HEAD~1 file.txtRestore file from a specific commit
git clean -fdRemove untracked files and directories
git clean -nDry run - show what clean would remove
git rebase -i HEAD~3Interactive rebase - reword, squash, or drop last 3 commits
git rebase mainReplay current branch commits on top of main
git rebase --abortAbort in-progress rebase
git rebase --continueContinue after resolving conflict
git commit --fixup=HEAD~1Create a fixup commit targeting a parent
git rebase -i --autosquash HEAD~5Auto-apply fixup!/squash! commits during rebase

Stash

git stashStash working tree and staging area changes
git stash push -m "WIP: auth"Stash with a description
git stash -uAlso stash untracked files
git stash listList all stashes
git stash popApply latest stash and remove it from stack
git stash pop stash@{2}Apply specific stash and remove it
git stash apply stash@{2}Apply stash without removing it
git stash drop stash@{0}Delete a specific stash
git stash clearDelete all stashes
git stash show -pShow diff of latest stash
git stash branch feature stash@{0}Create branch from stash

Tags

git tagList all tags
git tag v1.0.0Create lightweight tag on current commit
git tag -a v1.0.0 -m "Release 1.0"Create annotated tag with message
git tag -a v1.0.0 abc1234Tag a specific past commit
git tag -d v1.0.0Delete local tag
git push origin v1.0.0Push a tag to remote
git push origin --tagsPush all tags
git push origin --delete v1.0.0Delete remote tag
git describe --tagsDescribe commit using nearest tag
git switch --detach v1.0.0Check out a tag (detached HEAD)

Worktrees

git worktree add ../hotfix hotfix-branchCheck out a branch into a separate directory
git worktree add -b new-branch ../new-dirCreate new branch and check it out in a new directory
git worktree add --detach ../review abc1234Check out a specific commit in a new directory
git worktree listList all worktrees with path and HEAD
git worktree remove ../hotfixRemove a worktree directory and its metadata
git worktree pruneClean up stale worktree metadata
git worktree lock ../hotfixPrevent a worktree from being pruned
git worktree unlock ../hotfixRemove lock from a worktree

Submodules

git submodule add https://github.com/user/lib libs/libAdd a submodule at a path
git submodule initInitialize submodule config after cloning
git submodule updateCheck out the recorded commit for each submodule
git submodule update --init --recursiveInit and update all submodules recursively
git clone --recurse-submodules urlClone and initialize all submodules in one step
git submodule update --remoteFetch and update submodules to latest upstream commit
git submodule foreach git pull origin mainRun a command in every submodule
git submodule statusShow current commit hash for each submodule
git submodule deinit libs/libUnregister a submodule
git rm libs/libRemove submodule entry from .gitmodules and index

Debugging

git bisect startStart bisect session to find a regression
git bisect badMark current commit as bad
git bisect good v1.0.0Mark known-good commit
git bisect run ./test.shAuto-bisect with script (exit 0=good, 1=bad)
git bisect resetEnd bisect session, return to HEAD
git reflog --date=isoReflog with ISO timestamps
git reflog show branchReflog for a specific branch
git fsck --lost-foundFind dangling/unreachable objects
git log --follow path/to/fileFollow file history across renames
git diff branch1...branch2Changes since two branches diverged
git show HEAD:file.txtShow file contents at a specific commit

Conventional Commits

feat: add login endpointNew feature (triggers MINOR bump in semver)
fix: correct null pointer errorBug fix (triggers PATCH bump)
docs: update READMEDocumentation only changes
style: format with prettierFormatting, no logic change
refactor: extract auth serviceRefactor without feature/fix
test: add user unit testsAdding or updating tests
chore: update dependenciesMaintenance tasks
perf: cache database queriesPerformance improvement
ci: add GitHub Actions workflowCI/CD changes
build: upgrade webpack to v5Build system changes
BREAKING CHANGE: footerBreaking API change (triggers MAJOR bump)
feat!: new APIShorthand for breaking change
<type>(<scope>): <description>Optional scope: feat(auth): add OAuth