← Journal

Advanced Git: Branching Strategies and Collaboration

Master advanced Git techniques including Git Flow, rebasing vs merging, stashing, conflict resolution, and effective team collaboration patterns.

Effective branching strategies help teams collaborate smoothly and maintain a clean project history. This post covers the most common approaches and the scenarios where each shines.

Git Flow Branch Usage

Typical Git Flow Branch Distribution (%)
20
main
30
develop
25
feature
15
release
10
hotfix
Feature and develop branches handle most day-to-day activity.

Setting Up Git Flow

# macOS
brew install git-flow

# Initialise in your repo
git flow init

# Start a feature
git flow feature start new-payment-ui

# Finish feature (merges to develop, deletes branch)
git flow feature finish new-payment-ui

# Create a release
git flow release start 2.0.0

# Finish release (merges to main AND develop, tags it)
git flow release finish 2.0.0

Resolving Merge Conflicts

git merge feature-branch
# Conflict appears in the file:
# <<<<<<< HEAD
# current branch code
# =======
# incoming branch code
# >>>>>>> feature-branch

# 1. Edit the file to keep what you want
# 2. Remove the conflict markers
# 3. Stage and finish the merge
git add conflicted-file.txt
git commit

Stashing Work in Progress

git stash                              # save current changes
git stash list                         # show all stashes
git stash pop                          # apply and remove most recent
git stash save "WIP: payment form"     # named stash
git stash apply stash@{1}             # apply specific stash

Rebase vs Merge

MethodWhen to UseProsCons
MergePublic branches, team collaborationPreserves history, safeCreates merge commits
RebaseLocal branches before pushingClean linear historyRewrites history
# Rebase current branch onto latest main
git checkout feature-branch
git rebase main

# Resolve any conflicts, then:
git rebase --continue

# Interactive rebase — squash / reword the last 3 commits
git rebase -i HEAD~3

Collaboration Best Practices

  • Always git pull --rebase before pushing to keep history linear
  • Use pull requests / merge requests for code review — never push directly to main
  • Protect main with branch protection rules (require 1 review + passing CI)
  • Write a .gitignore before the first commit
  • Use git bisect to quickly identify which commit introduced a bug