What's the difference between git merge
and git rebase
?

- 54,432
- 29
- 203
- 199

- 30,507
- 32
- 137
- 219
-
17since my answer was deleted, visit this link to get the right answer for this question: http://git-scm.com/book/en/Git-Branching-Rebasing#The-Basic-Rebase – laplasz Oct 01 '13 at 07:18
-
8By the way i will add this site. All you need to know about git learn by playing: http://pcottle.github.io/learnGitBranching/ – Rollyng Nov 07 '13 at 15:44
-
1Read this first: http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging Then: http://git-scm.com/book/en/v2/Git-Branching-Rebasing You'll really understand. – Liber Nov 19 '14 at 02:33
-
1[version control - When do you use git rebase instead of git merge? - Stack Overflow](https://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-merge) – user202729 Aug 14 '18 at 14:32
8 Answers
Suppose originally there were 3 commits, A
,B
,C
:
Then developer Dan created commit D
, and developer Ed created commit E
:
Obviously, this conflict should be resolved somehow. For this, there are 2 ways:
MERGE:
Both commits D
and E
are still here, but we create merge commit M
that inherits changes from both D
and E
. However, this creates diamond shape, which many people find very confusing.
REBASE:
We create commit R
, which actual file content is identical to that of merge commit M
above. But, we get rid of commit E
, like it never existed (denoted by dots - vanishing line). Because of this obliteration, E
should be local to developer Ed and should have never been pushed to any other repository. Advantage of rebase is that diamond shape is avoided, and history stays nice straight line - most developers love that!

- 111,019
- 13
- 122
- 148
-
91Nice illustrations. However, I do not fully agree with the positive tone that rebase is handled. In both merge and rebase conflicts can occur that need manual resolution. And as always when programmers are involved there is a non-neglectable chance of errors aka bugs. If a merge error happens the whole team or community can see the merge and verify whether a bug was introduced there. The history of the rebase stays in 1 developer's repo and even there it has only limited lifetime in the reflog. It might look nicer, but nobody else can see as easily what went wrong. – Uwe Geuder Sep 23 '13 at 12:59
-
9> "However, this creates diamond shape, which many people find very confusing." Um...can you elaborate? – Greg Maletic Jul 03 '14 at 20:07
-
4@GregMaletic: Diamond shape is non-linear history. I don't know about you, but I do not like non-linear things in general. That said, you are welcome to use merge with diamonds if you really prefer it - nobody is forcing you. – mvp Jul 03 '14 at 20:43
-
4While this answer is extremely helpful, it would be better if you added actual git commands with simple foo.txt files to reproduce it locally. Like last user said, it's not obvious who's doing rebase. – Vortex Feb 26 '17 at 02:51
-
2@Vortex: the whole idea is to give you explanation of the concept. Adding any git commands would just obscure what's really happening. – mvp Feb 26 '17 at 03:00
-
Perhaps a better comparison is `git merge --squash` and `git rebase`, which both try to create a history that shows code that actually existed in a single place together. Git merge (or pull) will interleave commits by date, looking like certain commits existed together (when there is no way to tell if they ever did) and implying the code at any date actually works together (there is no such implication nor is it often true). IMO `git merge` no `--squash` should never be used, it comes just short of creating lies that only the most circumspect engineer can see through. – pferrel Jan 23 '19 at 00:56
-
1@pferrel: l don't think you got it correctly. `git merge` does not interleave commits (but it might appear so by looking at `git log`). Instead, `git merge` keeps both development histories by Dan and Ed preserved intact, as it was seen from each one point of view at a time. `git rebase` makes it look like that Dan worked on it first, and Ed followed him. In both cases (merge and rebase), actual resultant file tree is absolutely identical. – mvp Jan 23 '19 at 07:35
-
@mvp, You explanation doesn't explain why Git keep Dan's commit and not Ed's. – ATL_DEV Nov 15 '19 at 23:00
-
@ATL_DEV: both commits are kept in history. If rebase was done by Ed, then history would appear D first, E second. If Dan was doing rebase, it would be E then D. But, regardless of order, final state is exactly the same, and is equivalent to merge. For merge, both points of view are kept intact, and it is impossible to say which one was done first. – mvp Nov 16 '19 at 04:30
-
@mvp. Git rebase operation appears to be superfluous. If the only real difference is cosmetic, then whether it's a straight or not should really be determined in how it is visually rendered on the screen. In a sense, it's like directories in a file system which are only useful for human organization, but serves no purpose in the actual file structure implementation. – ATL_DEV Nov 16 '19 at 18:08
-
@ATL_DEV: it's not just cosmetic. Because merge keeps both development histories intact, it makes harder for people to understand what's going on. Rebase linearizes this natural conflict on who was there first. – mvp Nov 16 '19 at 18:28
-
@mvp, just to be clear, I don't see why it should linearize the conflicts at the repo level when it can keep always keep the histories intact but linearize the tree only when it presents the tree visually. – ATL_DEV Nov 16 '19 at 22:04
-
@mvp, in your answer you said "for git rebase, ...we get rid of commit E, like it never existed (denoted by dots - vanishing line)", but in your reply to "ATL_DEV", you said "for git rebase, ...both commits are kept in history...". I'm confused here: for git rebase, if "both commits are kept in history", did you really "get rid of commit E, like it never existed"? where can you see the history of commit E? – gangmax Nov 18 '20 at 01:54
-
@gangmax, commit E is removed by rebase, but changes intended by that comment live on in R. Difference between R and D is the same as difference between E and C. For merge, commit E is fully preserved in history, but that history becomes forked, which could make it more difficult to understand what's really going on. – mvp Nov 18 '20 at 16:41
Personally I don't find the standard diagramming technique very helpful - the arrows always seem to point the wrong way for me. (They generally point towards the "parent" of each commit, which ends up being backwards in time, which is weird).
To explain it in words:
- When you rebase your branch onto their branch, you tell Git to make it look as though you checked out their branch cleanly, then did all your work starting from there. That makes a clean, conceptually simple package of changes that someone can review. You can repeat this process again when there are new changes on their branch, and you will always end up with a clean set of changes "on the tip" of their branch.
- When you merge their branch into your branch, you tie the two branch histories together at this point. If you do this again later with more changes, you begin to create an interleaved thread of histories: some of their changes, some of my changes, some of their changes. Some people find this messy or undesirable.
For reasons I don't understand, GUI tools for Git have never made much of an effort to present merge histories more cleanly, abstracting out the individual merges. So if you want a "clean history", you need to use rebase.
I seem to recall having read blog posts from programmers who only use rebase and others that never use rebase.
Example
I'll try explaining this with a just-words example. Let's say other people on your project are working on the user interface, and you're writing documentation. Without rebase, your history might look something like:
Write tutorial
Merge remote-tracking branch 'origin/master' into fixdocs
Bigger buttons
Drop down list
Extend README
Merge remote-tracking branch 'origin/master' into fixdocs
Make window larger
Fix a mistake in howto.md
That is, merges and UI commits in the middle of your documentation commits.
If you rebased your code onto master instead of merging it, it would look like this:
Write tutorial
Extend README
Fix a mistake in howto.md
Bigger buttons
Drop down list
Make window larger
All of your commits are at the top (newest), followed by the rest of the master
branch.
(Disclaimer: I'm the author of the "10 things I hate about Git" post referred to in another answer)

- 114,604
- 39
- 168
- 219
-
2Commit digrams have pointers pointing to parents not children, _because that's what git stores_. Understanding that - and the fact that a branch is a pointer to a single commit at its "tip" - is incredibly useful for understanding many things that git does. I've also no idea what you mean by "GUI tools for Git have never made much of an effort to present merge histories more cleanly, abstracting out the individual merges". Your actual descriptions of rebase and merge are great though. – IMSoP Jun 15 '21 at 13:13
While the accepted and most upvoted answer is great, I additionally find it useful trying to explain the difference only by words:
merge
- “okay, we got two differently developed states of our repository. Let's merge them together. Two parents, one resulting child.”
rebase
- “Give the changes of the main branch (whatever its name) to my feature branch. Do so by pretending my feature work started later, in fact on the current state of the main branch.”
- “Rewrite the history of my changes to reflect that.” (need to force-push them, because normally versioning is all about not tampering with given history)
- “Likely —if the changes I raked in have little to do with my work— history actually won't change much, if I look at my commits diff by diff (you may also think of ‘patches’).“
summary: When possible, rebase is almost always better. Making re-integration into the main branch easier.
Because? ➝ your feature work can be presented as one big ‘patch file’ (aka diff) in respect to the main branch, not having to ‘explain’ multiple parents: At least two, coming from one merge, but likely many more, if there were several merges. Unlike merges, multiple rebases do not add up. (another big plus)
Git rebase is closer to a merge. The difference in rebase is:
- the local commits are removed temporally from the branch.
- run the git pull
- insert again all your local commits.
So that means that all your local commits are moved to the end, after all the remote commits. If you have a merge conflict, you have to solve it too.

- 854
- 9
- 7
-
2
-
“Everything should be made as simple as possible, but not simpler”. And you made it, sir! – mirekphd Oct 28 '22 at 06:01
What's the difference between merge
and rebase
?
Reading the official Git manual it states that “rebase reapplies commits on top of another base branch”, whereas “merge joins two or more development histories together”. In other words, the key difference between merge and rebase is that while merge
preserves history as it happened, rebase
rewrites it.
Let's contextualize these statements with a side-by-side example!
As illustrated above, the merge
operation intertwined the branches together by creating a new single merge commit (C7), causing a diamond shaped non-linear history — essentially preserving history as it happened. By comparing this result with the outcome from the rebase
action we see that no merge commit was created, instead the two commits C5 and C6 simply got rewinded and reapplied straight on top of C4, keeping the history linear.
If we scrutinise the two reapplied commits even further, we can see that the hashes have changed, indicating that rebase
truly rewrites history.
Worth noting
Whenever you rebase
a branch, new commits will always be generated even though the content might still be the same! That said, any former commits will eventually (post garbage collection) be deleted from history if no other pointer (branch/tag) is referencing them.
With great power comes great responsibility
We’ve seen how rebase rewrites history, while merge preserves it. But what does this mean in a broader sense? And what possibilities and potential drawbacks do the two operations come with?
Conflicting changes
Let’s say, for example, you’ve had some nasty conflicts trying to integrate the changes. In the merge scenario, you would have only needed to solve the conflicts once, straight in the C7 commit. With rebase, on the other hand, you could potentially have been forced to solve similar conflicts in each commit (C5 and C6) as they were reapplied.
Published branches
Another potential problem related to rebase occurs when the branch you are rebasing has already been published remotely, and someone else has based their work on it. Then, your rebased branch can cause serious confusion and headaches for all involved parties as Git will tell you that your branch is both ahead and behind at the same time. If this happens, pulling remote changes using the rebase flag (git pull --rebase) generally solves the problem.
Furthermore, whenever you are rebasing an already published branch, regardless if no one else has based their work on it, you’ll still need to force push it to get your updates to the remote server — overwriting the existing remote reference completely.
Loss of data (to your advantage)
Finally, as rebase rewrites history while merge preserves it, it’s possible to actually lose data when rebasing. When new commits are reapplied, the old ones are (eventually, post garbage collection) deleted. This same trait is in fact what makes rebase so powerful — it allows you to tidy up your development history before making it publicly available!
Conclusion
While merge
is safe to use from a potential data loss perspective, and can feel more straight forward to use. Here are some pointers that can help you avoid the most common issues related to rebase
.
- Don't rebase a branch that’s been published remotely…
- …unless you know you are the only one working on it (and feel safe force pushing)
- Prior to rebasing, create a backup branch from the tip of the branch you’re about to rebase, as it will allow you to easily compare the outcome (once done) and jump back to the pre-rebase state if necessary.
Source: Above excerpt is taken from this full length post on the subject: Differences Between Git Merge and Rebase — and Why You Should Care

- 829
- 1
- 12
- 13
For easy understand can see my figure.
Rebase will change commit hash, so that if you want to avoid much of conflict, just use rebase when that branch is done/complete as stable.

- 2,496
- 1
- 17
- 13
I found one really interesting article on git rebase vs merge, thought of sharing it here
- If you want to see the history completely same as it happened, you should use merge. Merge preserves history whereas rebase rewrites it.
- Merging adds a new commit to your history
- Rebasing is better to streamline a complex history, you are able to change the commit history by interactive rebase.

- 5,672
- 3
- 29
- 43
let's say you have done 3 commits in your feature branch when you want to send your feature branch changes to the main branch. You have two options
git merge
: In this case main branch will receive only 1 commit (combining 3 commits)git rebase
: In this case main branch will receive 3 commits

- 1,127
- 1
- 12
- 25
-
So at the end of the day git merge is just a git rebase with squash commits? – user2896152 Mar 29 '23 at 20:17