Sub topic 14
Merge or Rebase: Choosing and Avoiding Pitfalls
This chapter assumes you can create and switch branches, run a basic git merge and git rebase, and push to a remote with git push -u. It covers how the two commands differ in what they do to history, when each is the better choice, and which mistakes cause lost or duplicated work.
What each command does to history
Both commands combine work from two branches, but they change history differently.
git mergekeeps every existing commit as it is. If the branches have diverged, it adds one new merge commit with two parents.git rebasecopies your commits onto a new base. The copies have new SHAs, because a commit's SHA depends on its parent. The original commits are no longer reachable from any branch, though the reflog still records them.
| Row | Merge | Rebase |
|---|---|---|
| Existing commits | Unchanged | Replaced by copies |
| Resulting shape | Branches and merge commits | Straight line |
| Conflicts | Resolved once, in one merge commit | Possibly resolved once per replayed commit |
| Safe on shared branches | Yes | No, unless everyone agrees |
| Push afterward | Normal push | Force push needed if already pushed |
The rule: do not rewrite shared history
Rebasing a branch that others have pulled causes trouble. Their local copies still contain the old commits. When they pull, Git sees two histories that contain the same changes under different SHAs. A merge then brings back the old commits next to the new copies, and you get duplicate changes and confusing conflicts.
Rewrite only commits that exist on your machine or on a branch you alone use. Treat main, release branches, and any branch a teammate has based work on as shared.
Worked example: bringing a feature branch up to date
You have been working on feature/login while main moved ahead. You have two ways to catch up.
Option 1, merge main into your branch:
git switch feature/login
git fetch origin
git merge origin/mainThis adds a merge commit to your branch. Nothing is rewritten, and a normal git push works.
Option 2, rebase onto main:
git switch feature/login
git fetch origin
git rebase origin/main
git push --force-with-leaseThis leaves a straight history, as if you had started from the latest main. The force push is needed because the branch's commits have new SHAs, so the remote cannot fast-forward to them.
Conflicts during a rebase
A merge stops once, with all conflicts together. A rebase replays your commits one at a time and can stop at each one. When it stops, fix the files, stage them, and continue:
git status
# edit the conflicted files, then:
git add path/to/conflicted-file
git rebase --continueYou have two other ways out:
git rebase --skipdrops the commit being replayed. Use it only when the commit's change is already present upstream.git rebase --abortreturns the branch to its state before the rebase began.
The labels reverse during a rebase, which is a common source of confusion. Git is building the new history on top of the branch you rebase onto. So in conflict tools and in git checkout --ours, "ours" means the upstream branch, and "theirs" means your commit being replayed. In a merge it is the other way round.
Force pushing safely
After a rebase you must overwrite the remote branch. Plain --force replaces whatever is there, including commits a teammate pushed after your last fetch. --force-with-lease is the safer form. It refuses the push if the remote branch is not where your remote-tracking branch says it is.
git push --force-with-leaseThe lease has a limit. It compares against your local origin/feature/login. If something fetches in the background, such as an editor or a scheduled job, that ref updates without you looking at the new commits. The lease then passes and you overwrite them. To avoid this, pin the expected SHA:
git push --force-with-lease=feature/login:<expected-sha>Tidying commits before you share them
Interactive rebase edits your own unpublished commits:
git rebase -i origin/mainGit opens a list of your commits. Change the word before a commit to reword to edit its message, squash to fold it into the previous commit and combine the messages, or fixup to fold it in and discard its message. You can also reorder lines or delete one to drop that commit. This is the most useful form of rebase. It turns "wip", "fix typo", and "fix again" into a few commits that each make sense on their own.
Merge variants for landing a branch
When a feature is ready to land on main, you have three options:
git merge --no-ff feature/loginalways creates a merge commit, so the branch stays visible in history even when a fast-forward was possible.git merge --squash feature/loginstages the combined changes without committing. You then rungit committo record them as one commit onmain.- Rebase the feature onto
main, then fast-forwardmainwithgit merge --ff-only feature/login. This keeps history linear and keeps every commit.
A squash has a side effect. The new commit has no link to the branch's commits, so Git cannot tell the branch was merged. git branch -d feature/login refuses to delete it, and you must use -D. Check first that the work really landed.
Choosing between them
These criteria help decide:
- Shared branches get merges. Rebasing only changes your own unpublished work.
- Many teams rebase a private feature branch before review, to keep it current and tidy, and merge it when it lands.
- Merge commits record when and how lines of work joined. Linear history is easier to read with
git logand to bisect, since there are no merge commits to step through. - A long-lived branch with many commits that all conflict with
mainis easier to merge. A rebase would make you resolve the same conflict repeatedly. - Follow the team's convention. Consistency matters more than which choice is better.
You can also set git config pull.rebase true. Then git pull rebases your local commits onto the fetched ones instead of creating a merge commit.
Recovering from a bad rebase
A rebase that has not finished can be cancelled with git rebase --abort. If it finished and the result is wrong, Git saved the old tip in ORIG_HEAD:
git reflog
git reset --hard ORIG_HEADgit reflog shows where the branch pointed before the rebase, so you can check the SHA first. Another rebase, merge, or reset overwrites ORIG_HEAD, so if you have done any of those since, take the SHA from the reflog instead.
The command git reset --hard discards uncommitted changes in your working tree. Commit or stash first.
Common mistakes
- Rebasing
mainor a branch others use, then force pushing. Teammates end up with duplicated commits. - Using
git push --forcewhere--force-with-leasewould do. - Resolving a rebase conflict by choosing the wrong side because "ours" and "theirs" are reversed.
- Rebasing with uncommitted changes in the working tree. Commit or stash first.
- Deleting a squash-merged branch with
-Dbefore confirming the squash commit landed.
Exercise
You rebased feature/login onto origin/main and git push was rejected as non-fast-forward. Name the command to use, and explain why it is safer than git push --force.
Show answer
Use git push --force-with-lease. The push is rejected because the rebase gave your commits new SHAs, so the remote branch cannot fast-forward to them. The lease option overwrites the remote branch only if it still matches your remote-tracking branch. If a teammate pushed commits you have not fetched, Git refuses, so you do not destroy their work. Plain --force overwrites it regardless.
Exercise
During git rebase origin/main, a conflict stops the rebase. In the conflicted file, the section between <<<<<<< and ======= is your own change. After you fix the file, which two commands finish this step and continue?
Show answer
Run git add path/to/conflicted-file to stage the resolved file, then git rebase --continue. The section between <<<<<<< and ======= is labelled as "ours" (the upstream side), so it is not your change. Yours is the part after =======, since your commit is being replayed.