Mastering Git Workflow: Beyond the Basics

Git branch visualization with colorful nodes

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:

Text
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:

Text
main        ─●─●─●─●─●─●─●─●─●─●─●─●─●
               ↑   ↑   ↑   ↑
features    ───●───●───●───●

Best for: CI/CD, small teams, rapid iteration

GitHub Flow#

Simplified model for continuous deployment:

Text
main        ─────●─────●─────●─────●
                 ↑     ↑     ↑
feature     ─────●─────│     β”‚
bugfix      ───────────●─────│
enhancement ─────────────────●

Interactive Rebase#

Rewriting history for cleaner commits:

Bash
# Rebase last 5 commits
git rebase -i HEAD~5

Options in interactive mode:

  • pick - Keep commit as-is
  • reword - Change commit message
  • edit - Pause to amend commit
  • squash - Combine with previous commit
  • fixup - Like squash, discard message
  • drop - Remove commit

Squashing Commits#

Before:

Text
abc1234 WIP
def5678 More WIP
ghi9012 Fix typo
jkl3456 Actually working now

After squash:

Text
mno7890 Implement user authentication

Advanced Stashing#

Bash
# 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:

Bash
# 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:

Bash
# 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:

Bash
# 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:

Bash
# .git/hooks/pre-commit
#!/bin/sh
npm run lint
npm run test

Common hooks:

  • pre-commit - Run before commit
  • commit-msg - Validate commit message
  • pre-push - Run before push
  • post-merge - Run after merge

Worktrees#

Work on multiple branches simultaneously:

Bash
# Create worktree
git worktree add ../project-feature feature-branch

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../project-feature

Best Practices#

  1. Write meaningful commit messages
    Text
    feat(auth): add OAuth2 login flow
    
    - Implement Google OAuth provider
    - Add token refresh mechanism
    - Store tokens securely in httpOnly cookies
    
    Closes #123
    
  2. Keep commits atomic - One logical change per commit
  3. Rebase before merging - Clean linear history
  4. Use signed commits - Verify authorship
  5. 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.

Share:

Related Articles