4 Hours

5. Advanced Branching: Rebase, Cherry-Pick, and Interactive Rebase

Advanced

Advanced Git commands give you fine-grained control over your repository history. Rebase rewrites history by applying commits on top of another branch. Cherry-pick applies specific commits. Interactive rebase lets you squash, reorder, and edit commits. These powerful tools give you complete control over your commit history. Understanding these commands separates intermediate Git users from experts.

The key insight is that rebase rewrites history — it changes commit hashes. This makes it dangerous on shared branches but powerful for cleaning up local history. Understanding when to rebase vs merge is essential for collaboration. Rebase gives you a clean, linear history. Merge preserves the actual timeline of branches and merges. Both have their place, and knowing which to use when is a mark of a senior engineer.

You'll learn about git rebase, git cherry-pick, and git rebase -i (interactive). Each command is explained with examples, best practices, and when to use each. The interactive visualizer below will show you rebasing in action — watch commits being replayed on top of another branch.

Rebasing: The Alternative to Merging

Rebase applies commits from one branch onto another, rewriting history. Instead of creating a merge commit, it moves the entire branch to the tip of the target branch. This creates a linear, clean history. The trade-off is that rebase rewrites commit hashes. Use rebase for local branches before merging to main. Never rebase shared branches — it rewrites history that others may have based work on. Understanding this rule is essential for collaboration. Rebasing shared branches causes massive conflicts and confusion for your team.

The visualizer above shows the difference between merging and rebasing. Watch the rebase scenario to see commits being replayed on top of the target branch. You'll see new commits created with new hashes, and the branch pointer moving to the tip. This is what rebasing looks like under the hood. Understanding this visual representation makes the abstract concept concrete.

Cherry-Picking Specific Commits

Cherry-pick applies a specific commit from one branch to another. This is useful when you want to bring a single change without merging the entire branch. It's also useful for hotfixes and selective changes. Cherry-pick applies the changes from the commit and creates a new commit with a new hash. The commit message is retained. Cherry-pick is a surgical tool — use it when you need a specific change from another branch.

Cherry-pick is often used for hotfixes. You might have a fix on a development branch that needs to go into production immediately. Instead of merging the entire development branch, you cherry-pick just the fix commit. This gives you surgical control over what goes into production. Understanding cherry-pick gives you precision in managing your history.

Interactive Rebase: Squash, Edit, Reorder

Interactive rebase lets you rewrite history with fine control. You can squash commits (combine them), edit commit messages, reorder commits, and drop commits. This is the most powerful Git tool for cleaning up history before merging. Interactive rebase is essential for creating a clean, readable commit history. It's used before merging features to main. It allows you to create a narrative of your changes that's easy to understand.

A clean commit history is a sign of a professional team. It makes debugging easier, helps with code review, and creates a clear story of how the codebase evolved. Interactive rebase is how you create that clean history. Squash multiple WIP commits into one logical commit. Reword unclear commit messages. Reorder commits to tell a coherent story. These practices make your repository more maintainable.

Step-by-Step: Rebase and Interactive Rebase

Follow these steps to master rebasing and interactive rebase. These are advanced techniques that will make you a Git power user.

Part 1: Basic Rebase
Step 1: Set up a repository with two branches
mkdir rebase-demo
cd rebase-demo
git init
echo <strong><code>Base content > file.txt
git add file.txt
git commit -m <strong><code>Initial commit
git branch feature
git switch feature
echo <strong><code>Feature change 1 >> file.txt
git add file.txt
git commit -m <strong><code>Feature commit 1
echo <strong><code>Feature change 2 >> file.txt
git add file.txt
git commit -m <strong><code>Feature commit 2
git switch main
echo <strong><code>Main change >> file.txt
git add file.txt
git commit -m <strong><code>Main commit

Step 2: Rebase the feature branch onto main
git switch feature
git rebase main
Git rewinds the feature branch, applies the new main commit, then reapplies the feature commits on top. Run git log --oneline --graph to see the linear history.

Part 2: Interactive Rebase
Step 3: Create multiple commits for squashing
echo <strong><code>Work 1 >> file.txt
git add file.txt
git commit -m <strong><code>WIP: work 1
echo <strong><code>Work 2 >> file.txt
git add file.txt
git commit -m <strong><code>WIP: work 2
echo <strong><code>Work 3 >> file.txt
git add file.txt
git commit -m <strong><code>WIP: work 3

Step 4: Squash commits with interactive rebase
git rebase -i HEAD~3
An editor opens showing the last 3 commits. Change pick to squash on the second and third commits. Save and exit. Then edit the commit message to something meaningful like Add complete feature and save. Run git log --oneline to see the three commits squashed into one.

Try It Yourself: Practice cherry-picking a commit from one branch to another. Create a new branch, make a commit, then switch back and use git cherry-pick <commit-hash> to bring that commit over.

Knowledge Check

Ready to test your understanding of 5. Advanced Branching: Rebase, Cherry-Pick, and Interactive Rebase?

Back