Rebase vs. merge: keep your Git history clean
When you bring changes from one Git branch into another, you can merge or rebase. Both have their place, but rebase keeps the commit history clean and readable, which is why we reach for it first in most workflows. This post explains how the two differ, why rebase usually wins, when merge is the better choice, and a few habits that keep your history useful.
How merge and rebase differ
Merge: join the branches with a merge commit
A merge takes the changes from a feature branch and joins them into the target branch (for example, main or develop). Git records this as a merge commit, and every time you merge main back into your branch to stay up to date, you add another one. Over time, these commits clutter the history.
Merge commit M joins main into the feature branch, and every sync with main adds another one. The result is a non-linear history that makes individual changes harder to trace.
Rebase: replay your commits on top
A rebase moves your branch instead: it takes each commit from the feature branch and replays it on top of the target branch. The result is a linear history, as if you had started your work from the latest version of main.
X′, Y′ and Z′ are new commits with the same changes as X, Y and Z, reapplied in order after main. The history reads as one straight line.
Why rebase usually wins
1. A history you can read
A rebased history is linear and organized. Without merge commits in the way, git log shows what changed and in what order, which makes it easier to find when a bug was introduced or to revert a single change.
2. Easier code reviews
When a branch tells a clear story, one meaningful commit after another, reviewers can follow how the feature was built, review it commit by commit, and spot problems sooner.
3. Smaller, earlier conflicts
Rebasing your feature branch onto main or develop often keeps it close to the code everyone else is working on. Conflicts then show up early and in small pieces, one commit at a time, instead of all at once after the branch has drifted for weeks.
4. A shared history that stays readable
When several developers work in the same repository, each one integrates their work without adding redundant merge commits. The shared history stays readable for everyone, including whoever joins the project next.
When merge is the better choice
Rebase is not always the right tool. Merge commits are still the better choice when you are:
- Preserving the context of a branch: if a feature branch has several contributors, merging keeps its commits exactly as they were made.
- Merging long-lived branches: when you bring one major branch into another (for example,
developintomain), the merge commit marks the moment it happened. - Working on a shared branch: rebasing rewrites history, so never rebase a branch other people are building on.
⚠️ Rebasing does not move your existing commits; it creates new ones with the same changes. If you have already pushed a branch and then rebase it locally, your copy and the remote’s no longer match, and Git will only accept the push with --force. A force push replaces the remote branch with yours, discarding any commits others pushed to it in the meantime.
That is why rebasing is discouraged on shared branches such as main, develop or any branch several developers commit to. Use merge commits there. On a feature branch only you work on, rebasing after pushing is fine: push with git push --force-with-lease, which refuses to overwrite the remote if someone else has pushed to it since you last fetched.
How to rebase safely
- Rebase feature branches before merging them into the upstream branch (for example,
mainordevelop). - Don’t rebase shared branches, so you never rewrite history others depend on.
- Use
git pull --rebaseinstead ofgit pullto sync with the remote without creating merge commits. - If a conflict comes up, resolve it, stage the files and run
git rebase --continue. If things go wrong,git rebase --aborttakes you back to where you started. - When merging a Pull Request on GitHub, choose “Rebase and merge” instead of “Create a merge commit”, so no merge commit lands on the base branch.

Tips for a tidy commit history
1. Fold small fixes into the last commit
If you need to add a small fix to your last commit, you don’t need a new commit with a near-duplicate message. git commit --amend adds your staged changes to the last commit and lets you keep or rewrite its message.
First commit:
git commit -sm "Create migration to merge previous migrations for firms app"Second commit with small changes:
git commit --amend -sm "Create migration to merge previous migrations for firms app"Here -m sets the message, and -s adds a sign-off line to it, such as:
Signed-off-by: Matheus Gomes <[email protected]>
To keep the previous message as it is, use git commit --amend --no-edit. Like a rebase, amending rewrites the last commit, so if you had already pushed it, you will need to force push.
2. Let AI draft your commit messages
Tools like GitHub Copilot can draft a commit message from your changes, which helps keep messages clear and consistent across the history. Here’s an example from the Source Control panel in VS Code:

3. Squash noisy commits into one
If your Pull Request has many small or duplicate commits, or messages written in a hurry, you can squash them into one. You can do it locally with Git commands (see Git Tower’s guide to squashing commits and the FAQ below) or when you merge the Pull Request, by choosing “Squash and merge”. GitHub then opens a text area where you can write or edit the message of the combined commit. Here’s an example:

Rebase first, merge where it counts
Rebase keeps your Git history linear and meaningful, which makes debugging, code review and collaboration easier. Merge commits still have their place, on shared and long-lived branches, but a rebase-first approach gives most teams a cleaner, more structured workflow.
Knowing when to rebase, when to merge and how to keep commits tidy helps your team keep the repository easy to understand for years to come.
Frequently Asked Questions
What does it mean to squash commits in Git?
Squashing combines several commits into one, with a single commit message. Git has no git squash command: you squash with an interactive rebase, a squash merge, or the “Squash and merge” option when merging a Pull Request.
When should you squash your commits?
It depends on what your team wants the history to show. Squashing a feature branch before it is merged leaves one commit for the whole feature on the main branch, which is easier to read and to revert. Keeping every commit preserves each step of the work. Neither is right for every project, so agree on one approach and use it consistently.
How do you squash commits with interactive rebase?
Run git rebase -i HEAD~3 to open the last three commits in your editor. Keep pick on the first one, change pick to squash (or s) on the ones you want to fold into it, then save and write the message for the combined commit.
How do you squash commits when merging a branch?
Run git merge --squash <branch>, for example git merge --squash feature/login. Git stages all of the branch’s changes without creating a merge commit, so you finish with a regular git commit and a message of your own.
Can you squash commits when merging a Pull Request?
Yes. GitHub, GitLab and Bitbucket all offer a squash option when merging a Pull Request. On GitHub, choose “Squash and merge” and edit the commit message before you confirm.
Related articles

Article · 3 min read
3 awesome Git commands to help you debug faster
Use blame, bisect and rebase to track down when and why a bug entered your code.
3 min read

Article · 7 min read
Pull Requests: Merging good practices into your project (part 1)
Part 1 introduces Pull Requests and shares data on why code review often falls short.
7 min read




