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

Rewrite commit history safely and intentionally.

Interactive rebase last 5 commits

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

bashANYgitrebaseinteractivehistory
bash
git rebase -i HEAD~5
Notes

Interactive rebase is the main advanced tool for reordering, squashing, editing, or dropping commits before sharing them.

Amend last commit without changing message

Replace the most recent commit using the current staged changes.

bashANYgitcommitamendhistory
bash
git commit --amend --no-edit
Notes

Useful when you forgot to add files or want to correct the content of the last commit while keeping its message.

Rebase a branch onto a new base

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

bashANYgitrebaseontohistory
bash
git rebase --onto main old-base feature
Notes

`--onto` is the advanced rebase form for transplanting a branch or a subset of commits onto a different base.

Rewrite history to keep a subdirectory

Create a new history containing only one subdirectory.

bashANYgithistoryrewritefilter-branch
bash
git filter-branch --subdirectory-filter path/to/subdir -- --all
Notes

Older but still encountered approach for history rewriting. For large or modern repos, many teams prefer `git filter-repo` externally.

Interactive rebase preserving merges

Rebase while preserving merge topology.

bashANYgitrebasemergehistory
bash
git rebase -i --rebase-merges main
Notes

Useful when you need the benefits of interactive rebase but your feature branch already contains meaningful merge structure.

Recovery and Debugging

Recover lost work and pinpoint regressions.

Show reflog with local timestamps

Inspect branch and HEAD movements with readable times.

bashANYgitreflogrecovery
bash
git reflog --date=local
Notes

Reflog is one of the most important recovery tools in Git because it records local reference updates even after history rewriting.

Start a bisect session

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

bashANYgitbisectdebugginghistory
bash
git bisect start && git bisect bad && git bisect good <known_good_commit>
Notes

`git bisect` uses binary search over commit history to isolate the commit that changed a known property, commonly a bug introduction.

Inspect ORIG_HEAD

View the previous HEAD after disruptive operations.

bashANYgitrecoveryorig-head
bash
git show ORIG_HEAD
Notes

Helpful after merge, pull, reset, and rebase operations when you need to inspect where HEAD was before the change.

Check repository integrity

Verify object connectivity and repository health.

bashANYgitfsckintegrityrecovery
bash
git fsck --full
Notes

Useful when investigating possible repository corruption or dangling objects after recovery operations.

Advanced Working Tree Management

Stash, worktree, clean, sparse workflows, and conflict reuse.

Create an additional worktree for a branch

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

bashANYgitworktreeworking-tree
bash
git worktree add ../feature feature-branch
Notes

Worktrees are excellent for parallel branch work, hotfixes, and avoiding branch switching in a single working directory.

Stash including untracked files

Save tracked and untracked work temporarily.

bashANYgitstashworking-tree
bash
git stash push -u -m "WIP before rebase"
Notes

Use `-u` when your temporary work includes new files that are not yet tracked.

Preview removal of untracked files and directories

Dry-run a repository cleanup before deleting anything.

bashANYgitcleanworking-treedanger
bash
git clean -ndx
Notes

Always preview with `-n` first. `-x` includes ignored files, which can be destructive in build directories.

Check rerere status

Inspect recorded conflict-resolution reuse state.

bashANYgitrereremergeconflicts
bash
git rerere status
Notes

`rerere` can save time during repeated conflict resolution in long-lived branches or repeated rebases.

Specialized Tools

Notes, submodules, range selection, and patch-style workflows.

Cherry-pick a range without committing

Apply commit changes into the index and working tree first.

bashANYgitcherry-pickhistory
bash
git cherry-pick -n <start_commit>^..<end_commit>
Notes

Useful when you want to combine multiple picked commits into one local commit or inspect them before committing.

Initialize and update submodules recursively

Populate nested submodules after clone or after config changes.

bashANYgitsubmodulemodules
bash
git submodule update --init --recursive
Notes

Common recovery and onboarding command when a repository uses submodules and nested dependencies.

Attach a note to a commit

Add metadata to a commit without rewriting it.

bashANYgitnotesmetadata
bash
git notes add -m "Reviewed by security team" <commit_sha>
Notes

Git notes let you annotate objects while preserving their original identity and hash.

Compare two versions of a branch series

Review how a patch series changed across rebases.

bashANYgitrange-diffreviewrebase
bash
git range-diff main...feature-v1 main...feature-v2
Notes

Especially useful in advanced review workflows after interactive rebase or when patch series evolve.

Recommended next

No recommendations yet.