Mastering Git Workflow: Beyond the Basics

Most developers learn Git basics quicklyβcommit, push, pull. But mastering Git's advanced features transforms how you manage code and collaborate with teams.
Branching Strategies#
Git Flow#
Traditional approach with dedicated branches:
main βββββββββββββββββββββββββββββββββ
β β
hotfix βββ β
β β
develop βββββββββββββββββββββββ
β β
feature/A βββββββββββββββ
β
feature/B βββββββββββββββ
Best for: Release-based software, larger teams
Trunk-Based Development#
Everyone commits to main with short-lived branches:
main ββββββββββββββββββββββββββ
β β β β
features ββββββββββββββββ
Best for: CI/CD, small teams, rapid iteration
GitHub Flow#
Simplified model for continuous deployment:
main ββββββββββββββββββββββββ
β β β
feature ββββββββββββ β
bugfix ββββββββββββββββββ
enhancement ββββββββββββββββββ
Interactive Rebase#
Rewriting history for cleaner commits:
# Rebase last 5 commits
git rebase -i HEAD~5
Options in interactive mode:
pick- Keep commit as-isreword- Change commit messageedit- Pause to amend commitsquash- Combine with previous commitfixup- Like squash, discard messagedrop- Remove commit
Squashing Commits#
Before:
abc1234 WIP
def5678 More WIP
ghi9012 Fix typo
jkl3456 Actually working now
After squash:
mno7890 Implement user authentication
Advanced Stashing#
# Stash with message
git stash push -m "Work in progress on feature X"
# Stash specific files
git stash push -m "Partial changes" -- file1.js file2.js
# List stashes
git stash list
# Apply specific stash
git stash apply stash@{2}
# Create branch from stash
git stash branch new-feature stash@{0}
Cherry-Picking#
Apply specific commits to another branch:
# Single commit
git cherry-pick abc1234
# Range of commits
git cherry-pick abc1234..def5678
# Without committing (stage changes only)
git cherry-pick -n abc1234
Bisect: Finding Bugs#
Binary search through commits to find when a bug was introduced:
# Start bisect
git bisect start
# Mark current as bad
git bisect bad
# Mark known good commit
git bisect good v1.0.0
# Git checks out middle commit
# Test and mark good or bad
git bisect good # or git bisect bad
# Repeat until culprit found
# Reset when done
git bisect reset
Reflog: Recovery Safety Net#
Git tracks all reference updates:
# View reflog
git reflog
# Recover deleted branch
git checkout -b recovered-branch HEAD@{5}
# Undo a rebase
git reset --hard HEAD@{2}
Hooks for Automation#
Git hooks automate tasks:
# .git/hooks/pre-commit
#!/bin/sh
npm run lint
npm run test
Common hooks:
pre-commit- Run before commitcommit-msg- Validate commit messagepre-push- Run before pushpost-merge- Run after merge
Worktrees#
Work on multiple branches simultaneously:
# Create worktree
git worktree add ../project-feature feature-branch
# List worktrees
git worktree list
# Remove worktree
git worktree remove ../project-feature
Best Practices#
- Write meaningful commit messagesText
feat(auth): add OAuth2 login flow - Implement Google OAuth provider - Add token refresh mechanism - Store tokens securely in httpOnly cookies Closes #123 - Keep commits atomic - One logical change per commit
- Rebase before merging - Clean linear history
- Use signed commits - Verify authorship
- Protect main branch - Require reviews and CI
Conclusion#
Git mastery comes from understanding its modelβcommits are snapshots, branches are pointers, and history is malleable. These advanced techniques give you surgical precision over your codebase history.



