Cheatsheets

Quick reference for commands I use daily. No fluff.

Official docs

Setup and Config

git config --global user.name "Name"
Set your name for all repos
git config --global user.email "email@example.com"
Set your email for all repos
git config --global core.editor vim
Set default editor
git config --global init.defaultBranch main
Set default branch name to main
git config --global pull.rebase true
Rebase by default on pull
git config --global alias.co checkout
Create a shortcut alias
git config --list --show-origin
Show all config values and where they come from
git config --global credential.helper cache
Cache credentials in memory temporarily
git config --global core.autocrlf input
Fix line endings on commit (Mac/Linux)
git config --global diff.tool vscode && git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
Set VS Code as default diff tool

Basics

git init
Initialize a new repo in current directory
git clone <url>
Clone a remote repo locally
git clone --depth 1 <url>
Shallow clone, only latest commit
git add <file>
Stage a specific file
git add -p
Interactively stage chunks of changes
git commit -m "message"
Commit staged changes with a message
git commit --amend
Edit the last commit message or add staged changes to it
git status
Show working tree status
git status -s
Short format status output
git diff
Show unstaged changes
git diff --staged
Show staged changes
git diff <branch1>..<branch2>
Compare two branches

Branching

git branch
List local branches
git branch -a
List all branches including remote
git branch <name>
Create a new branch
git branch -d <name>
Delete a merged branch
git branch -D <name>
Force delete a branch regardless of merge status
git branch -m <old> <new>
Rename a branch
git switch <branch>
Switch to a branch
git switch -c <branch>
Create and switch to a new branch
git checkout -b <branch>
Create and switch to a new branch (old syntax)
git branch --merged
List branches already merged into current
git branch -vv
Show branches with tracking info and last commit

Merging and Rebasing

git merge <branch>
Merge branch into current branch
git merge --no-ff <branch>
Merge with a merge commit even if fast-forward is possible
git merge --squash <branch>
Squash all commits from branch into one staged change
git merge --abort
Abort a merge in progress
git rebase <branch>
Rebase current branch onto another
git rebase -i HEAD~3
Interactive rebase last 3 commits
git rebase --onto main feature bugfix
Rebase bugfix onto main, detaching from feature
git rebase --abort
Abort a rebase in progress
git rebase --continue
Continue rebase after resolving conflicts
git config --global rerere.enabled true
Enable automatic reuse of recorded conflict resolutions

Stashing

git stash
Stash uncommitted changes
git stash -u
Stash including untracked files
git stash push -m "description"
Stash with a descriptive message
git stash list
List all stashes
git stash pop
Apply latest stash and remove it from list
git stash apply stash@{2}
Apply a specific stash without removing it
git stash drop stash@{0}
Delete a specific stash
git stash clear
Delete all stashes
git stash show -p
Show the diff of latest stash
git stash branch <name>
Create a branch from a stash

Remote

git remote -v
List remotes with URLs
git remote add origin <url>
Add a remote
git remote remove <name>
Remove a remote
git remote rename origin upstream
Rename a remote
git fetch
Download objects and refs from remote
git fetch --prune
Fetch and remove deleted remote branches locally
git pull
Fetch and merge remote changes
git pull --rebase
Fetch and rebase instead of merge
git push
Push commits to remote
git push -u origin <branch>
Push and set upstream tracking
git push origin --delete <branch>
Delete a remote branch
git push --force-with-lease
Force push but fail if remote has new commits

Undoing Changes

git restore <file>
Discard unstaged changes in a file
git restore --staged <file>
Unstage a file without losing changes
git reset HEAD~1
Undo last commit, keep changes in working directory (unstaged)
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --hard HEAD~1
Undo last commit and discard all changes (DESTRUCTIVE, no undo)
git reset --hard origin/main
Reset branch to match remote exactly (DESTRUCTIVE, loses local commits)
git revert <commit>
Create a new commit that undoes a specific commit
git revert HEAD --no-edit
Revert last commit without editing message
git clean -fd
Remove untracked files and directories (DESTRUCTIVE, no undo)
git clean -fdn
Dry run, show what would be removed
git checkout -- .
Discard all unstaged changes (old syntax)

Logging and History

git log --oneline
Compact one-line log
git log --oneline --graph --all
Visual branch graph of all branches
git log -n 5
Show last 5 commits
git log --author="Name"
Filter commits by author
git log --since="2 weeks ago"
Show commits from last 2 weeks
git log -p <file>
Show commit history with diffs for a file
git log --grep="fix"
Search commit messages
git reflog
Show history of HEAD changes, useful for recovery
git blame <file>
Show who changed each line and when
git show <commit>
Show details and diff of a specific commit
git shortlog -sn
Commit count per author, sorted

Tags

git tag
List all tags
git tag v1.0.0
Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0"
Create an annotated tag with message
git tag -a v1.0.0 <commit>
Tag a specific commit
git tag -d v1.0.0
Delete a local tag
git push origin v1.0.0
Push a specific tag to remote
git push origin --tags
Push all tags to remote
git push origin --delete v1.0.0
Delete a remote tag
git describe --tags
Show most recent tag reachable from current commit
git tag -l "v1.*"
List tags matching a pattern

Advanced

git cherry-pick <commit>
Apply a commit from another branch
git cherry-pick --no-commit <commit>
Apply changes without committing
git bisect start
Start binary search for a buggy commit
git bisect good <commit>
Mark a commit as good during bisect
git bisect bad <commit>
Mark a commit as bad during bisect
git bisect reset
End bisect session
git worktree add ../hotfix hotfix-branch
Check out a branch in a separate directory
git worktree list
List all worktrees
git submodule add <url> <path>
Add a submodule
git submodule update --init --recursive
Initialize and update all submodules
git archive --format=zip HEAD > archive.zip
Export repo as a zip file
git rev-parse --short HEAD
Get current short commit hash (useful in CI/scripts)
git clean -fdx
Clean untracked AND ignored files (DESTRUCTIVE, full reset for CI)
git commit --fixup <commit>
Create a fixup commit for later autosquash rebase
git rebase -i --autosquash HEAD~5
Interactive rebase that auto-orders fixup commits
git log --all --full-history -- <file>
Track a file through renames and deletions

Common Pipelines

git branch --merged | grep -v '\*\|main\|master' | xargs git branch -d
Delete all local branches already merged into current
git log --all --oneline --graph --decorate
Pretty visual history of all branches
git diff --name-only HEAD~5..HEAD
List files changed in last 5 commits
git log --format='%an' | sort | uniq -c | sort -rn
Rank contributors by commit count
git log --diff-filter=D --summary | grep 'delete mode'
Find all deleted files in history
git stash list | while read s; do echo "$s"; git stash show "$(echo $s | cut -d: -f1)"; echo; done
Show contents of all stashes
git log --oneline --since='last monday' --author="$(git config user.name)"
Your commits since last Monday (standup helper)
git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'
List branches sorted by last commit date
git log -S 'functionName' --oneline
Find commits that added or removed a string (pickaxe)
git diff HEAD~1 --stat
Show file change stats for last commit
git remote prune origin && git branch -vv | grep 'gone]' | awk '{print $1}' | xargs git branch -d
Clean up local branches whose remote was deleted
git log --pretty=format:'%h %ad %s' --date=short --all -- <file>
Full history of a specific file with short dates

Share this site

QR Code for cardasac.com

cardasac.com

Scan with your phone camera