Git Rebase vs Merge: When to Use Which, With History Diagrams and Team Rules

Short answer
Git rebase vs merge: which one should you use?
In the git rebase vs merge choice, the answer depends on who owns the branch. Use rebase to clean up commits that live only on your machine. Use merge to combine branches you already pushed and others build on. The official Git book sums it up: rebase local changes before pushing, but never rebase anything you have pushed.
The git rebase vs merge question starts a debate on almost every team sooner or later. Here is the short answer: both move changes from one branch to another, and the difference lies in how history looks afterward. Merge records what happened as it happened, while rebase rewrites your commits onto a new base to produce a straight line. This guide compares both with history diagrams drawn in words, then covers the risk of rebasing shared branches, interactive rebase, and a practical rule set for your team.
Also, the rule is easy to memorize, but understanding why it works matters more. Most team arguments come from not seeing clearly what each command does to history. So the sections below first explain each command, then show when each one fits.
For a quick decision, ask three questions:
- Did anyone else pull this branch or commit on top of it? If yes, merge.
- Do your commits exist only locally? If yes, rebase is safe.
- Does your team want a straight history or a faithful record? That answer sets how you integrate into the main branch.
What does git merge actually do?
Merge performs a three way merge of the two branch tips and their common ancestor, then records the result as a new merge commit. The rebasing chapter of the official Git book describes it as taking the endpoints of two lines of work and merging them together.
The key property of merge is that it leaves existing commits alone. In other words, every commit hash on everyone's machine stays the same. That makes merge safe on shared branches, because it never rewrites anyone's history.
In addition, fast forward is a special case. According to the official git merge documentation, when the current branch is an ancestor of the commit you merge, no new commit is needed; Git just moves the branch pointer forward. History stays straight and no merge commit appears.
The cost of merge is a history full of lines that branch off and join again. On a small team this may not matter. However, on a project where dozens of branches land every day, reading the log can get hard.
What does git rebase actually do?
Rebase takes the change each commit on your branch introduced and replays those changes, in order, on top of another base. The official book explains it as replaying changes from one line of work onto another in the order they were introduced.
The critical point: rebase does not move your old commits. Instead, it creates new commits that look similar but are different. Even when the content matches, the commit hashes change, and Git leaves the old commits behind.
As a result, history looks like one straight line, as if everyone finished their work in sequence. The official book puts it as making work look serial even when it happened in parallel. That makes the log easier to read and makes it easier to find where a bug came in.
However, the hash change is the source of every rebase risk. If someone built work on the old commits, your new hashes no longer match their history. We cover that problem in its own section below.
What does history look like in the same scenario?
To make the difference concrete, let's finish one scenario two ways. The main branch has commits A, B and C. You branched off at B and added commits D and E on a feature branch. Meanwhile, a teammate added commit F to main. So main now reads A, B, C, F, while your branch reads A, B, D, E.
If you finish with a merge, you switch to main and merge the feature branch. Git creates a new commit M that joins F and E. In the log you see two parallel lines: one runs through C and F, the other through D and E, and both meet at M. The hashes of D and E do not change.
If you finish with a rebase, you first rebase the feature branch onto main. Git replays the changes from D and E on top of F and creates new commits D' and E'. Then, when you switch to main and merge, Git fast forwards. The log is one line: A, B, C, F, D', E'.
In both cases the final file content is the same. The official book also notes that both methods produce the same final snapshot and differ only in history. Therefore the debate is not about code correctness; it is about readability and safety of history.
Git rebase vs merge: comparison table
The table below puts the criteria teams ask about most side by side. When you weigh git rebase vs merge for your project, rank these rows by your own priorities.
| Criterion | git merge | git rebase |
|---|---|---|
| History shape | Lines that branch and rejoin | Straight, linear line |
| Existing commit hashes | Unchanged | Changed, new commits appear |
| Safety on shared branches | Safe | Risky, can disrupt team history |
| Conflict resolution | Once, at merge time | Possibly once per commit |
| Pushing to the remote | Normal push works | Needs a force push if pushed before |
| Searching and reviewing history | Merge commits can add noise | Linear history is easier to follow |
| Undoing | Revert the merge commit | Possible through reflog and ORIG_HEAD |
The takeaway is clear: the two are not rivals; they are tools for different stages. Rebase for local cleanup, merge for shared integration.
Why is rebasing a shared branch dangerous?
The official Git book states a rule in bold: do not rebase commits that exist outside your repository and that people may have based work on. The rule is simple, yet breaking it affects the whole team.
For example, here is how it unfolds. You pushed a branch, a teammate pulled it and committed on top. Then you rebased that branch and force pushed. Now the remote holds copies with new hashes in place of the old commits. When your teammate pulls, Git sees their old commits and your new ones as different work.
According to the official book, your teammate then has to merge their work again, and history ends up messy, with the same changes twice. Worse, if they push the old commits again, commits you removed on purpose can come back.
So the safest team principle is this: never rebase common branches such as main or develop. Rebase your personal feature branch only when you are sure nobody else works on it.
What should you do if someone rebased a shared branch?
Still, if the rule got broken, don't panic. The official book explains that Git computes a patch id from the change each commit introduces, so it can often recognize rewritten commits. In other words, if you rebase your own work onto the new history, Git often figures out what is uniquely yours and replays only that.
In practice, after fetching the remote branch, you rebase your branch onto the new remote branch. Alternatively, you use git pull --rebase, which does it in one step. The official book recommends that everyone run this command in that situation.
The official git rebase documentation covers this under recovering from an upstream rebase and splits it into two cases. In the easy case, the changes are literally the same and Git skips the duplicates. In the hard case, the changes differ because of conflict resolution or edits; then you must tell Git explicitly where the old upstream ended.
The documentation also warns that using the easy case command in the hard case can resurrect commits that upstream removed on purpose. So if you are unsure, create a temporary backup branch first.
If you must force push, what is --force-with-lease?
If you rebased your personal branch and you had pushed it before, a normal push fails, because the new history is not a continuation of the old one. At that point you need a force push. However, plain --force overwrites whatever sits on the remote branch at that moment.
According to the official git push documentation, --force-with-lease allows the push only if the remote ref still has the value you expect, and rejects it otherwise. The docs describe it as taking a lease on the ref without explicitly locking it. So if someone committed to the branch while you rebased, the push fails and you don't overwrite their work.
That said, the documentation adds an important warning: if a background process runs automatic fetches, the protection weakens, because Git treats remote tracking refs as what you have seen. To reduce that risk, the docs suggest adding --force-if-includes as well.
In short, when your own branch needs a force push, make these two options your habit instead of plain force.
When do fast forward, --no-ff, --ff-only and --squash make sense?
Also, merge is not a single behavior; options let you shape how history looks. Based on the definitions in the official git merge documentation, the options work like this:
--ffis the default; it fast forwards when possible and creates a merge commit when not.--no-ffalways creates a merge commit, even when a fast forward would work. Teams that want to see a feature's commits as a group in the log prefer it.--ff-onlymerges only when a fast forward is possible; otherwise it refuses and exits with an error. It is a safe lock for teams that require straight history.--squashprepares the merged result in your working tree without committing; you then create a single commit. The branch's whole history collapses into one commit.
For example, "rebase, then --ff-only" produces fully linear history on the main branch and prevents accidental merge commits. In contrast, "--no-ff" preserves feature boundaries. Squash is handy for small branches with messy commits; just know that you lose the intermediate steps.
git pull or git pull --rebase?
Plain git pull fetches remote changes and merges them into your local branch. If both you and the remote moved forward on the same branch, every pull can create a small merge commit. Over time the log fills up with meaningless "merged remote branch" commits.
In contrast, git pull --rebase fetches remote changes and replays the commits you have not pushed yet on top of them. The official book presents this command as a way to avoid merge clutter and notes that git config --global pull.rebase true makes it the default.
This use does not break the "don't rebase shared work" rule. The only commits it rewrites are local ones you have not pushed yet. Remote commits stay untouched.
That is why many teams use rebase for pulls in daily work and merge for bringing feature branches into the main branch. It is a practical balance that combines the good sides of both.
How do you clean up history with interactive rebase?
Interactive rebase is the most powerful way to edit your own commits before you push. The git rebase -i command opens an editor with one line per commit. By changing the command at the start of each line, you choose what happens to that commit. The official documentation lists these commands:
pickkeeps the commit as is; this is the default.rewordedits only the commit message.editstops after applying the commit, so you can change files or the commit, then continue.squashfolds the commit into the previous one and combines the messages.fixupfolds it in like squash but discards this commit's message.dropremoves the commit; deleting the line has the same effect.
The documentation also describes --autosquash. Commits whose messages start with "fixup!" or "squash!" move automatically right after their target commit. The recommended way to create them is git commit --fixup. That way you commit small fixes during the day and get clean history with one command before you push.
Step by step: keeping a feature branch current with main
If you work on a personal feature branch and main has moved ahead, follow this flow to keep your branch current. It fits only when you work on the branch alone.
- Run
git statusand confirm your working tree is clean. - Fetch remote changes with
git fetch origin. - On your feature branch, run
git rebase origin/main. - If a conflict appears, fix the files, stage them with
git add, and rungit rebase --continue. - If things get messy, return to the starting point with
git rebase --abort. - Run your tests, because a rebase can leave code that no longer builds.
- If you pushed the branch before, push with
git push --force-with-lease.
Above all, don't skip step six. A rebase applies every commit to a new base, so even without conflicts, a change on main can contradict your code logically. Tests catch that silent break.
Also, the more often you rebase, the smaller each conflict gets. Waiting for weeks and updating in one go makes conflicts bigger.
Step by step: bringing a feature branch in with merge
When you integrate a shared branch, or when your team wants merge commits as a record, follow the merge flow:
- Switch to the main branch with
git switch main. - Update it with
git pull. - Merge with
git merge --no-ff feature-branch; drop --no-ff if you don't need the feature boundary. - If a conflict appears, resolve it, stage the files and complete the merge commit.
- Run the tests, then push with
git push.
Most teams do this step not in the terminal but in the pull request screen of their code hosting platform. That screen usually offers merge, squash and rebase options; which ones appear depends on repository settings. Option names vary by platform, so check your platform's own documentation.
If you hit a conflict during the merge, our guide on how to resolve git merge conflicts walks through every step, from reading conflict markers to verifying the fix.
How does conflict resolution differ between rebase and merge?
With merge, you resolve conflicts once, at merge time. Git compares the final state of both branches and shows every conflicting file in one go. When you finish, Git creates the merge commit.
With rebase, Git applies your commits one by one, so conflicts can appear separately in several commits. For instance, if you changed the same file across five commits, you may resolve conflicts in that file five times. That is the most tiring side of rebase.
On the other hand, rebase conflicts are smaller and their context is clearer, because each time you look at the change from a single commit. Some developers find rebase conflicts easier to understand for that reason.
While resolving conflicts during a rebase, you will notice that HEAD is temporarily not on a branch. That is normal; for details, see our article on git detached head. If the same conflicts keep coming back, Git's rerere feature, which remembers resolutions, is worth a look.
How do you undo a bad rebase?
You finished a rebase and dislike the result? Your commits are not gone. According to the official git rebase documentation, ORIG_HEAD points at the tip of the branch being rebased when the rebase starts. However, the docs note that if commands such as reset run during the rebase, ORIG_HEAD may no longer point there at the end.
The reflog is the more reliable route. The documentation says the previous tip stays reachable through the branch's reflog. Follow these steps:
- Run
git reflogand find the line just before the rebase began. - Copy the hash from that line.
- Preserve the current state first with a backup branch:
git branch backup. - Then move your branch back to the old hash; if you use reset for that, make sure you have no uncommitted changes.
If the rebase is still running, it is even easier: git rebase --abort cancels it and resets HEAD to the original branch. In short, you can undo a rebase, and the habit of creating a backup branch keeps the undo from causing a new loss.
What rule should your team adopt?
The official Git book admits the answer depends on your team and project. One view treats history as a record of what actually happened and resists changing it. The other treats history as the story of how the project was made and favors editing it before publishing. At Talha Aslan and team, we suggest this rule set as a starting point for most teams:
- Nobody rebases or force pushes main or other common branches.
- You may clean personal feature branches with interactive rebase before pushing.
- Everyone uses
git pull --rebasefor daily updates. - Once you share a feature branch with someone, you stop rebasing it.
- When a force push is unavoidable, you use
--force-with-leaseonly. - You write the integration method for the main branch down in one place: --no-ff, squash, or rebase followed by --ff-only.
Put the rule set in a contributing guide inside the repository and back it with branch protection settings on your code hosting platform. Open source projects usually keep such rules in their contribution docs; our beginner guide to contributing to open source explains how to read them.
How do you decide on small projects?
When you work alone or with one other person, the debate gets much simpler. The shared history risk is close to zero, because you use most branches yourself. In that case readability usually wins, and keeping a straight history with rebase makes life easier.
Still, building a few habits early lowers the cost of change when the project grows. For example, open short lived branches instead of committing straight to the main branch. Also, run tests before each integration and keep commit messages meaningful.
Once the project opens to a second or third person, it is time to write the rules down. At that point, start from the team rule set above and adapt it to your needs. That way you never enter the "everyone does their own thing" phase.
In short, rebase brings comfort on a small project, and written rules bring calm on a growing one. Thinking about both together is the healthiest approach.
Example scenario: tangled history on a three person team
This is an example scenario, not a real team. Three developers work on the same feature branch. One of them rebases the branch onto main and force pushes. The next morning, the other two pull and face a messy merge that contains two copies of the same commits.
First, the team decides to freeze the situation: nobody pushes anything new. Each developer creates a backup branch locally. Then the two affected developers rebase their unpushed commits onto the new remote branch; Git recognizes the rewritten commits by patch id, skips them, and replays only their own work.
Finally, the team adds two lines to its contributing guide: nobody rebases a shared branch, and everyone uses merge to integrate into main. In short, the team gets through without lasting damage, but the real gain is a written rule that stops the same mistake from happening again.
What are the most common mistakes?
In the git rebase vs merge debate, most mistakes come from using a command in the wrong context, not from the command itself. Avoid these:
- Rebasing a common branch such as main and force pushing it.
- Rewriting a feature branch someone else pulled, without telling them.
- Using plain
--forceand overwriting a teammate's new commit. - Pushing after a rebase without running the tests.
- Switching branches mid rebase to escape a conflict.
- Squash merging and later needing the branch's intermediate steps.
- Skipping updates for weeks, then fighting one huge conflict.
Let's add one security mistake: removing a commit from history with rebase does not erase a secret you already pushed to the remote. In that case, follow the steps in our guide on what to do when an .env file gets committed to Git, and revoke the keys first.
What do --onto and --update-refs do for stacked branches?
Sometimes you build one feature branch on top of another. Once the first branch lands in main, moving the second one to the right base gets tricky. The --onto option in the official git rebase documentation exists for exactly this: it lets you set the starting point for the new commits explicitly.
In the documentation's example, a branch built on top of another branch moves directly onto main with --onto. As a result, the commits from the middle branch stay where they are, and only the commits that belong to your branch land on the new base.
The documentation also describes --update-refs. This option automatically updates other branches that point at the commits being rebased. It helps when you want to realign several stacked branches with a single rebase; however, according to the docs, branches checked out in another worktree are not updated.
Put simply, these options are advanced. Before you use them, create a backup branch and verify the result in the log. To refresh the basics, see our Git and GitHub guide to essential commands.
Does your git rebase vs merge choice affect CI/CD?
Yes, indirectly. On a team that merges, each merge commit can trigger a new build on the main branch, and history shows when each feature landed through those merge commits. On a team that rebases and fast forwards, several commits land on main at once, and each one should build.
So teams that prefer rebase take extra care to keep intermediate commits testable. Otherwise, a binary search through history stalls on commits that don't build. Teams that squash avoid that problem but lose the detail of the intermediate steps.
You will find the core concepts of pipelines in our article on what CI/CD is. In projects with submodules, the integration method also affects submodule references; our git submodule guide covers that.
If authentication fails when you push, our article on the SSH permission denied (publickey) error solves it step by step.



