Cherry-Pick
Apply specific commit to current branch.
git cherry-pick <commit-hash>
Cherry-pick without committing:
git cherry-pick -n <commit-hash>
Cherry-pick range of commits:
git cherry-pick <start-commit>..<end-commit>
Bisect - Find Bug Introduction
Binary search to find commit that introduced a bug.
git bisect start
git bisect bad # current commit is bad
git bisect good <commit-hash> # known good commit
Mark each commit as good or bad:
git bisect good # or
git bisect bad
End bisect session:
git bisect reset
Submodules
Add a submodule to your repository.
git submodule add <repository-url> <path>
Clone repository with submodules:
git clone --recursive <repository-url>
Initialize and update submodules:
git submodule init
git submodule update
Update all submodules to latest:
git submodule update --remote
Worktrees
Create multiple working directories from one repository.
git worktree add <path> <branch-name>
List all worktrees:
git worktree list
Remove worktree:
git worktree remove <path>
Prune deleted worktrees:
git worktree prune
Interactive Rebase
Edit, reorder, or squash commits.
git rebase -i HEAD~5
Commands in interactive rebase:
# pick = use commit
# reword = use commit, edit message
# edit = use commit, stop for amending
# squash = combine with previous
# fixup = like squash, discard message
# drop = remove commit
Reflog Recovery
Recover lost commits or branches.
git reflog
Restore to specific reflog entry:
git reset --hard HEAD@{2}
Filter Branch
Rewrite entire branch history.
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
Remove file from all commits:
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch <file>" \
--prune-empty --tag-name-filter cat -- --all
Git Hooks
Automate actions on Git events. Located in .git/hooks/
# pre-commit - runs before commit
# post-commit - runs after commit
# pre-push - runs before push
# Example pre-commit hook:
#!/bin/sh
npm test
Sparse Checkout
Check out only specific directories.
git sparse-checkout init --cone
git sparse-checkout set <directory1> <directory2>
Shallow Clone
Clone with limited history depth.
git clone --depth 1 <repository-url>
Fetch more history later:
git fetch --depth=100
Archive Repository
Create archive of repository files.
git archive --format=zip HEAD > archive.zip
Archive specific branch:
git archive --format=tar main | gzip > archive.tar.gz
Blame with Line Range
Show who modified specific lines.
git blame -L 10,20 <filename>
Find Large Files
Identify large files in repository history.
git rev-list --objects --all | \
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
sed -n 's/^blob //p' | \
sort --numeric-sort --key=2 | \
tail -10