Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Git & GitHub›Rebasing — Rebase vs Merge

Branching and Merging

Rebasing — Rebase vs Merge

Rebasing and merging are the two primary methods Git provides to integrate changes from one branch into another. Understanding their distinct impact on project history is critical for maintaining a clean, readable, and debuggable codebase over time. Choosing the right tool depends on whether you prioritize capturing the exact timing of work or creating a clean, linear project progression.

The Anatomy of a Merge

A merge commit effectively records the history of two distinct branches by creating a new 'merge commit' that possesses two parent commits. When you execute a merge, Git combines the diverged history of your feature branch with the target branch, keeping every commit chronologically intact as they occurred. This is the non-destructive way to integrate changes because it does not alter the hash of any existing commits in your history. By keeping the merge commit, you preserve the precise context of when and why branches joined, which is invaluable for auditors or developers tracking exactly how a feature was integrated. However, as projects scale, constant merging creates a 'railroad track' graph that can become increasingly difficult to read and navigate, often obscuring the actual sequence of development in favor of capturing every branch existence.

# Start on the feature branch
git checkout feature-branch
# Merge main into feature to include latest updates
git merge main
# The history now shows a new merge commit connecting the two histories

Understanding Rebase Mechanics

Rebasing serves as a mechanism to rewrite project history by literally picking your local commits and re-applying them onto the tip of another branch. Instead of generating a merge commit, Git takes each commit from your current branch, calculates the patch, and creates a brand-new commit with a new hash on top of the target branch. The fundamental reason this works is that it linearizes history, making it look as if you started your work from the very latest state of the codebase. By moving the base of your work, you eliminate the need for merge commits entirely, resulting in a perfectly straight, readable timeline. However, you must understand that because rebase creates new commits, it effectively discards the originals; you should never rebase commits that have already been pushed to a shared repository.

# Move the current branch work to the latest main
git checkout feature-branch
git rebase main
# History is now linear, as if changes were made sequentially

When History Matters: The Merge Approach

Merging is the preferred choice when the exact provenance of a feature is critical to the team's workflow. Because merging does not modify the commit hashes, it is inherently safer and provides a reliable audit trail of team collaboration. If you are working on a long-running feature branch that is shared with other developers, merging is essential because it avoids rewriting history that others rely on. If you were to rebase shared history, you would force your colleagues to reconcile their local copies with your rewritten commits, leading to chaotic conflicts and broken syncs. Use merging when you want to highlight that a feature was developed in isolation and integrated at a specific point in time, ensuring that the team sees the branch context as part of the formal project history documentation.

# Merge with no-ff flag to force a merge commit even if possible to fast-forward
git checkout main
git merge --no-ff feature-branch
# This ensures the merge event is explicitly visible in history

When Cleanliness Matters: The Rebase Approach

Rebasing is best utilized when you are polishing a local, private feature branch before finally merging it into the primary codebase. By rebasing, you can clean up intermediate 'work-in-progress' commits into a logical series of changes before they are ever seen by the rest of the team. This process makes the project history significantly easier to traverse using tools like binary search, as each commit is guaranteed to be a functional, logical unit derived from the most recent upstream state. You reach for rebase when you value a narrative-style history over the preservation of the branch structure. It is a powerful tool for maintaining professional standards, allowing developers to present a streamlined, coherent history that demonstrates a clear, incremental evolution of the software project without the noise of accidental merge commits.

# Interactive rebase to clean up local commits before pushing
git rebase -i HEAD~3
# Use 'squash' or 'fixup' in the interactive editor to combine messy commits
# This creates a polished, linear history before integrating

Conflict Resolution During Rebase

One of the most intimidating aspects of rebasing for beginners is handling conflicts. Because rebase applies changes one by one, if a commit you are moving creates a conflict with the target branch, Git will pause and ask you to fix it before continuing. The reasoning here is that each commit in the new sequence must be valid against the state of the codebase at that point. Once you fix the conflict, you do not commit normally; instead, you add the files and execute the continue command. This iterative process ensures that every single commit in your new history is clean and free of conflicts, which is a higher standard than simple merging, where one big conflict is resolved once at the final point of integration. This granular control is why experienced developers favor rebase for high-quality, maintainable code contributions.

# If a conflict occurs during rebase:
git add corrected_file.py
# Continue the rebase process after fixing the conflict
git rebase --continue
# If you get stuck, you can always abort the entire rebase
git rebase --abort

Key points

  • Merging preserves the complete, non-linear history of how and when branches were integrated into the project.
  • Rebasing rewrites the project history by creating new commits that reflect a linear progression of development.
  • The primary risk of rebasing is modifying commits that have already been shared, which causes significant synchronization issues.
  • Merge commits provide a clear audit trail and preserve the original context of how a feature was developed.
  • Rebasing is highly effective for cleaning up local development history to ensure every commit is logical and functional.
  • Conflict resolution in a rebase occurs iteratively, ensuring that each individual commit in the sequence is verified.
  • Use merging for shared branches where preserving the collaborative context is prioritized over a clean linear graph.
  • Use rebasing for private branches to ensure your eventual contribution to the main branch is polished and easy to follow.

Common mistakes

  • Mistake: Rebasing branches that have already been pushed to a shared remote repository. Why it's wrong: It rewrites commit history, causing severe synchronization conflicts for other team members. Fix: Use rebase only for local, unpushed branches, or use git merge for shared branches.
  • Mistake: Forgetting to resolve conflicts during a rebase session. Why it's wrong: Leaving conflict markers in code breaks the build and corrupts the repository history. Fix: After 'git add' on resolved files, use 'git rebase --continue' instead of 'git commit'.
  • Mistake: Using rebase to 'clean up' history that others are currently working on. Why it's wrong: It creates divergence between your local history and the remote version. Fix: Only rebase your private feature branch against the main branch to keep it up to date.
  • Mistake: Aborting a rebase incorrectly. Why it's wrong: Running commands haphazardly after a failed rebase can lose work or leave the repository in a 'rebasing' state indefinitely. Fix: Use 'git rebase --abort' to return to the state before the rebase started.
  • Mistake: Assuming rebase and merge are interchangeable. Why it's wrong: Merge preserves the complete historical timeline and branch context, while rebase creates a linear, flattened history. Fix: Choose merge for tracking full context and rebase for maintaining a clean, readable project log.

Interview questions

Can you explain what a Git merge does in the context of integrating changes from one branch into another?

A Git merge takes the contents of a source branch and integrates them into the target branch by creating a new 'merge commit.' This process preserves the complete history of both branches, including every individual commit and the exact timing of when they occurred. It is a non-destructive way to unify work because it explicitly shows when lines of development diverged and when they were joined back together in the repository history.

What is the primary function of a Git rebase, and how does it differ from a standard merge?

A Git rebase works by moving or combining a sequence of commits to a new base commit. Instead of creating a merge commit, Git literally rewrites the project history by creating brand new commits for each commit in the original branch. This results in a perfectly linear project history. You essentially tell Git: 'Take my work from this branch and replay it as if I had started my work from the latest version of the main branch.'

When would you prefer to use a rebase over a merge in your Git workflow?

You should prefer rebasing when you want to maintain a clean, linear project history. It is highly effective for cleaning up local, unpushed feature branches before merging them into a shared repository. By rebasing, you avoid 'noise' in the commit logs caused by frequent merge commits. However, you must only rebase local branches that have not been shared with other team members, as rewriting shared history can cause significant synchronization issues for your collaborators.

Compare and contrast the 'Merge' and 'Rebase' approaches regarding how they handle commit history.

The fundamental difference lies in how they track evolution. A 'Merge' is a history-preserving operation; it captures the entire timeline, including the side-branches, which is excellent for auditability. Conversely, 'Rebase' is a history-rewriting operation; it flattens the graph into a single line. A merge provides context on when features were integrated, while a rebase provides a simplified, chronological view of the project that is often much easier for developers to traverse and understand during debugging sessions.

What are the risks associated with rebasing a branch that has already been pushed to a remote repository?

The biggest risk of rebasing public, shared branches is the destruction of history. Since rebase creates entirely new commit hashes for existing work, it diverges the branch from the remote version. If you push a rebased branch, your teammates' local history will conflict with your new, rewritten history. To fix this, they would have to perform complex manual synchronization. As a rule, never rebase code that others are actively building upon, as it disrupts the shared 'source of truth' for the project.

Explain the concept of an 'Interactive Rebase' and why it is a powerful tool for repository maintenance.

An interactive rebase, triggered by 'git rebase -i', allows you to manipulate commits before they are finalized in the history. You can use this to squash multiple 'work-in-progress' commits into a single clean commit, reorder commits, or even edit commit messages. This is powerful because it allows you to curate a high-quality, readable commit history before merging your feature branch. Instead of a messy log like 'fixed typo,' 'fixed another typo,' and 'oops,' you end up with one clean, professional commit: 'Refactored authentication logic.'

All Git & GitHub interview questions →

Check yourself

1. What is the primary structural difference between a git merge and a git rebase?

  • A.Merge creates a new commit that combines histories, while rebase rewrites commits to create a linear path.
  • B.Rebase creates a merge commit, while merge prevents the creation of any extra commits.
  • C.Merge deletes the original branch history, while rebase preserves it indefinitely.
  • D.Rebase is used for remote repositories, while merge is strictly for local usage.
Show answer

A. Merge creates a new commit that combines histories, while rebase rewrites commits to create a linear path.
The first option is correct because merge produces a 'merge commit' linking two histories, while rebase moves the feature branch base to the latest main commit. Option 1 is backward; 2 is false as rebase is dangerous on shared branches; 3 is incorrect as both preserve history differently.

2. When is it considered a 'best practice' to perform a rebase on your local feature branch?

  • A.Every time you want to pull the latest changes from the main branch to keep your feature branch current.
  • B.When you have finished a large project and want to hide all individual commits into one single commit.
  • C.Immediately after someone else has pushed changes to the same branch you are currently working on.
  • D.Whenever you want to delete your branch and move the code to the main branch history.
Show answer

A. Every time you want to pull the latest changes from the main branch to keep your feature branch current.
Rebasing your local feature branch onto the updated main branch keeps your history clean and linear before merging. Option 1 is better handled by 'squash', 2 causes chaos for others, and 3 is not the purpose of rebasing.

3. If you are in the middle of a rebase and encounter a merge conflict, which sequence of commands is correct to resolve it?

  • A.Edit files, git add, git commit.
  • B.Edit files, git add, git rebase --continue.
  • C.Edit files, git commit, git rebase --continue.
  • D.Edit files, git rebase --continue, git add.
Show answer

B. Edit files, git add, git rebase --continue.
During a rebase, 'git commit' is replaced by 'git rebase --continue' to allow Git to resume the process. You must add the files first to mark them as resolved. Option 0 creates a separate commit, and 2/3 are syntactically incorrect for the rebase flow.

4. Why is it dangerous to rebase a branch that is being used by multiple developers?

  • A.Because it makes the branch impossible to merge later.
  • B.Because rebase changes the commit hashes, creating a 'diverged' history for other users who base their work on the original commits.
  • C.Because the branch will automatically be deleted by the server.
  • D.Because Git requires a paid license to perform a rebase on shared branches.
Show answer

B. Because rebase changes the commit hashes, creating a 'diverged' history for other users who base their work on the original commits.
Rebasing changes the underlying commit ID (hash), making local versions of that branch for others incompatible with the 'new' version. Option 0 is false; 2 is nonsense; 3 is false as Git is open source.

5. Which scenario best justifies using 'git merge' instead of 'git rebase'?

  • A.When you want to maintain the exact history of when and how features were integrated into the project.
  • B.When you want to make your commit log look like a single straight line.
  • C.When you have a very large number of tiny, messy 'wip' commits that you want to hide.
  • D.When you are working completely alone on a repository.
Show answer

A. When you want to maintain the exact history of when and how features were integrated into the project.
Merge is preferred for preserving the historical timeline of branches. Rebasing (Option 1) is specifically used for linear histories. Option 2 describes interactive squashing, and 3 is a valid use case for rebase, not merge.

Take the full Git & GitHub quiz →

← PreviousMerging — Fast-forward vs Three-wayNext →Cherry-picking Commits

Git & GitHub

20 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app