Git Advanced Cheat Sheet

Advanced Git commands and workflows for rewriting history, recovering commits, debugging regressions, working with multiple trees, and maintaining repositories.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## History Rewriting
Interactive rebase last 5 commits
git rebase -i HEAD~5

# Open the last 5 commits in the interactive rebase todo list.

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

# Replace the most recent commit using the current staged changes.

Rebase a branch onto a new base
git rebase --onto main old-base feature

# Move a branch from one base commit or branch to another.

Rewrite history to keep a subdirectory
git filter-branch --subdirectory-filter path/to/subdir -- --all

# Create a new history containing only one subdirectory.

Interactive rebase preserving merges
git rebase -i --rebase-merges main

# Rebase while preserving merge topology.

## Recovery and Debugging
Show reflog with local timestamps
git reflog --date=local

# Inspect branch and HEAD movements with readable times.

Start a bisect session
git bisect start && git bisect bad && git bisect good <known_good_commit>

# Begin a binary search to find the commit that introduced a bug.

Inspect ORIG_HEAD
git show ORIG_HEAD

# View the previous HEAD after disruptive operations.

Check repository integrity
git fsck --full

# Verify object connectivity and repository health.

## Advanced Working Tree Management
Create an additional worktree for a branch
git worktree add ../feature feature-branch

# Check out another branch in a separate directory without cloning again.

Stash including untracked files
git stash push -u -m "WIP before rebase"

# Save tracked and untracked work temporarily.

Preview removal of untracked files and directories
git clean -ndx

# Dry-run a repository cleanup before deleting anything.

Check rerere status
git rerere status

# Inspect recorded conflict-resolution reuse state.

## Specialized Tools
Cherry-pick a range without committing
git cherry-pick -n <start_commit>^..<end_commit>

# Apply commit changes into the index and working tree first.

Initialize and update submodules recursively
git submodule update --init --recursive

# Populate nested submodules after clone or after config changes.

Attach a note to a commit
git notes add -m "Reviewed by security team" <commit_sha>

# Add metadata to a commit without rewriting it.

Compare two versions of a branch series
git range-diff main...feature-v1 main...feature-v2

# Review how a patch series changed across rebases.

Recommended next

No recommendations yet.