Git Advanced
Worktrees
bash1# Create a worktree for a feature branch (avoids stashing) 2git worktree add ../feature-auth feature/auth 3 4# Create a worktree with a new branch 5git worktree add ../hotfix-123 -b hotfix/123 origin/main 6 7# List all worktrees 8git worktree list 9 10# Remove a worktree after merging 11git worktree remove ../feature-auth
Worktrees let you work on multiple branches simultaneously without stashing or committing WIP. Each worktree has its own working directory but shares the same .git repository.
Bisect
bash1# Start bisect, mark current as bad and known good commit 2git bisect start 3git bisect bad HEAD 4git bisect good v1.5.0 5 6# Git checks out a midpoint commit. Test it, then mark: 7git bisect good # if this commit works 8git bisect bad # if this commit is broken 9 10# Automate with a test script 11git bisect start HEAD v1.5.0 12git bisect run npm test 13 14# When done, reset 15git bisect reset
Bisect performs binary search across commits to find which commit introduced a bug. Automated bisect with run is the fastest approach.
Interactive Rebase
bash1# Rebase last 5 commits interactively 2git rebase -i HEAD~5 3 4# Common operations in the editor: 5# pick - keep commit as-is 6# reword - change commit message 7# edit - stop to amend the commit 8# squash - merge into previous commit, keep both messages 9# fixup - merge into previous commit, discard this message 10# drop - remove the commit entirely 11 12# Rebase feature branch onto latest main 13git fetch origin 14git rebase origin/main 15 16# Continue after resolving conflicts 17git rebase --continue 18 19# Abort if things go wrong 20git rebase --abort
Git Hooks
bash1#!/bin/sh 2# .git/hooks/pre-commit 3 4# Run linter on staged files only 5STAGED_FILES=$(git diff --cached --name-only --diff-filter=d | grep -E '\.(ts|tsx|js|jsx)$') 6if [ -n "$STAGED_FILES" ]; then 7 npx eslint $STAGED_FILES --fix 8 git add $STAGED_FILES 9fi
bash1#!/bin/sh 2# .git/hooks/commit-msg 3 4# Enforce conventional commit format 5COMMIT_MSG=$(cat "$1") 6PATTERN="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,72}$" 7 8if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$PATTERN"; then 9 echo "Error: Commit message must follow Conventional Commits format" 10 echo "Example: feat(auth): add OAuth2 login flow" 11 exit 1 12fi
bash1#!/bin/sh 2# .git/hooks/pre-push 3 4# Run tests before pushing 5npm test 6if [ $? -ne 0 ]; then 7 echo "Tests failed. Push aborted." 8 exit 1 9fi
Recovery Techniques
bash1# Undo last commit but keep changes staged 2git reset --soft HEAD~1 3 4# Recover a deleted branch using reflog 5git reflog 6git checkout -b recovered-branch HEAD@{3} 7 8# Recover a file from a specific commit 9git checkout abc1234 -- path/to/file.ts 10 11# Find lost commits (dangling after reset or rebase) 12git fsck --lost-found 13git show <dangling-commit-sha> 14 15# Undo a rebase 16git reflog 17git reset --hard HEAD@{5} # point before rebase started
Useful Aliases
bash1# ~/.gitconfig 2[alias] 3 lg = log --graph --oneline --decorate --all 4 st = status -sb 5 co = checkout 6 unstage = reset HEAD -- 7 last = log -1 HEAD --stat 8 branches = branch -a --sort=-committerdate 9 stash-all = stash push --include-untracked 10 conflicts = diff --name-only --diff-filter=U
Anti-Patterns
- Force-pushing to shared branches without
--force-with-lease - Rebasing commits that have already been pushed and shared
- Committing large binary files without Git LFS
- Using
git add .without reviewinggit diff --staged - Not using
.gitignorefor build artifacts, dependencies, and secrets - Keeping long-lived feature branches instead of merging frequently
Checklist
- Worktrees used for parallel branch work instead of stashing
-
git bisect runautomates bug-finding with a test command - Interactive rebase cleans up commits before merging to main
- Pre-commit hooks run linting on staged files
- Commit message format enforced via commit-msg hook
-
--force-with-leaseused instead of--forcewhen force-pushing - Reflog consulted before any destructive operation
-
.gitignorecovers build outputs, dependencies, and environment files