A comprehensive reference organized by workflow. Covers syntax, common flags, and one-line descriptions.
Setup & Configuration
| Command | Description |
|---|---|
git init | Initialize a new Git repository in the current directory |
git init --bare | Initialize a bare repository (no working directory) |
git clone <url> | Clone a remote repository |
git clone --depth <n> <url> | Shallow clone with only the last N commits |
git clone --filter=blob:none <url> | Partial clone — download blobs on demand |
git clone --recurse-submodules <url> | Clone including all submodules |
git config --global user.name "<name>" | Set your name for all repositories |
git config --global user.email "<email>" | Set your email for all repositories |
git config --global core.editor "<editor>" | Set your default editor (vim, nano, code --wait) |
git config --global init.defaultBranch main | Set default branch name for new repos |
git config --list --show-origin | Show all config values and where they're set |
git config --global alias.<name> "<cmd>" | Create a Git alias |
Basic Workflow
| Command | Description |
|---|---|
git status | Show working tree status (modified, staged, untracked) |
git add <file> | Stage a specific file |
git add . | Stage all changes in current directory |
git add -p | Stage changes interactively, hunk by hunk |
git add -p <file> | Interactive staging for a specific file |
git commit -m "<msg>" | Commit staged changes with a message |
git commit -am "<msg>" | Stage tracked files and commit in one step |
git commit --amend | Modify the most recent commit (message and/or content) |
git commit --amend --no-edit | Amend last commit without changing the message |
git commit --allow-empty -m "<msg>" | Create a commit with no file changes |
Viewing Changes
| Command | Description |
|---|---|
git diff | Show unstaged changes (working dir vs index) |
git diff --staged | Show staged changes (index vs last commit) |
git diff HEAD | Show all changes (working dir vs last commit) |
git diff <a>..<b> | Show diff between two commits/branches |
git diff <a>...<b> | Show changes on b since it diverged from a |
git diff --stat | Show a summary of changed files and line counts |
git diff --name-only | Show only the names of changed files |
git diff --name-status | Show names and change type (A/M/D/R) |
git diff --word-diff | Show word-level differences (useful for prose) |
git diff --check | Warn about whitespace errors |
git diff --diff-filter=M | Show only modified files (A=added, D=deleted, R=renamed) |
Branching
| Command | Description |
|---|---|
git branch | List local branches |
git branch -a | List all branches (local and remote) |
git branch -v | List branches with last commit info |
git branch -vv | List branches with tracking and ahead/behind info |
git branch <name> | Create a new branch (don't switch to it) |
git branch -d <name> | Delete a merged branch |
git branch -D <name> | Force-delete an unmerged branch |
git branch -m <old> <new> | Rename a branch |
git branch --merged | List branches merged into current branch |
git branch --no-merged | List branches not yet merged |
git switch <branch> | Switch to a branch (safe — won't detach HEAD) |
git switch -c <branch> | Create and switch to a new branch |
git switch --detach <ref> | Switch to a commit in detached HEAD (explicit) |
git checkout <branch> | Switch to a branch (legacy — can detach HEAD) |
git checkout -b <branch> | Create and switch to a new branch (legacy) |
Merging
| Command | Description |
|---|---|
git merge <branch> | Merge a branch into the current branch |
git merge --no-ff <branch> | Merge with a merge commit (no fast-forward) |
git merge --squash <branch> | Squash all commits into one staged change |
git merge --abort | Abort a merge in progress (return to pre-merge state) |
git merge --continue | Continue a merge after resolving conflicts |
git mergetool | Launch a visual merge conflict tool |
Rebasing
| Command | Description |
|---|---|
git rebase <branch> | Rebase current branch onto another branch |
git rebase -i <ref> | Interactive rebase — reorder, squash, edit commits |
git rebase -i HEAD~<n> | Interactive rebase for the last N commits |
git rebase --onto <new> <old> <branch> | Transplant commits from one base to another |
git rebase --abort | Abort a rebase in progress |
git rebase --continue | Continue after resolving rebase conflicts |
git rebase --skip | Skip the current conflicting commit |
git rebase --autosquash | Auto-apply fixup!/squash! commit prefixes |
git rebase --autostash | Stash changes before rebase, apply after |
Interactive rebase commands: pick, reword, edit, squash, fixup, drop, exec
Undoing Changes
| Command | Description |
|---|---|
git restore <file> | Discard working directory changes in a file |
git restore --staged <file> | Unstage a file (keep working dir changes) |
git restore --source=<ref> <file> | Restore a file from a specific commit |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged (default) |
git reset --hard HEAD~1 | Undo last commit, discard all changes |
git reset --hard ORIG_HEAD | Undo the last merge/rebase/reset operation |
git revert <commit> | Create a new commit that undoes a commit |
git revert -m 1 <merge> | Revert a merge commit (keep parent 1) |
git revert --no-commit <range> | Revert multiple commits without auto-committing |
git clean -n | Dry run — show untracked files that would be removed |
git clean -f | Remove untracked files |
git clean -fd | Remove untracked files and directories |
git clean -fdx | Remove untracked, directories, and ignored files |
git clean -fdX | Remove only ignored files and directories |
Stashing
| Command | Description |
|---|---|
git stash | Stash tracked modified and staged files |
git stash -u | Stash including untracked files |
git stash -m "<msg>" | Stash with a descriptive message |
git stash list | List all stashes |
git stash show | Show the latest stash diff summary |
git stash show -p | Show the latest stash full diff |
git stash pop | Apply latest stash and remove it from the list |
git stash apply | Apply latest stash (keep it in the list) |
git stash apply stash@{n} | Apply a specific stash |
git stash drop stash@{n} | Delete a specific stash |
git stash clear | Delete all stashes |
git stash branch <name> | Create a branch from a stash |
Remotes
| Command | Description |
|---|---|
git remote -v | List remotes with fetch/push URLs |
git remote add <name> <url> | Add a new remote |
git remote remove <name> | Remove a remote |
git remote rename <old> <new> | Rename a remote |
git remote set-url <name> <url> | Change a remote's URL |
git remote show <name> | Show detailed info about a remote |
git fetch <remote> | Download objects and refs from a remote |
git fetch --all | Fetch from all remotes |
git fetch --prune | Fetch and remove stale remote-tracking refs |
git pull | Fetch and merge from the tracking remote |
git pull --rebase | Fetch and rebase instead of merge |
git push <remote> <branch> | Push a branch to a remote |
git push -u <remote> <branch> | Push and set upstream tracking |
git push --force-with-lease | Safe force push (rejects if remote changed) |
git push --follow-tags | Push commits and annotated tags |
git push origin --delete <branch> | Delete a remote branch |
git branch --set-upstream-to=<remote>/<branch> | Set tracking for current branch |
History & Log
| Command | Description |
|---|---|
git log | Show commit history |
git log --oneline | Compact one-line-per-commit log |
git log --oneline --graph --all | Visual graph of all branches |
git log --oneline --graph --decorate | Graph with branch/tag labels |
git log -n <N> | Show only the last N commits |
git log --stat | Show changed files per commit |
git log -p | Show full diffs per commit |
git log --author="<name>" | Filter by author |
git log --since="2 weeks ago" | Filter by date (also: --after, --until, --before) |
git log --grep="<pattern>" | Filter by commit message |
git log -S "<string>" | Pickaxe — find commits that add/remove a string |
git log -G "<regex>" | Find commits where a regex match changed |
git log -- <path> | Show commits that touched a specific file/directory |
git log --follow -- <file> | Follow file history through renames |
git log -L <start>,<end>:<file> | Show history of a line range in a file |
git log --merges | Show only merge commits |
git log --no-merges | Exclude merge commits |
git log --first-parent | Follow only the first parent (main line) |
git log --left-right <a>...<b> | Show which commits are on which side |
git log --pretty=format:"<fmt>" | Custom format (see format placeholders below) |
git shortlog -sn | Contributor commit counts, sorted |
git shortlog -sn --no-merges | Commit counts excluding merges |
Format Placeholders
| Placeholder | Meaning |
|---|---|
%H / %h | Full / abbreviated commit hash |
%an / %ae | Author name / email |
%cn / %ce | Committer name / email |
%s | Subject (first line of commit message) |
%b | Body of commit message |
%ar / %cr | Author / committer relative date |
%ad / %cd | Author / committer date |
%d / %D | Ref names (decorated) / without wrapping |
%Cred %Cgreen %Cblue %Creset | Color formatting |
Investigation & Debugging
| Command | Description |
|---|---|
git blame <file> | Show who last modified each line |
git blame -w <file> | Blame ignoring whitespace changes |
git blame -C <file> | Detect lines moved/copied from other files |
git blame --ignore-revs-file .git-blame-ignore-revs | Skip formatting commits in blame |
git bisect start | Start a binary search for a bug |
git bisect bad | Mark current commit as containing the bug |
git bisect good <ref> | Mark a known-good commit |
git bisect run <script> | Automate bisect with a test script |
git bisect reset | End bisect and return to original branch |
git grep "<pattern>" | Search file contents in current commit |
git grep -n "<pattern>" | Search with line numbers |
git grep -p "<pattern>" | Search with function/scope context |
git grep -c "<pattern>" | Count matches per file |
git grep "<pattern>" <ref> | Search in a specific commit |
git show <ref> | Show a commit's details and diff |
git show <ref>:<file> | Show a file at a specific commit |
git show --stat <ref> | Show commit with file change summary |
Cherry-Pick
| Command | Description |
|---|---|
git cherry-pick <commit> | Apply a specific commit to the current branch |
git cherry-pick -n <commit> | Cherry-pick without committing (stage only) |
git cherry-pick -x <commit> | Cherry-pick and add "(cherry picked from ...)" to message |
git cherry-pick <a>..<b> | Cherry-pick a range of commits (excludes a) |
git cherry-pick --abort | Abort a cherry-pick in progress |
git cherry-pick --continue | Continue after resolving conflicts |
Tags
| Command | Description |
|---|---|
git tag | List all tags |
git tag -l "v1.*" | List tags matching a pattern |
git tag <name> | Create a lightweight tag |
git tag -a <name> -m "<msg>" | Create an annotated tag |
git tag -a <name> <commit> | Tag a specific commit |
git tag -d <name> | Delete a local tag |
git push origin <tag> | Push a specific tag to remote |
git push --follow-tags | Push commits and annotated tags |
git push --tags | Push all tags to remote |
git push origin --delete <tag> | Delete a remote tag |
Reflog & Recovery
| Command | Description |
|---|---|
git reflog | Show history of HEAD movements |
git reflog show <branch> | Show reflog for a specific branch |
git reflog --date=iso | Show reflog with timestamps |
git fsck --unreachable | Find unreachable objects |
git fsck --lost-found | Save dangling commits to .git/lost-found/ |
Worktrees
| Command | Description |
|---|---|
git worktree add <path> <branch> | Create a linked worktree for a branch |
git worktree add <path> -b <new> | Create a worktree with a new branch |
git worktree list | List all worktrees |
git worktree remove <path> | Remove a linked worktree |
git worktree prune | Clean up stale worktree references |
Submodules
| Command | Description |
|---|---|
git submodule add <url> <path> | Add a submodule |
git submodule update --init --recursive | Initialize and fetch all submodules |
git submodule update --remote | Update submodules to latest remote commit |
git submodule sync | Sync submodule URLs after .gitmodules change |
git submodule deinit <path> | Unregister a submodule |
git submodule foreach '<cmd>' | Run a command in each submodule |
Subtrees
| Command | Description |
|---|---|
git subtree add --prefix=<dir> <url> <branch> --squash | Add a subtree |
git subtree pull --prefix=<dir> <url> <branch> --squash | Pull updates into a subtree |
git subtree push --prefix=<dir> <url> <branch> | Push subtree changes back upstream |
Git LFS
| Command | Description |
|---|---|
git lfs install | Initialize Git LFS for the current user |
git lfs track "<pattern>" | Track files matching a pattern with LFS |
git lfs untrack "<pattern>" | Stop tracking a pattern with LFS |
git lfs ls-files | List files tracked by LFS |
git lfs status | Show LFS file status |
git lfs pull | Download LFS file content |
git lfs fetch --recent | Pre-fetch LFS files for recent branches |
git lfs migrate import --include="<pattern>" | Retroactively move files to LFS |
Sparse Checkout & Partial Clone
| Command | Description |
|---|---|
git clone --sparse <url> | Clone with sparse checkout (root files only) |
git clone --filter=blob:none --sparse <url> | Sparse partial clone (optimal for monorepos) |
git sparse-checkout set --cone <dirs> | Set directories to check out |
git sparse-checkout add <dir> | Add a directory to sparse checkout |
git sparse-checkout list | List checked-out directories |
git sparse-checkout disable | Disable sparse checkout (get all files) |
Hooks
| Hook | When It Runs |
|---|---|
pre-commit | Before commit is created (lint, format, test) |
prepare-commit-msg | After default message generated, before editor opens |
commit-msg | After message written (validate commit message format) |
post-commit | After commit is created (notifications) |
pre-push | Before push (run tests, check branch rules) |
post-merge | After a merge (install deps, rebuild) |
pre-rebase | Before rebase starts (prevent on shared branches) |
| Command | Description |
|---|---|
git config core.hooksPath <dir> | Set custom hooks directory |
git commit --no-verify | Skip pre-commit and commit-msg hooks |
git push --no-verify | Skip pre-push hook |
Performance & Maintenance
| Command | Description |
|---|---|
git gc | Run garbage collection |
git gc --aggressive | Thorough GC with maximum compression |
git maintenance start | Enable background scheduled maintenance |
git maintenance run | Run all maintenance tasks now |
git commit-graph write --reachable --changed-paths | Generate commit-graph with Bloom filters |
git count-objects -v | Show object storage statistics |
git config core.fsmonitor true | Enable filesystem monitor (speeds up git status) |
git config core.untrackedCache true | Cache untracked file state |
git config rerere.enabled true | Enable reuse of recorded conflict resolutions |
git rerere forget <file> | Forget a recorded resolution |
git repack -a -d | Repack all objects into one packfile |
Commit Signing
| Command | Description |
|---|---|
git config gpg.format ssh | Use SSH keys for signing |
git config user.signingkey <key> | Set signing key (GPG ID or SSH pubkey path) |
git config commit.gpgsign true | Sign all commits automatically |
git config tag.gpgsign true | Sign all tags automatically |
git commit -S -m "<msg>" | Create a signed commit (one-time) |
git tag -s <name> -m "<msg>" | Create a signed tag |
git log --show-signature | Show signatures in log |
git verify-commit <commit> | Verify a commit's signature |
git verify-tag <tag> | Verify a tag's signature |
Git Notes
| Command | Description |
|---|---|
git notes add -m "<msg>" <commit> | Add a note to a commit |
git notes append -m "<msg>" <commit> | Append to an existing note |
git notes edit <commit> | Edit a note in your editor |
git notes remove <commit> | Remove a note |
git notes --ref=<ns> add -m "<msg>" | Add a note in a namespace |
git log --show-notes | Show notes in log output |
git log --show-notes="*" | Show notes from all namespaces |
Useful Inspection Commands
| Command | Description |
|---|---|
git rev-parse HEAD | Show the full SHA of HEAD |
git rev-parse --abbrev-ref HEAD | Show current branch name |
git cat-file -t <hash> | Show object type (blob/tree/commit/tag) |
git cat-file -p <hash> | Pretty-print object contents |
git ls-files | List all tracked files |
git ls-tree HEAD | List files in the current commit's tree |
git hash-object <file> | Compute the SHA-1 hash of a file |
git check-mailmap "<name> <email>" | Test .mailmap resolution |
Graph Traversal Syntax
| Syntax | Meaning |
|---|---|
HEAD~1 or HEAD~ | First parent (one generation back) |
HEAD~3 | Three generations back (great-grandparent) |
HEAD^1 or HEAD^ | First parent of a merge |
HEAD^2 | Second parent of a merge (the merged branch) |
HEAD~2^2 | Second parent of the grandparent |
@ | Shorthand for HEAD |
@{-1} | The branch you were on before the last git switch |
main@{upstream} or main@{u} | Upstream tracking branch for main |
Range Notation
| Syntax | In git log | In git diff |
|---|---|---|
A..B | Commits reachable from B but not A | Diff between A and B |
A...B | Commits reachable from either but not both | Diff of B since it diverged from A |
Common .gitignore Patterns
# Build output
build/
dist/
*.o
*.class
# Dependencies
node_modules/
vendor/
.venv/
# IDE files
.idea/
.vscode/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Environment/secrets
.env
.env.local
*.pem
*.key
# Logs
*.log
logs/
# Coverage/testing
coverage/
.nyc_output/Quick Reference: Everyday Commands
# Morning routine
git fetch --prune
git status
# Feature development
git switch -c feature/my-feature
git add -p # stage interactively
git commit -m "feat: add feature"
git push -u origin feature/my-feature
# Stay up to date
git fetch origin
git rebase origin/main
# After code review
git push --force-with-lease
# Clean up after merge
git switch main
git pull
git branch -d feature/my-feature