Git rebase vs merge

In short
Git rebase and merge are two ways to bring changes from one branch into another. Merge creates a new commit that joins the two histories, while rebase replays your commits on top of the other branch, producing a straight-line history.
What rebase and merge do
Git keeps a record of changes as commits. When two branches split apart and both get new commits, you need a way to bring them back together. Git offers two main tools for this: merge and rebase.
A merge takes the latest snapshot of each branch and their most recent common ancestor, then builds a new snapshot and a new commit that ties both lines of work together. The history keeps its fork and join shape.
A rebase works differently. It takes the change each of your commits introduced, and replays those changes on top of another branch. Git then creates new commits for the replayed work. The end result looks as if your work happened after the other branch, in a single line.
How rebase works
Suppose you are on a branch called experiment and want to bring in the latest work from master. You can run the following commands:
git checkout experiment
git rebase master
Git finds the common ancestor of the two branches, collects the changes from each of your commits, and resets your branch to the tip of master. It then applies your changes one at a time. Because the replayed commits are new commits, their identifiers differ from the originals, even though the content of the changes is the same.
After the rebase, you can switch to master and run a fast-forward merge. A fast-forward simply moves the branch pointer forward, so no extra merge commit is needed.
git checkout master
git merge experiment
The final snapshot is the same whether you merged or rebased. Only the history looks different. A rebased history reads as a straight line, which many people find easier to follow. A merged history shows when work happened in parallel and when it came back together.
When rebase is useful
Rebase is handy when you want your commits to apply cleanly on top of the latest upstream work. A common case is contributing to a project you do not maintain. You can do your work on a local branch, then rebase it onto the main project's latest branch before you submit it. The maintainer can then accept the changes with a simple fast-forward or a clean apply, without doing extra integration work.
Rebase is also useful for tidying up a personal branch before sharing it. Your history stays readable, and you avoid a string of small merge commits from keeping up with the main line.
The main risk: rewriting shared history
The most important rule about rebase is this: do not rebase commits that other people may have based their work on. Rebase abandons the original commits and creates similar but different ones. If you have already pushed commits that others have pulled, and you rewrite them, your collaborators will have to untangle two versions of the same work.
A typical problem looks like this. A teammate rebases and force pushes to the shared branch. You then pull, and Git creates a merge commit that contains two copies of the same changes. The history becomes confusing, and pushing it back can spread the mess further.
Git can sometimes recover from this. It calculates a patch ID based on the changes in each commit, so it can often tell which commits are truly yours. Still, the safest approach is to avoid rewriting public history in the first place.
If you only rebase commits that exist on your own computer, you are generally safe. If your team is involved, agree on the rule before you start. Pulling with rebase instead of merge can also help. You can make it the default with the pull.rebase setting, for example by running git config --global pull.rebase true.
Choosing between them
There is no single correct answer. Some teams prefer merge because the history is a true record of what happened, including parallel work. Other teams prefer rebase because a straight history is easier to read and review.
A practical rule works well for most people. Rebase your private, local work to keep it tidy. Merge shared branches so that everyone's history stays intact. Whichever you choose, make sure your team knows which approach you use, so nobody is surprised by a force push or an unexpected merge commit.
Common mistakes
Beginners often rebase a branch that has already been pushed and shared, then try to push again and get rejected. Forcing the push can then overwrite a colleague's work. Another common mistake is resolving conflicts in a rebase one commit at a time without checking the result. Run your tests after a rebase, and check the log to confirm the history looks right.
FAQ
Does rebase change the code in my files?
The final snapshot of files is the same as with a merge when the two branches have no conflicting edits. What changes is the commit history. Rebase creates new commits that contain the same changes, placed on a different base.
Is it safe to rebase a branch I already pushed?
It is safe only if nobody else has based work on those commits. If others have pulled them, rewriting them causes duplicate commits and confusion. Coordinate with your team first, and use git pull --rebase so everyone knows what changed.
Which should I use, merge or rebase?
Use rebase to tidy up your own local work before sharing it. Use merge for shared branches where keeping the full history matters. Many teams use both, following a rule they agree on.
What does a fast-forward merge mean?
A fast-forward merge happens when the target branch has no new commits of its own. Git simply moves the branch pointer forward to your latest commit, so no merge commit is created.
Sources
This page is based on the official documentation below. Read it for full detail.
Git rebase — official documentation ↗