Beyond basic commits and branches, Git offers powerful features that can supercharge your workflow.
Clean up your commit history before merging:
git rebase -i HEAD~3
# In the editor:
pick abc123 Add feature
squash def456 Fix typo
reword ghi789 Update tests
Apply specific commits from one branch to another:
# Apply a single commit
git cherry-pick abc123
# Apply multiple commits
git cherry-pick abc123..def456
Find the commit that introduced a bug:
git bisect start
git bisect bad # Current commit is bad
git bisect good v1.0 # v1.0 was good
# Git will checkout commits for you to test
git bisect good # or bad
# When done
git bisect reset
Save work-in-progress with descriptive messages:
# Save with message
git stash save "WIP: implementing user auth"
# List all stashes
git stash list
# Apply specific stash
git stash apply stash@{2}
# Create branch from stash
git stash branch feature-auth stash@{0}
Recover "lost" commits:
# View all reference updates
git reflog
# Restore deleted branch
git checkout -b recovered-branch abc123
Work on multiple branches simultaneously:
# Create new worktree
git worktree add ../project-hotfix hotfix-branch
# List worktrees
git worktree list
# Remove when done
git worktree remove ../project-hotfix
Find exactly what you're looking for:
# Commits by author in date range
git log --author="John" --since="2 weeks ago"
# Commits that changed specific file
git log --follow -- src/auth.js
# Search commit messages
git log --grep="bugfix"
# Pretty one-line format
git log --oneline --graph --all
Automate tasks with hooks:
# .git/hooks/pre-commit
#!/bin/sh
npm run lint
npm test
These advanced Git techniques can significantly improve your development workflow. Start with one or two that solve your immediate needs, then gradually incorporate others as you become comfortable.