Git Cheat Sheet

Comprehensive Git commands for day-to-day work, collaboration, history, releases, recovery, and advanced repository workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Setup and Configuration

Install identity, defaults, aliases, editors, and safety settings.

Set global user name

Set default author name.

bashANYconfigidentity
bash
git config --global user.name "Your Name"

Set global email

Set default author email.

bashANYconfigidentity
bash
git config --global user.email "you@example.com"

Set default editor

Choose editor for commit messages and interactive rebase.

bashANYconfigeditor
bash
git config --global core.editor "code --wait"

Set default branch name

Use main for new repositories.

bashANYconfigbranch
bash
git config --global init.defaultBranch main

Set pull to rebase by default

Avoid merge commits on pull.

bashANYconfigpullrebase
bash
git config --global pull.rebase true

Enable rerere

Reuse recorded resolution for repeated conflicts.

bashANYconfigmergererere
bash
git config --global rerere.enabled true

Create status alias

Add a short alias.

bashANYconfigalias
bash
git config --global alias.st status

Trust repository path

Allow Git to operate in a repository path marked safe.

bashANYconfigsecurity
bash
git config --global --add safe.directory /path/to/repo

List config with source

Show active configuration and where each value came from.

bashANYconfiginspect
bash
git config --list --show-origin

Open config docs

View the Git config manual.

bashANYhelpconfig
bash
git help config

Repository Creation and Discovery

Start, clone, inspect, and archive repositories.

Initialize repository

Create a new repository in the current directory.

bashANYinitrepository
bash
git init

Initialize bare repository

Create a repository without a working tree for server or shared use.

bashANYinitbare
bash
git init --bare repo.git

Clone via HTTPS

Clone a remote repository.

bashANYcloneremote
bash
git clone https://github.com/owner/repo.git

Clone a single branch

Clone only one branch history.

bashANYclonebranch
bash
git clone --branch main --single-branch https://github.com/owner/repo.git

Shallow clone

Clone limited history for faster checkout.

bashANYcloneshallow
bash
git clone --depth 1 https://github.com/owner/repo.git

Partial clone with blob filter

Reduce object download size for large repos.

bashANYclonepartial
bash
git clone --filter=blob:none https://github.com/owner/repo.git

Show repo root

Print the top-level working tree directory.

bashANYrepositoryinspect
bash
git rev-parse --show-toplevel

Create tar archive from HEAD

Create an archive from a tree-ish.

bashANYarchive
bash
git archive --format=tar HEAD > repo.tar

Create zip archive with prefix

Bundle repository files without the .git directory.

bashANYarchivezip
bash
git archive --format=zip --prefix=repo/ HEAD > repo.zip

Snapshotting and Commits

Stage, commit, amend, remove, move, and record history.

Status short format

Compact status listing.

bashANYstatusinspect
bash
git status --short

Status with branch info

Short status plus branch tracking summary.

bashANYstatusbranch
bash
git status -sb

Stage a path

Stage a specific file.

bashANYaddstage
bash
git add path/to/file

Stage all changes

Stage modified, deleted, and untracked files.

bashANYaddstage
bash
git add -A

Interactively stage hunks

Select hunks to stage.

bashANYaddpatchinteractive
bash
git add -p

Discard unstaged changes

Restore file contents from HEAD or index.

bashANYrestoreundo
bash
git restore path/to/file

Unstage a path

Remove a file from the index without losing edits.

bashANYrestorestageundo
bash
git restore --staged path/to/file

Remove tracked file

Delete file and stage the removal.

bashANYrmdelete
bash
git rm path/to/file

Stop tracking a file

Keep the file locally but remove it from version control.

bashANYrmignore
bash
git rm --cached path/to/file

Rename tracked file

Move or rename and stage the change.

bashANYmvrename
bash
git mv old-name new-name

Commit staged changes

Create a commit with a message.

bashANYcommit
bash
git commit -m "Describe your change"

Commit tracked changes

Commit tracked file modifications without separate add.

bashANYcommit
bash
git commit -am "Commit message"

Amend last commit without editing message

Add staged changes into the previous commit.

bashANYcommitamend
bash
git commit --amend --no-edit

Create fixup commit

Prepare a fixup for autosquash rebase.

bashANYcommitfixuprebase
bash
git commit --fixup=<commit_sha>

Create empty commit

Record an empty commit, often for automation.

bashANYcommitci
bash
git commit --allow-empty -m "Trigger pipeline"

Branching and Switching

Create, rename, delete, and inspect branches and checkouts.

List all branches

Show local and remote-tracking branches.

bashANYbranchinspect
bash
git branch -a

List branches with last commit

Include tracking and latest commit info.

bashANYbranchinspect
bash
git branch -vv

Create branch

Create a branch at current HEAD.

bashANYbranchcreate
bash
git branch feature/my-change

Create and switch branch

Create and switch in one step.

bashANYbranchswitch
bash
git switch -c feature/my-change

Switch branches

Switch to an existing branch.

bashANYbranchswitch
bash
git switch main

Checkout commit detached

Inspect an older commit in detached HEAD state.

bashANYcheckoutdetached-head
bash
git checkout <commit_sha>

Rename current branch

Rename current branch.

bashANYbranchrename
bash
git branch -m new-branch-name

Delete merged branch

Delete a merged local branch.

bashANYbranchdelete
bash
git branch -d feature/my-change

Force delete branch

Delete local branch regardless of merge state.

bashANYbranchdeletedanger
bash
git branch -D feature/my-change

Find merge base

Find common ancestor for two refs.

bashANYbranchmerge-base
bash
git merge-base main feature/my-change

Remotes and Collaboration

Remotes, fetch, pull, push, prune, and upstream tracking.

List remotes

Show remote names and URLs.

bashANYremote
bash
git remote -v

Add origin remote

Add a remote repository.

bashANYremoteadd
bash
git remote add origin https://github.com/owner/repo.git

Change remote URL

Update fetch and push destination.

bashANYremoteurl
bash
git remote set-url origin git@github.com:owner/repo.git

Fetch from origin

Download remote refs and objects.

bashANYfetchremote
bash
git fetch origin

Fetch all remotes and prune

Refresh all remotes and remove stale remote-tracking branches.

bashANYfetchprune
bash
git fetch --all --prune

Pull with rebase

Fetch and replay local commits on top of upstream.

bashANYpullrebase
bash
git pull --rebase

Push and set upstream

Push a new branch and configure tracking.

bashANYpushupstream
bash
git push -u origin feature/my-change

Force push with lease

Safer force push that refuses if remote moved unexpectedly.

bashANYpushforcesafety
bash
git push --force-with-lease

Set upstream branch

Link local branch to a remote-tracking branch.

bashANYbranchupstream
bash
git branch --set-upstream-to=origin/main main

Prune stale origin branches

Delete stale remote-tracking refs.

bashANYremoteprune
bash
git remote prune origin

List refs on remote

Inspect refs advertised by a remote.

bashANYremoterefsinspect
bash
git ls-remote --heads origin

Merge, Rebase, and Commit Selection

Integrate branches with merges, rebases, cherry-picks, and conflict tools.

Merge with merge commit

Preserve branch history with an explicit merge commit.

bashANYmerge
bash
git merge --no-ff feature/my-change

Squash merge

Stage combined branch changes without creating a merge commit.

bashANYmergesquash
bash
git merge --squash feature/my-change

Abort merge

Return to pre-merge state after conflicts.

bashANYmergeundo
bash
git merge --abort

Rebase current branch onto main

Replay local commits on top of main.

bashANYrebase
bash
git rebase main

Interactive rebase last 10 commits

Reorder, squash, reword, or drop commits.

bashANYrebaseinteractive
bash
git rebase -i HEAD~10

Interactive rebase with autosquash

Automatically place fixup/squash commits.

bashANYrebaseautosquash
bash
git rebase -i --autosquash main

Continue rebase

Resume after resolving conflicts.

bashANYrebaseconflict
bash
git rebase --continue

Abort rebase

Stop rebasing and restore branch state.

bashANYrebaseundo
bash
git rebase --abort

Cherry-pick commit

Apply one commit onto the current branch.

bashANYcherry-pick
bash
git cherry-pick <commit_sha>

Cherry-pick range

Apply a contiguous commit range.

bashANYcherry-pick
bash
git cherry-pick A^..B

Compare unique commits

Show commits not yet upstream.

bashANYcherrycompare
bash
git cherry -v origin/main

Show rerere status

Inspect remembered conflict resolutions.

bashANYrereremerge
bash
git rerere status

History and Inspection

Explore logs, show commits, blame, grep, diff, and file history.

Graph log

Compact visual history.

bashANYloghistory
bash
git log --oneline --decorate --graph --all

History for one path

Show commits touching a file.

bashANYlogpath
bash
git log -- path/to/file

Log with file stats

Show changed files with commit history.

bashANYlogstat
bash
git log --stat

Show commit details

Display metadata, diff, and patch.

bashANYshowhistory
bash
git show <commit_sha>

Diff unstaged changes

Compare working tree to index.

bashANYdiff
bash
git diff

Diff staged changes

Compare index to HEAD.

bashANYdiffstaged
bash
git diff --staged

Diff branches

Compare two refs.

bashANYdiffbranch
bash
git diff main..feature/my-change

Blame file lines

See who last changed each line.

bashANYblame
bash
git blame path/to/file

Search tracked files

Search in tracked content.

bashANYgrepsearch
bash
git grep "search text"

Summarize by author

Count commits grouped by author.

bashANYshortlogreporting
bash
git shortlog -sn

Describe commit from nearest tag

Generate human-readable version identifiers.

bashANYdescribetags
bash
git describe --tags

Stash, Clean, and Temporary Work

Save incomplete work, recover it, and remove generated files.

Stash current work

Save tracked changes.

bashANYstash
bash
git stash push -m "WIP"

Stash including untracked

Save tracked and untracked files.

bashANYstashuntracked
bash
git stash push -u -m "WIP with untracked"

List stashes

Inspect stash stack.

bashANYstashinspect
bash
git stash list

Show stash patch

Inspect stash content as a patch.

bashANYstashdiff
bash
git stash show -p stash@{0}

Apply stash

Restore stash without dropping it.

bashANYstashapply
bash
git stash apply stash@{0}

Pop stash

Apply latest stash and remove it.

bashANYstashapply
bash
git stash pop

Drop stash entry

Delete a stash entry.

bashANYstashdelete
bash
git stash drop stash@{0}

Create branch from stash

Create branch, apply stash, and drop it on success.

bashANYstashbranch
bash
git stash branch fixup stash@{0}

Preview clean

See what untracked files would be removed.

bashANYcleandry-run
bash
git clean -nd

Remove untracked files and dirs

Delete untracked files and directories.

bashANYcleandanger
bash
git clean -fd

Remove ignored files too

Clean build artifacts and ignored files.

bashANYcleanignored
bash
git clean -fdX

Tags and Releases

Create, inspect, sign, and push tags used for releases.

List tags

Show local tags.

bashANYtag
bash
git tag

Create annotated tag

Create a release tag with metadata.

bashANYtagrelease
bash
git tag -a v1.2.0 -m "Release v1.2.0"

Create signed tag

Sign a release tag with GPG.

bashANYtagsign
bash
git tag -s v1.2.0 -m "Signed release v1.2.0"

Show tag details

Inspect tag target and metadata.

bashANYtaginspect
bash
git show v1.2.0

Push one tag

Publish a tag.

bashANYtagpush
bash
git push origin v1.2.0

Push all tags

Publish all local tags.

bashANYtagpush
bash
git push origin --tags

Delete local tag

Remove a local tag.

bashANYtagdelete
bash
git tag -d v1.2.0

Delete remote tag

Remove a tag from the remote.

bashANYtagdeleteremote
bash
git push origin :refs/tags/v1.2.0

Undo, Reset, Revert, and Recovery

Safely undo work, rewrite local state, and recover mistakes.

Soft reset one commit

Move HEAD back and keep changes staged.

bashANYresetundo
bash
git reset --soft HEAD~1

Mixed reset one commit

Move HEAD back and unstage changes.

bashANYresetundo
bash
git reset --mixed HEAD~1

Hard reset one commit

Discard commits and working tree changes.

bashANYresetdanger
bash
git reset --hard HEAD~1

Reset file in index

Unstage a file using reset syntax.

bashANYresetstage
bash
git reset HEAD path/to/file

Revert commit

Create a commit that undoes an earlier commit.

bashANYrevertundo
bash
git revert <commit_sha>

Revert range without committing

Stage reverse patches for a range and commit later.

bashANYrevertrange
bash
git revert --no-commit A^..B

Show reflog

See where refs and HEAD pointed previously.

bashANYreflogrecovery
bash
git reflog

Recover by reflog entry

Restore earlier branch state via reflog.

bashANYreflogresetrecovery
bash
git reset --hard HEAD@{1}

Find dangling objects

Locate dangling commits and blobs after mistakes.

bashANYfsckrecovery
bash
git fsck --lost-found

Create orphan branch

Start a history with no parent commit.

bashANYbranchorphan
bash
git checkout --orphan gh-pages

Worktrees, Submodules, and Sparse Checkout

Advanced repository layouts and large-repo workflows.

List worktrees

See linked working trees for the repo.

bashANYworktree
bash
git worktree list

Add worktree for branch

Create another working tree for a branch.

bashANYworktree
bash
git worktree add ../repo-feature feature/my-change

Add worktree and create branch

Create new branch and worktree together.

bashANYworktreebranch
bash
git worktree add -b fix/login ../repo-fix main

Remove worktree

Delete a linked worktree.

bashANYworktreeremove
bash
git worktree remove ../repo-feature

Add submodule

Track another repo as a submodule.

bashANYsubmodule
bash
git submodule add https://github.com/org/lib.git vendor/lib

Init and update submodules

Populate nested submodule content.

bashANYsubmodule
bash
git submodule update --init --recursive

Show submodule status

Inspect submodule commit pointers.

bashANYsubmoduleinspect
bash
git submodule status --recursive

Init sparse checkout cone mode

Prepare a sparse working tree.

bashANYsparse-checkout
bash
git sparse-checkout init --cone

Set sparse directories

Limit checkout to selected directories.

bashANYsparse-checkout
bash
git sparse-checkout set src docs

Disable sparse checkout

Re-expand working tree.

bashANYsparse-checkout
bash
git sparse-checkout disable

Search, Filter, and Select Commits

Revision ranges, ancestry, and content-focused querying.

Log by author

Filter commits by author.

bashANYlogfilter
bash
git log --author="Alice"

Log by message text

Find commits with matching messages.

bashANYloggrep
bash
git log --grep="fix login"

Log since date

Limit history by time.

bashANYlogdate
bash
git log --since="2026-01-01"

Pickaxe search for string

Find commits that changed occurrence count for a string.

bashANYlogpickaxe
bash
git log -S "FeatureFlag" -- source/file.ts

Pickaxe regex

Find commits whose patch matches a regex.

bashANYlogpickaxeregex
bash
git log -G "useFeature\(" -- "*.ts"

Count commits on branch

Return commit count for a ref.

bashANYrev-listcount
bash
git rev-list --count HEAD

Name a commit

Describe a commit relative to branch/tag refs.

bashANYname-rev
bash
git name-rev --name-only <commit_sha>

Compare two commit series

Review rewritten patch series.

bashANYrange-diffrebase
bash
git range-diff origin/main...v1 origin/main...v2

Bundles, Bisect, and Maintenance

Offline transfer, bug hunting, and repository upkeep.

Create bundle

Create a self-contained bundle of refs and objects.

bashANYbundle
bash
git bundle create repo.bundle --all

Verify bundle

Check if a bundle can be imported.

bashANYbundleverify
bash
git bundle verify repo.bundle

Clone from bundle

Clone from a local bundle file.

bashANYbundleclone
bash
git clone repo.bundle repo-from-bundle

Start bisect

Begin binary search for a bad commit.

bashANYbisect
bash
git bisect start

Mark bad revision

Mark current commit as bad.

bashANYbisect
bash
git bisect bad

Mark good revision

Mark a known-good revision.

bashANYbisect
bash
git bisect good v1.0.0

Automate bisect test

Use script exit codes to drive bisect.

bashANYbisectautomation
bash
git bisect run ./test-script.sh

Exit bisect

Return to original branch state.

bashANYbisectreset
bash
git bisect reset

Run aggressive garbage collection

Compact repository storage more aggressively.

bashANYgcmaintenance
bash
git gc --aggressive

Run maintenance tasks

Execute configured maintenance operations.

bashANYmaintenance
bash
git maintenance run

Selected Server and Admin Commands

Useful commands for mirrors, daemon support, and policy setup.

Clone mirror

Create a mirror including refs suitable for replication.

bashANYmirrorserver
bash
git clone --mirror git@github.com:owner/repo.git

Update remotes

Fetch updates for configured remotes.

bashANYremoteserver
bash
git remote update

Update dumb transport info

Refresh metadata for dumb HTTP serving.

bashANYserver
bash
git update-server-info

Mark repo exportable

Allow export via git-daemon when configured.

ANYserverdaemon
bash
touch .git/git-daemon-export-ok

Set symbolic HEAD ref

Point HEAD to a named branch ref.

bashANYsymbolic-refadmin
bash
git symbolic-ref HEAD refs/heads/main