Skip to content

Latest commit

Β 

History

History
164 lines (112 loc) Β· 3.3 KB

File metadata and controls

164 lines (112 loc) Β· 3.3 KB

Episode 8: Power Tools ⚑

The hidden gems: git notes, git archive, git format-patch, git clean, and more weapons for the Git black belt.

Git Notes β€” Attach Metadata to Commits

Add notes to commits without changing the commit hash. Perfect for code review comments, deployment markers, or audit trails.

# Add a note to the current commit
git notes add -m "Deployed to production 2024-03-15"

# Add a note to a specific commit
git notes add -m "Reviewed by Alice" abc1234

# Show notes
git log --show-notes

# Edit a note
git notes edit abc1234

# Remove a note
git notes remove abc1234

Notes live in refs/notes/commits and can be pushed/fetched:

git push origin refs/notes/*
git fetch origin refs/notes/*:refs/notes/*

Git Archive β€” Export Without .git

Create a clean tarball or zip of your project without the .git directory:

# Create a tar.gz of the current HEAD
git archive --format=tar.gz --prefix=myproject/ HEAD > release.tar.gz

# Create a zip of a specific tag
git archive --format=zip v1.0 > v1.0.zip

# Archive only a subdirectory
git archive HEAD -- episodes/ > episodes.tar.gz

# Archive a specific branch
git archive --format=tar.gz feature/search > feature-search.tar.gz

Unlike tar -czf, git archive respects .gitignore and export-ignore attributes.

Git Format-Patch & Am β€” Email-Style Patches

The original Git collaboration workflow β€” create and apply patches via email or files.

# Create patch files for the last 3 commits
git format-patch -3

# Create a single patch for a branch vs main
git format-patch main..feature/search

# Apply a patch
git am 0001-add-search-feature.patch

# Apply a series of patches
git am *.patch

# If a patch conflicts, fix it and continue
git am --continue
# Or abort
git am --abort

Git Clean β€” Remove Untracked Files

# Dry run β€” see what WOULD be deleted
git clean -n

# Actually delete untracked files
git clean -f

# Delete untracked files AND directories
git clean -fd

# Delete everything, including .gitignore'd files
git clean -fdx

# Interactive mode β€” choose what to delete
git clean -i

Always run git clean -n first. This is a destructive operation.

Git Worktree + Bisect Combo

Combining Episodes 1 and 2 for maximum power:

# Create a worktree specifically for bisecting
git worktree add ../bisect-tree HEAD

cd ../bisect-tree
git bisect start
git bisect bad HEAD
git bisect good v1.0
git bisect run python -m pytest tests/

# When done
git bisect reset
cd ..
git worktree remove bisect-tree

Your main working directory is completely undisturbed.

Git Rev-Parse β€” The Plumber's Tool

# Get the full SHA of HEAD
git rev-parse HEAD

# Get the short SHA
git rev-parse --short HEAD

# Get the root of the repo
git rev-parse --show-toplevel

# Check if you're inside a git repo
git rev-parse --is-inside-work-tree

# Get the .git directory path
git rev-parse --git-dir

Git Diff Tricks

# Word-level diff (instead of line-level)
git diff --word-diff

# Show only file names
git diff --name-only

# Show stat summary
git diff --stat

# Diff between branches
git diff main..feature/search

# Diff of staged changes
git diff --cached

# Diff ignoring whitespace
git diff -w

Run the Demo

bash episodes/demos/08-power-tools-demo.sh