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
Set global user name
git config --global user.name "Your Name"

# Set default author name.

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

# Set default author email.

Set default editor
git config --global core.editor "code --wait"

# Choose editor for commit messages and interactive rebase.

Set default branch name
git config --global init.defaultBranch main

# Use main for new repositories.

Set pull to rebase by default
git config --global pull.rebase true

# Avoid merge commits on pull.

Enable rerere
git config --global rerere.enabled true

# Reuse recorded resolution for repeated conflicts.

Create status alias
git config --global alias.st status

# Add a short alias.

Trust repository path
git config --global --add safe.directory /path/to/repo

# Allow Git to operate in a repository path marked safe.

List config with source
git config --list --show-origin

# Show active configuration and where each value came from.

Open config docs
git help config

# View the Git config manual.

## Repository Creation and Discovery
Initialize repository
git init

# Create a new repository in the current directory.

Initialize bare repository
git init --bare repo.git

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

Clone via HTTPS
git clone https://github.com/owner/repo.git

# Clone a remote repository.

Clone a single branch
git clone --branch main --single-branch https://github.com/owner/repo.git

# Clone only one branch history.

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

# Clone limited history for faster checkout.

Partial clone with blob filter
git clone --filter=blob:none https://github.com/owner/repo.git

# Reduce object download size for large repos.

Show repo root
git rev-parse --show-toplevel

# Print the top-level working tree directory.

Create tar archive from HEAD
git archive --format=tar HEAD > repo.tar

# Create an archive from a tree-ish.

Create zip archive with prefix
git archive --format=zip --prefix=repo/ HEAD > repo.zip

# Bundle repository files without the .git directory.

## Snapshotting and Commits
Status short format
git status --short

# Compact status listing.

Status with branch info
git status -sb

# Short status plus branch tracking summary.

Stage a path
git add path/to/file

# Stage a specific file.

Stage all changes
git add -A

# Stage modified, deleted, and untracked files.

Interactively stage hunks
git add -p

# Select hunks to stage.

Discard unstaged changes
git restore path/to/file

# Restore file contents from HEAD or index.

Unstage a path
git restore --staged path/to/file

# Remove a file from the index without losing edits.

Remove tracked file
git rm path/to/file

# Delete file and stage the removal.

Stop tracking a file
git rm --cached path/to/file

# Keep the file locally but remove it from version control.

Rename tracked file
git mv old-name new-name

# Move or rename and stage the change.

Commit staged changes
git commit -m "Describe your change"

# Create a commit with a message.

Commit tracked changes
git commit -am "Commit message"

# Commit tracked file modifications without separate add.

Amend last commit without editing message
git commit --amend --no-edit

# Add staged changes into the previous commit.

Create fixup commit
git commit --fixup=<commit_sha>

# Prepare a fixup for autosquash rebase.

Create empty commit
git commit --allow-empty -m "Trigger pipeline"

# Record an empty commit, often for automation.

## Branching and Switching
List all branches
git branch -a

# Show local and remote-tracking branches.

List branches with last commit
git branch -vv

# Include tracking and latest commit info.

Create branch
git branch feature/my-change

# Create a branch at current HEAD.

Create and switch branch
git switch -c feature/my-change

# Create and switch in one step.

Switch branches
git switch main

# Switch to an existing branch.

Checkout commit detached
git checkout <commit_sha>

# Inspect an older commit in detached HEAD state.

Rename current branch
git branch -m new-branch-name

# Rename current branch.

Delete merged branch
git branch -d feature/my-change

# Delete a merged local branch.

Force delete branch
git branch -D feature/my-change

# Delete local branch regardless of merge state.

Find merge base
git merge-base main feature/my-change

# Find common ancestor for two refs.

## Remotes and Collaboration
List remotes
git remote -v

# Show remote names and URLs.

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

# Add a remote repository.

Change remote URL
git remote set-url origin git@github.com:owner/repo.git

# Update fetch and push destination.

Fetch from origin
git fetch origin

# Download remote refs and objects.

Fetch all remotes and prune
git fetch --all --prune

# Refresh all remotes and remove stale remote-tracking branches.

Pull with rebase
git pull --rebase

# Fetch and replay local commits on top of upstream.

Push and set upstream
git push -u origin feature/my-change

# Push a new branch and configure tracking.

Force push with lease
git push --force-with-lease

# Safer force push that refuses if remote moved unexpectedly.

Set upstream branch
git branch --set-upstream-to=origin/main main

# Link local branch to a remote-tracking branch.

Prune stale origin branches
git remote prune origin

# Delete stale remote-tracking refs.

List refs on remote
git ls-remote --heads origin

# Inspect refs advertised by a remote.

## Merge, Rebase, and Commit Selection
Merge with merge commit
git merge --no-ff feature/my-change

# Preserve branch history with an explicit merge commit.

Squash merge
git merge --squash feature/my-change

# Stage combined branch changes without creating a merge commit.

Abort merge
git merge --abort

# Return to pre-merge state after conflicts.

Rebase current branch onto main
git rebase main

# Replay local commits on top of main.

Interactive rebase last 10 commits
git rebase -i HEAD~10

# Reorder, squash, reword, or drop commits.

Interactive rebase with autosquash
git rebase -i --autosquash main

# Automatically place fixup/squash commits.

Continue rebase
git rebase --continue

# Resume after resolving conflicts.

Abort rebase
git rebase --abort

# Stop rebasing and restore branch state.

Cherry-pick commit
git cherry-pick <commit_sha>

# Apply one commit onto the current branch.

Cherry-pick range
git cherry-pick A^..B

# Apply a contiguous commit range.

Compare unique commits
git cherry -v origin/main

# Show commits not yet upstream.

Show rerere status
git rerere status

# Inspect remembered conflict resolutions.

## History and Inspection
Graph log
git log --oneline --decorate --graph --all

# Compact visual history.

History for one path
git log -- path/to/file

# Show commits touching a file.

Log with file stats
git log --stat

# Show changed files with commit history.

Show commit details
git show <commit_sha>

# Display metadata, diff, and patch.

Diff unstaged changes
git diff

# Compare working tree to index.

Diff staged changes
git diff --staged

# Compare index to HEAD.

Diff branches
git diff main..feature/my-change

# Compare two refs.

Blame file lines
git blame path/to/file

# See who last changed each line.

Search tracked files
git grep "search text"

# Search in tracked content.

Summarize by author
git shortlog -sn

# Count commits grouped by author.

Describe commit from nearest tag
git describe --tags

# Generate human-readable version identifiers.

## Stash, Clean, and Temporary Work
Stash current work
git stash push -m "WIP"

# Save tracked changes.

Stash including untracked
git stash push -u -m "WIP with untracked"

# Save tracked and untracked files.

List stashes
git stash list

# Inspect stash stack.

Show stash patch
git stash show -p stash@{0}

# Inspect stash content as a patch.

Apply stash
git stash apply stash@{0}

# Restore stash without dropping it.

Pop stash
git stash pop

# Apply latest stash and remove it.

Drop stash entry
git stash drop stash@{0}

# Delete a stash entry.

Create branch from stash
git stash branch fixup stash@{0}

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

Preview clean
git clean -nd

# See what untracked files would be removed.

Remove untracked files and dirs
git clean -fd

# Delete untracked files and directories.

Remove ignored files too
git clean -fdX

# Clean build artifacts and ignored files.

## Tags and Releases
List tags
git tag

# Show local tags.

Create annotated tag
git tag -a v1.2.0 -m "Release v1.2.0"

# Create a release tag with metadata.

Create signed tag
git tag -s v1.2.0 -m "Signed release v1.2.0"

# Sign a release tag with GPG.

Show tag details
git show v1.2.0

# Inspect tag target and metadata.

Push one tag
git push origin v1.2.0

# Publish a tag.

Push all tags
git push origin --tags

# Publish all local tags.

Delete local tag
git tag -d v1.2.0

# Remove a local tag.

Delete remote tag
git push origin :refs/tags/v1.2.0

# Remove a tag from the remote.

## Undo, Reset, Revert, and Recovery
Soft reset one commit
git reset --soft HEAD~1

# Move HEAD back and keep changes staged.

Mixed reset one commit
git reset --mixed HEAD~1

# Move HEAD back and unstage changes.

Hard reset one commit
git reset --hard HEAD~1

# Discard commits and working tree changes.

Reset file in index
git reset HEAD path/to/file

# Unstage a file using reset syntax.

Revert commit
git revert <commit_sha>

# Create a commit that undoes an earlier commit.

Revert range without committing
git revert --no-commit A^..B

# Stage reverse patches for a range and commit later.

Show reflog
git reflog

# See where refs and HEAD pointed previously.

Recover by reflog entry
git reset --hard HEAD@{1}

# Restore earlier branch state via reflog.

Find dangling objects
git fsck --lost-found

# Locate dangling commits and blobs after mistakes.

Create orphan branch
git checkout --orphan gh-pages

# Start a history with no parent commit.

## Worktrees, Submodules, and Sparse Checkout
List worktrees
git worktree list

# See linked working trees for the repo.

Add worktree for branch
git worktree add ../repo-feature feature/my-change

# Create another working tree for a branch.

Add worktree and create branch
git worktree add -b fix/login ../repo-fix main

# Create new branch and worktree together.

Remove worktree
git worktree remove ../repo-feature

# Delete a linked worktree.

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

# Track another repo as a submodule.

Init and update submodules
git submodule update --init --recursive

# Populate nested submodule content.

Show submodule status
git submodule status --recursive

# Inspect submodule commit pointers.

Init sparse checkout cone mode
git sparse-checkout init --cone

# Prepare a sparse working tree.

Set sparse directories
git sparse-checkout set src docs

# Limit checkout to selected directories.

Disable sparse checkout
git sparse-checkout disable

# Re-expand working tree.

## Search, Filter, and Select Commits
Log by author
git log --author="Alice"

# Filter commits by author.

Log by message text
git log --grep="fix login"

# Find commits with matching messages.

Log since date
git log --since="2026-01-01"

# Limit history by time.

Pickaxe search for string
git log -S "FeatureFlag" -- source/file.ts

# Find commits that changed occurrence count for a string.

Pickaxe regex
git log -G "useFeature\(" -- "*.ts"

# Find commits whose patch matches a regex.

Count commits on branch
git rev-list --count HEAD

# Return commit count for a ref.

Name a commit
git name-rev --name-only <commit_sha>

# Describe a commit relative to branch/tag refs.

Compare two commit series
git range-diff origin/main...v1 origin/main...v2

# Review rewritten patch series.

## Bundles, Bisect, and Maintenance
Create bundle
git bundle create repo.bundle --all

# Create a self-contained bundle of refs and objects.

Verify bundle
git bundle verify repo.bundle

# Check if a bundle can be imported.

Clone from bundle
git clone repo.bundle repo-from-bundle

# Clone from a local bundle file.

Start bisect
git bisect start

# Begin binary search for a bad commit.

Mark bad revision
git bisect bad

# Mark current commit as bad.

Mark good revision
git bisect good v1.0.0

# Mark a known-good revision.

Automate bisect test
git bisect run ./test-script.sh

# Use script exit codes to drive bisect.

Exit bisect
git bisect reset

# Return to original branch state.

Run aggressive garbage collection
git gc --aggressive

# Compact repository storage more aggressively.

Run maintenance tasks
git maintenance run

# Execute configured maintenance operations.

## Selected Server and Admin Commands
Clone mirror
git clone --mirror git@github.com:owner/repo.git

# Create a mirror including refs suitable for replication.

Update remotes
git remote update

# Fetch updates for configured remotes.

Update dumb transport info
git update-server-info

# Refresh metadata for dumb HTTP serving.

Mark repo exportable
touch .git/git-daemon-export-ok

# Allow export via git-daemon when configured.

Set symbolic HEAD ref
git symbolic-ref HEAD refs/heads/main

# Point HEAD to a named branch ref.