Git - Advanced Branching

Advanced

Advanced branching techniques give you precise control over your project history. These powerful tools help maintain clean, linear history and enable sophisticated development workflows.

Understanding Git Rebase

Rebasing moves or combines commits by changing their base. Unlike merging, which preserves history, rebasing rewrites it for a cleaner timeline:

Before rebase:
main:     A---B---C---F
               \
feature:        D---E
After rebase:
main:     A---B---C---F
                       \
feature:                D'---E'  ← Commits moved and rewritten

Basic Rebase Operations

Simple Rebase

# Rebase current branch onto main
git rebase main

# Rebase specific branch
git rebase main feature-branch

# Rebase onto specific commit
git rebase a1b2c3d
Successful rebase output:
Successfully rebased and updated refs/heads/feature-branch.

Rebase with Conflicts

Rebase conflict output:
First, rewinding head to replay your work on top of it...
Applying: Add user authentication
Using index info to reconstruct a base tree...
M       config.js
Falling back to patching base and 3-way merge...
Auto-merging config.js
CONFLICT (content): Merge conflict in config.js
error: Failed to merge in the changes.
Patch failed at 0001 Add user authentication

Resolve all conflicts manually, mark them as resolved with
"git add/rm ", then run "git rebase --continue".
You can instead skip this commit: git rebase --skip
You can abort this rebase: git rebase --abort
Resolving rebase conflicts:
# 1. Check status
git status

# 2. Edit conflicted files (remove conflict markers)
nano config.js

# 3. Stage resolved files
git add config.js

# 4. Continue rebase
git rebase --continue

# Or skip problematic commit
git rebase --skip

# Or abort entire rebase
git rebase --abort

Interactive Rebase

Interactive rebase lets you edit, reorder, squash, or delete commits:

# Edit last 3 commits interactively
git rebase -i HEAD~3

# Interactive rebase from specific commit
git rebase -i a1b2c3d
Interactive rebase editor:
pick a1b2c3d Add login form
pick b2c3d4e Fix validation bug  
pick c3d4e5f Update styling

# Rebase d4e5f6g..c3d4e5f onto d4e5f6g (3 commands)
#
# Commands:
# p, pick  = use commit
# r, reword  = use commit, but edit the commit message
# e, edit  = use commit, but stop for amending
# s, squash  = use commit, but meld into previous commit
# f, fixup  = like "squash", but discard this commit's log message
# x, exec  = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label 

Interactive Rebase Commands

Common rebase actions:
# Squash commits together
pick a1b2c3d Add login form
squash b2c3d4e Fix validation bug  
squash c3d4e5f Update styling

# Reorder commits
pick c3d4e5f Update styling
pick a1b2c3d Add login form
pick b2c3d4e Fix validation bug

# Drop unwanted commits
pick a1b2c3d Add login form
drop b2c3d4e Debug print statements
pick c3d4e5f Update styling

# Edit commit message
reword a1b2c3d Add login form
pick b2c3d4e Fix validation bug

Cherry-Picking Commits

Cherry-pick applies specific commits from other branches:

# Cherry-pick single commit
git cherry-pick a1b2c3d

# Cherry-pick multiple commits
git cherry-pick a1b2c3d b2c3d4e

# Cherry-pick range of commits
git cherry-pick a1b2c3d^..c3d4e5f

# Cherry-pick without committing (review first)
git cherry-pick --no-commit a1b2c3d
Cherry-pick output:
[main d4e5f6g] Add user authentication
 Date: Wed Oct 25 14:30:22 2023 -0700
 2 files changed, 25 insertions(+), 3 deletions(-)

Cherry-Pick Conflicts

# When cherry-pick conflicts
git status  # Shows conflicted files
# Edit conflicts
git add conflicted-file.txt
git cherry-pick --continue

# Or abort cherry-pick
git cherry-pick --abort

Advanced Merge Strategies

Octopus Merge

# Merge multiple branches at once
git merge feature-1 feature-2 feature-3

# This creates an octopus merge with multiple parents

Subtree Merge

# Merge external project as subdirectory
git remote add external-lib https://github.com/user/library.git
git fetch external-lib
git merge -s subtree external-lib/main --allow-unrelated-histories

Ours/Theirs Strategy

# Always use our version in conflicts
git merge -X ours feature-branch

# Always use their version in conflicts  
git merge -X theirs feature-branch

# Completely ignore their changes (keep ours)
git merge -s ours feature-branch

Complex Branching Workflows

Feature Branch with Rebase

Clean feature integration:
# 1. Create feature branch
git switch -c feature/user-profile main

# 2. Work on feature (multiple commits)
git commit -m "Add profile model"
git commit -m "Add profile view"  
git commit -m "Add profile tests"

# 3. Update main branch
git switch main
git pull origin main

# 4. Rebase feature onto updated main
git switch feature/user-profile
git rebase main

# 5. Squash commits for clean history
git rebase -i HEAD~3

# 6. Merge cleanly into main
git switch main
git merge feature/user-profile  # Fast-forward merge

Release Branch Workflow

# Create release branch from develop
git switch -c release/v1.2.0 develop

# Make release-specific changes
git commit -m "Bump version to 1.2.0"
git commit -m "Update changelog"

# Merge to main and tag
git switch main
git merge --no-ff release/v1.2.0
git tag -a v1.2.0 -m "Release version 1.2.0"

# Merge back to develop
git switch develop
git merge --no-ff release/v1.2.0

# Clean up
git branch -d release/v1.2.0

Rebase vs Merge Guidelines

When to Use Rebase

  • ✅ Private feature branches
  • ✅ Cleaning up commit history before sharing
  • ✅ Keeping linear project history
  • ✅ Updating feature branch with main changes

When to Use Merge

  • ✅ Integrating completed features
  • ✅ Preserving development context
  • ✅ Public/shared branches
  • ✅ When history preservation is important
Golden Rule: Never rebase commits that have been pushed to shared repositories unless you coordinate with your entire team.

Advanced Rebase Techniques

Rebase with Exec

# Run tests after each commit during rebase
git rebase -i HEAD~3 --exec "npm test"

# In editor, exec commands are added automatically:
pick a1b2c3d Add feature
exec npm test
pick b2c3d4e Fix bug
exec npm test

Rebase onto Different Base

# Move feature branch from old-base to new-base
git rebase --onto new-base old-base feature-branch

# Example: Move commits that were based on v1.0 to v2.0
git rebase --onto v2.0 v1.0 feature-branch

Preserve Merge Commits

# Rebase while preserving merge structure
git rebase --preserve-merges main

# Or with newer Git (2.18+)
git rebase --rebase-merges main

Troubleshooting Advanced Operations

Fixing Broken Rebase

# If rebase goes wrong
git rebase --abort  # Start over

# If you need to edit earlier in rebase
git rebase --edit-todo  # Modify rebase plan

# Continue after resolving issues
git rebase --continue

Cherry-Pick Multiple Commits

# Cherry-pick commits from different branches
git cherry-pick feature-a~2
git cherry-pick feature-b~1  
git cherry-pick feature-c~3

# Handle conflicts individually
# Then continue with remaining picks

Branch Visualization

# Visualize complex branch structure
git log --graph --oneline --all --decorate

# Show branch relationships
git show-branch

# ASCII art branch visualization
git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' --abbrev-commit --all

Performance Considerations

Large History Rebases

# For large rebases, increase buffer
git config --global rebase.instructionFormat '%s [%an]'

# Show progress during rebase
git config --global rebase.stat true

# Use rerere to remember conflict resolutions
git config --global rerere.enabled true

Best Practices

Safe Rebase Workflow

# 1. Create backup branch
git branch backup-before-rebase

# 2. Perform rebase
git rebase main

# 3. Test thoroughly
npm test

# 4. If successful, delete backup
git branch -d backup-before-rebase

# 5. If failed, recover
git reset --hard backup-before-rebase

Team Communication

  • ✅ Communicate rebase plans to team
  • ✅ Use protected branches to prevent force pushes
  • ✅ Establish team conventions for rebase vs merge
  • ✅ Document branching strategy in README

Summary

You now understand advanced branching:

  • ✅ Rebase moves commits to new base for linear history
  • ✅ Interactive rebase allows editing commit history
  • ✅ Cherry-pick applies specific commits across branches
  • ✅ Advanced merge strategies handle complex scenarios
  • ✅ Choose rebase vs merge based on context
  • ✅ Never rebase shared/public branches
  • ✅ Always backup before destructive operations

Next Steps

Now that you understand advanced branching, let's explore collaboration workflows:

Git - Collaboration Workflows

Practice Tip: Create a test repository with messy commit history and practice cleaning it up with interactive rebase. Understanding these techniques is crucial for maintaining professional project history.