git config --global user.name "Your Name"Git Cheat Sheet
Comprehensive Git commands for day-to-day work, collaboration, history, releases, recovery, and advanced repository workflows.
Setup and Configuration
Install identity, defaults, aliases, editors, and safety settings.
git config --global user.email "you@example.com"git config --global core.editor "code --wait"git config --global init.defaultBranch maingit config --global pull.rebase truegit config --global rerere.enabled truegit config --global alias.st statusgit config --global --add safe.directory /path/to/repoList config with source
Show active configuration and where each value came from.
git config --list --show-origingit help configRepository Creation and Discovery
Start, clone, inspect, and archive repositories.
git initInitialize bare repository
Create a repository without a working tree for server or shared use.
git init --bare repo.gitgit clone https://github.com/owner/repo.gitgit clone --branch main --single-branch https://github.com/owner/repo.gitgit clone --depth 1 https://github.com/owner/repo.gitgit clone --filter=blob:none https://github.com/owner/repo.gitgit rev-parse --show-toplevelgit archive --format=tar HEAD > repo.tarCreate zip archive with prefix
Bundle repository files without the .git directory.
git archive --format=zip --prefix=repo/ HEAD > repo.zipSnapshotting and Commits
Stage, commit, amend, remove, move, and record history.
git status --shortgit status -sbgit add path/to/filegit add -Agit add -pgit restore path/to/filegit restore --staged path/to/filegit rm path/to/filegit rm --cached path/to/filegit mv old-name new-namegit commit -m "Describe your change"git commit -am "Commit message"Amend last commit without editing message
Add staged changes into the previous commit.
git commit --amend --no-editgit commit --fixup=<commit_sha>git commit --allow-empty -m "Trigger pipeline"Branching and Switching
Create, rename, delete, and inspect branches and checkouts.
git branch -agit branch -vvgit branch feature/my-changegit switch -c feature/my-changegit switch mainCheckout commit detached
Inspect an older commit in detached HEAD state.
git checkout <commit_sha>git branch -m new-branch-namegit branch -d feature/my-changegit branch -D feature/my-changegit merge-base main feature/my-changeRemotes and Collaboration
Remotes, fetch, pull, push, prune, and upstream tracking.
git remote -vgit remote add origin https://github.com/owner/repo.gitgit remote set-url origin git@github.com:owner/repo.gitgit fetch originFetch all remotes and prune
Refresh all remotes and remove stale remote-tracking branches.
git fetch --all --prunegit pull --rebasegit push -u origin feature/my-changeForce push with lease
Safer force push that refuses if remote moved unexpectedly.
git push --force-with-leasegit branch --set-upstream-to=origin/main maingit remote prune origingit ls-remote --heads originMerge, Rebase, and Commit Selection
Integrate branches with merges, rebases, cherry-picks, and conflict tools.
git merge --no-ff feature/my-changegit merge --squash feature/my-changegit merge --abortgit rebase mainInteractive rebase last 10 commits
Reorder, squash, reword, or drop commits.
git rebase -i HEAD~10Interactive rebase with autosquash
Automatically place fixup/squash commits.
git rebase -i --autosquash maingit rebase --continuegit rebase --abortgit cherry-pick <commit_sha>git cherry-pick A^..Bgit cherry -v origin/maingit rerere statusHistory and Inspection
Explore logs, show commits, blame, grep, diff, and file history.
git log --oneline --decorate --graph --allgit log -- path/to/filegit log --statgit show <commit_sha>git diffgit diff --stagedgit diff main..feature/my-changegit blame path/to/filegit grep "search text"git shortlog -sngit describe --tagsStash, Clean, and Temporary Work
Save incomplete work, recover it, and remove generated files.
git stash push -m "WIP"git stash push -u -m "WIP with untracked"git stash listgit stash show -p stash@{0}git stash apply stash@{0}git stash popgit stash drop stash@{0}git stash branch fixup stash@{0}git clean -ndgit clean -fdgit clean -fdXUndo, Reset, Revert, and Recovery
Safely undo work, rewrite local state, and recover mistakes.
git reset --soft HEAD~1git reset --mixed HEAD~1git reset --hard HEAD~1git reset HEAD path/to/filegit revert <commit_sha>Revert range without committing
Stage reverse patches for a range and commit later.
git revert --no-commit A^..Bgit refloggit reset --hard HEAD@{1}git fsck --lost-foundgit checkout --orphan gh-pagesWorktrees, Submodules, and Sparse Checkout
Advanced repository layouts and large-repo workflows.
git worktree listgit worktree add ../repo-feature feature/my-changegit worktree add -b fix/login ../repo-fix maingit worktree remove ../repo-featuregit submodule add https://github.com/org/lib.git vendor/libgit submodule update --init --recursivegit submodule status --recursivegit sparse-checkout init --conegit sparse-checkout set src docsgit sparse-checkout disableSearch, Filter, and Select Commits
Revision ranges, ancestry, and content-focused querying.
git log --author="Alice"git log --grep="fix login"git log --since="2026-01-01"Pickaxe search for string
Find commits that changed occurrence count for a string.
git log -S "FeatureFlag" -- source/file.tsgit log -G "useFeature\(" -- "*.ts"git rev-list --count HEADgit name-rev --name-only <commit_sha>git range-diff origin/main...v1 origin/main...v2Bundles, Bisect, and Maintenance
Offline transfer, bug hunting, and repository upkeep.
git bundle create repo.bundle --allgit bundle verify repo.bundlegit clone repo.bundle repo-from-bundlegit bisect startgit bisect badgit bisect good v1.0.0git bisect run ./test-script.shgit bisect resetRun aggressive garbage collection
Compact repository storage more aggressively.
git gc --aggressivegit maintenance runSelected Server and Admin Commands
Useful commands for mirrors, daemon support, and policy setup.
git clone --mirror git@github.com:owner/repo.gitgit remote updategit update-server-infotouch .git/git-daemon-export-okgit symbolic-ref HEAD refs/heads/main