Git Notes
Rebasing is one of Git\
Rebasing is one of Git's most powerful yet most misunderstood commands. While merging combines two branches by creating a merge commit, rebasing replays your commits on top of another branch, creating a clean linear history. Understanding when and how to use rebase versus merge is a key skill for professional development workflows.
The Concept Behind Rebasing
Imagine you are writing a book with a co-author. You both started writing from the same outline (a common ancestor commit). Your co-author has written chapters 5 and 6, which are now in the main manuscript. Meanwhile, you wrote chapters 7 and 8 on your own copy. Rebasing is like taking your chapters 7 and 8 and rewriting them as if you had started after chapters 5 and 6 were already done — making it look like you always had the latest version when you began.
How Rebase Works
When you rebase branch A onto branch B, Git:
- Finds the common ancestor of both branches
- Takes the changes introduced by each commit on branch A
- Saves those changes temporarily
- Resets branch A to point to the tip of branch B
- Replays each saved change one at a time on top of branch B
git checkout feature/search
git rebase mainVisually:
Before rebase
main: A---B---C---D
\
feature/search: E---F---G
After rebase
main: A---B---C---D
\
feature/search: E'---F'---G'
Notice that commits E, F, and G become E', F', and G' — they are new commits with different SHA hashes because their parent has changed, even though their content differences are the same.
Basic Rebase Commands
Rebase current branch onto main:
git checkout feature/analytics
git rebase mainRebase onto a specific branch:
git rebase developContinue after resolving conflicts:
git rebase --continueAbort a rebase in progress:
git rebase --abortSkip a conflicting commit:
git rebase --skipInteractive Rebase
Interactive rebase is incredibly powerful for cleaning up your commit history before sharing it with others:
git rebase -i HEAD~4This opens your editor showing the last 4 commits:
You can change pick to various actions:
- pick — keep the commit as-is
- reword — keep the commit but change its message
- squash — combine with the previous commit, keep both messages
- fixup — combine with previous commit, discard this message
- drop — remove the commit entirely
- edit — pause to amend the commit
For example, squashing a typo fix into the original commit:
Rebase vs Merge: When to Use Which
Use rebase when:
- You want a clean, linear project history
- You are working on a feature branch and want to incorporate the latest main changes
- You want to clean up messy commits before a pull request
- Your team convention prefers linear history
Use merge when:
- You want to preserve the complete history of how development happened
- You are combining a completed feature into main (especially via pull request)
- Multiple people are working on the same branch
- You want to clearly see when branches were integrated
The Golden Rule of Rebasing
Never rebase commits that have been pushed to a shared repository and that others may have based work on.
When you rebase, you rewrite commit history. If someone else has based their work on your original commits, rebasing creates a confusing situation where the same changes exist as different commits. This leads to duplicated work and messy history.
# SAFE - rebasing your local, unpushed feature branch
git checkout feature/my-work
git rebase main
# DANGEROUS - rebasing a shared branch
git checkout develop
git rebase main # Others have commits based on develop!Handling Rebase Conflicts
Conflicts during rebase are resolved one commit at a time. Git replays each commit sequentially, so you might need to resolve conflicts multiple times:
$ git rebase main
CONFLICT (content): Merge conflict in src/api.js
error: could not apply a1b2c3d... Add API endpoint
Resolve all conflicts manually, then run:
git rebase --continueAfter fixing the conflict:
git add src/api.js
git rebase --continueIf the same files conflict repeatedly, consider squashing related commits first with interactive rebase to reduce the number of conflict resolution steps.
Practical Workflow: Keeping Feature Branch Updated
The most common rebase workflow is keeping your feature branch up-to-date with main:
# On your feature branch
git fetch origin
git rebase origin/main
# If there are conflicts, resolve them
git add .
git rebase --continue
# Force push to update your remote branch (only if it is your own!)
git push --force-with-lease origin feature/my-workThe --force-with-lease flag is safer than --force because it refuses to push if someone else has pushed commits to that branch since you last fetched.
Rebase for Pull Request Cleanup
Before submitting a pull request, use interactive rebase to present clean, logical commits:
git rebase -i mainCombine work-in-progress commits, fix typo commits, and reword unclear messages. Your reviewers will appreciate a clean, easy-to-review history.
Key Takeaways
Rebasing creates clean, linear history by replaying commits on top of another branch. It is ideal for keeping feature branches updated and cleaning up commits before sharing. However, never rebase shared history that others depend on. Interactive rebase is a powerful editing tool for crafting meaningful commit history. Combined with merge (for integration points), rebase gives you complete control over how your project history reads.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Rebase.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, branching, and, merging, rebase
Related Git & GitHub Topics