If you have any doubt, use merge.
The only differences between a rebase and a merge are:
- The resulting tree structure of the history (generally only noticeable when looking at a commit graph) is different (one will have branches, the other won't).
- Merge will generally create an extra commit (e.g. node in the tree).
- Merge and rebase will handle conflicts differently. Rebase will present conflicts one commit at a time where merge will present them all at once.
So the short answer is to pick rebase or merge based on what you want your history to look like.
There are a few factors you should consider when choosing which operation to use.
Is the branch you are getting changes from shared with other developers outside your team (e.g. open source, public)?
If so, don't rebase. Rebase destroys the branch and those developers will have broken/inconsistent repositories unless they use
git pull --rebase. This is a good way to upset other developers quickly.
How skilled is your development team?
Rebase is a destructive operation. That means, if you do not apply it correctly, you could lose committed work and/or break the consistency of other developer's repositories.
I've worked on teams where the developers all came from a time when companies could afford dedicated staff to deal with branching and merging. Those developers don't know much about Git and don't want to know much. In these teams I wouldn't risk recommending rebasing for any reason.
Does the branch itself represent useful information
Some teams use the branch-per-feature model where each branch represents a feature (or bugfix, or sub-feature, etc.) In this model the branch helps identify sets of related commits. For example, one can quickly revert a feature by reverting the merge of that branch (to be fair, this is a rare operation). Or diff a feature by comparing two branches (more common). Rebase would destroy the branch and this would not be straightforward.
I've also worked on teams that used the branch-per-developer model (we've all been there). In this case the branch itself doesn't convey any additional information (the commit already has the author). There would be no harm in rebasing.
Might you want to revert the merge for any reason?
Reverting (as in undoing) a rebase is considerably difficult and/or impossible (if the rebase had conflicts) compared to reverting a merge. If you think there is a chance you will want to revert then use merge.
Do you work on a team? If so, are you willing to take an all or nothing approach on this branch?
Rebase operations need to be pulled with a corresponding
git pull --rebase. If you are working by yourself you may be able to remember which you should use at the appropriate time. If you are working on a team this will be very difficult to coordinate. This is why most rebase workflows recommend using rebase for all merges (and
git pull --rebase for all pulls).
Merge destroys history (squashes commits)
Assuming you have the following merge:
B -- C
Some people will state that the merge "destroys" the commit history because if you were to look at the log of only the master branch (A -- D) you would miss the important commit messages contained in B and C.
If this were true we wouldn't have questions like this. Basically, you will see B and C unless you explicitly ask not to see them (using --first-parent). This is very easy to try for yourself.
Rebase allows for safer/simpler merges
The two approaches merge differently, but it is not clear that one is always better than the other and it may depend on the developer workflow. For example, if a developer tends to commit regularly (e.g. maybe they commit twice a day as they transition from work to home) then there could be a lot of commits for a given branch. Many of those commits might not look anything like the final product (I tend to refactor my approach once or twice per feature). If someone else was working on a related area of code and they tried to rebase my changes it could be a fairly tedious operation.
Rebase is cooler / sexier / more professional
If you like to alias
rm -rf to "save time" then maybe rebase is for you.
My Two Cents
I always think that someday I will come across a scenario where git rebase is the awesome tool that solves the problem. Much like I think I will come across a scenario where git reflog is an awesome tool that solves my problem. I have worked with git for over five years now. It hasn't happened.
Messy histories have never really been a problem for me. I don't ever just read my commit history like an exciting novel. A majority of the time I need a history I am going to use git blame or git bisect anyways. In that case having the merge commit is actually useful to me because if the merge introduced the issue that is meaningful information to me.