Advanced Branch Operations

Learn advanced techniques for managing branches and commits in Git

Learning Objectives

  • Master cherry-picking commits between branches
  • Learn interactive rebasing and commit squashing
  • Understand advanced branch management techniques
  • Handle complex branch scenarios effectively

Cherry Picking

Cherry picking allows you to apply specific commits from one branch to another. It's useful when you want to selectively apply changes without merging entire branches.

Basic Cherry Pick

Cherry Pick Commands
# Cherry pick a single commit
$ git cherry-pick commit-hash
# Cherry pick multiple commits
$ git cherry-pick commit1..commit3
# Cherry pick without committing
$ git cherry-pick -n commit-hash

Handling Cherry Pick Conflicts

Conflict Resolution
# When conflicts occur
$ git cherry-pick commit-hash
# Fix conflicts...
$ git add resolved-files
$ git cherry-pick --continue
# Abort cherry pick
$ git cherry-pick --abort

Rebasing Operations

Rebasing rewrites branch history by moving commits to a new base. It can create a cleaner project history but should be used with caution on shared branches.

Basic Rebase

Basic Rebase
# Rebase current branch onto main
$ git checkout feature-branch
$ git rebase main
# Pull with rebase
$ git pull --rebase origin main
# Abort rebase
$ git rebase --abort

Warning: Rebase with Care

Never rebase commits that have been pushed to public branches. This can cause problems for other developers by rewriting shared history.

Interactive Rebasing

Interactive rebasing gives you full control over your commit history, allowing you to modify, reorder, combine, or drop commits.

Interactive Rebase Commands

Interactive Rebase
# Start interactive rebase
$ git rebase -i HEAD~3
# Available commands in rebase:
# 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 message
# d, drop = remove commit

Common Interactive Operations

Common Operations
# Squash last 3 commits
$ git rebase -i HEAD~3
# Change 'pick' to 'squash' for commits to combine
# Edit commit message
$ git rebase -i HEAD~2
# Change 'pick' to 'reword'
# Delete a commit
$ git rebase -i HEAD~4
# Change 'pick' to 'drop'

Advanced Branch Management

Advanced branch management involves techniques for handling complex branch scenarios and maintaining clean history.

Branch Cleanup

Branch Cleanup
# Delete merged branches
$ git branch --merged | grep -v "\*" | xargs git branch -d
# Prune remote tracking branches
$ git remote prune origin
# List branches by commit date
$ git branch --sort=-committerdate

Branch Comparison

Branch Comparison
# Compare branches
$ git log branch1..branch2
# Show branch differences
$ git diff branch1...branch2
# List unique commits
$ git cherry -v main feature-branch

Common Advanced Scenarios

Here are some common scenarios where these advanced techniques are useful:

Cleaning Feature Branch History

Clean History
# Update feature branch
$ git checkout feature-branch
$ git fetch origin
$ git rebase origin/main
# Squash commits
$ git rebase -i HEAD~5
# Force push updated branch
$ git push --force-with-lease origin feature-branch

Backporting Fixes

Backporting
# Cherry pick fix to maintenance branch
$ git checkout maintenance-1.0
$ git cherry-pick bug-fix-commit
# Apply to multiple versions
$ git checkout maintenance-2.0
$ git cherry-pick bug-fix-commit

Advanced Branch Operation Best Practices

  • Back up branches:

    Create backup branches before destructive operations like rebasing

  • Communicate changes:

    Inform team members when rewriting shared history

  • Use force-with-lease:

    Prefer --force-with-lease over --force for safer force pushes

What's Next?

Now that you understand advanced branch operations, in the next lesson you'll learn about:

  • Working with tags and releases
  • Managing release versions
  • Version numbering conventions and standards