Rebase Practice Project
What you'll build
A repository where main has advanced after your feature branch was created. You will rebase your feature commits on top of the new main, producing a linear history.
Goals
- Create a divergent branch scenario
- Rebase a feature branch onto updated main
- Compare rebase history with merge history
- Understand when rebase is appropriate vs merge
Prerequisites
- git branch, merge, commit (Branch Workflow Simulation)
Steps
- 1
Set up divergent branches
Create a repo, make a commit on main, branch off, then advance both main and the feature branch independently. This creates divergence.
Codemkdir rebase-practice cd rebase-practice git init echo "base" > file.txt git add . git commit -m "Base commit" git checkout -b feature echo "feature change" >> file.txt git commit -am "Add feature change" git checkout main echo "main update" >> file.txt git commit -am "Update on main"Expected result
main and feature each have a commit the other does not.
- 2
View the divergent history
Use git log --graph to see how the branches diverged. The history shows a fork where feature and main went separate ways.
Codegit log --oneline --graph --allExpected result
A graph showing main and feature diverging from the base commit.
- 3
Rebase feature onto main
Switch to feature and rebase onto main. This replays your feature commit on top of main's latest commit, creating a linear history.
Codegit checkout feature git rebase main git log --oneline --graph --allExpected result
The feature commit now sits on top of main's commit — linear history.
Hint
If a conflict occurs, resolve it, git add, and git rebase --continue.
- 4
Compare with a merge approach
Reset the feature branch and try merging instead of rebasing. Compare the two histories to see the difference: merge creates a merge commit, rebase does not.
Code# Reset feature to pre-rebase state (for comparison) git reset --hard ORIG_HEAD # Merge instead git checkout main git merge feature --no-ff git log --oneline --graph --all # Note the merge commit that rebase would not have createdExpected result
Merge history shows a merge commit; rebase history is linear.
- 5
Verify repository state
Use git status and git log to confirm the repository is clean and the commit history matches expectations. Before any destructive operation, checking the current state is a Git best practice.
Codegit status # Should show: nothing to commit, working tree clean git log --oneline -5 # Review the last 5 commits to confirm history git branch -v # List branches with last commit infoExpected result
git status shows a clean working tree; git log shows the expected commit history.
Try it yourself
What you learned
You practiced rebasing a feature branch onto updated main and compared the linear history with merge's merge-commit history. Rebase gives cleaner history but rewrites commits — use it for local branches, merge for shared branches.
Related Resources
Related Lessons
- git rebase
Learn rebase, conflicts, and abort.
Related Cheatsheets
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.