Why I Prefer Merge Over Rebase
Why I Prefer Merge Over Rebase
The author explores the philosophical debate of Merge vs. Rebase workflows in Git.
Join the DZone community and get the full member experience.Join For Free
Easily enforce open source policies in real time and reduce MTTRs from six weeks to six seconds with the Sonatype Nexus Platform. See for yourself - Free Vulnerability Scanner.
There are many ways to work with Git. The workflows vary depending on the size of the team, organization, and on the way of working — is it distributed, is it sprint-based, is it a company, or an open-source project, where a maintainer approves pull requests?
You can use vanilla-Git, you can use GitHub, BitBucket, GitLab, or Stash. And then on the client side you can use the command line, IDE integration, or stand-alone clients like SourceTree.
The workflows differ mostly in the way you organize you branches and the way you merge them. Do you branch off branches? Do you branch off other people’s branches, which are work-in-progress? Do you push or stay local? Do you use it like SVN (perfectly fine for a single developer on a pet project), or you delve into more “arcane” features like
This is all decided by each team, but I’d like to focus on one very debated topic — rebasing vs merging. While you can get tons of results discussing rebasing vs. merging, including the official Git documentation, it has become more of a philosophical debate rather than a practical one.
I recently asked a practical question about a rebase workflow. In short, by default rebasing seems not to favor pushing stuff to the central repo. If you do that before rebasing, you’d always need to force-push. And force-pushing may make it very hard for people that are based on your branch. Two questions that you are already asking:
- Why do you need to push if something isn’t ready? Isn’t it the point of the “D” in “DVCS” to be able to commit locally and push only when ready? Well, even if you don’t use Git as SVN, there are still plenty of use cases for pushing every change to your own feature branch remote – you may be working from different machines, a colleague may want to pick up where you left off (before leaving for holiday or falling sick), or even hard drive failures and theft. I think basically you have to push right before you log off, or even more often. The “distributed” allows for working offline, or even without a central repo (if it goes down), but it is not the major benefit of Git.
- Why would anyone be based on your work-in-progress branch? Because it happens. Sometimes tasks are not split that strictly and have dependencies – you write a piece of functionality, which you then realize should be used by your teammates who work on another task within the same story/feature. You aren’t yet finished (e.g. still polishing, testing), but they shouldn’t wait. Even a single person may want to base his next task on the previous one, while waiting for code review comments. The tool shouldn’t block you from doing this from time to time, even though it may not be the default workflow scenario.
Also, you shouldn’t expect every team member to be a Git guru, who rewrites history for breakfast. A basic set of commands (even GUIs) should be sufficient for a Git workflow, including the edge cases. Git is complicated and the task of each team is to make it work for them, rather than against them. There is probably an article for each Git command or concept with a title “X considered harmful”, and going through that maze is not trivial for an inexperienced Git user. As Linus Torvalds once allegedly said:
Git has taken over where Linux left off separating the geeks into know-nothings and know-it-alls. I didn’t really expect anyone to use it because it’s so hard to use, but that turns out to be its big appeal.
Back to the rebase vs. merge – merge (with pull requests) feels natural for the above. You branch often, you push often. Rebase can work in the above use cases (which I think are necessary). You can force-push after each rebase, and you can make sure your teammates resolve that. But what’s the point?
The practical argument is that the graph that shows the history of the repo is nice and readable. Which I can’t argue with, because I’ve never had a case when I needed a cleaner and better graph. No matter how many merge commits and ugliness there is in the graph, you can still find your way (if you ever need to). Besides, a certain change can easily be traced even without the graph (e.g. git annotate).
If you are truly certain you can’t go without a pretty graph, and your teammates are all Git gurus who can resolve a force-push in minutes, then rebasing is probably fine.
But I think a merge-only workflow is the more convenient way of work that accounts for more real-world scenarios.
I realize this is controversial, and I’m certainly a Git n00b (I even use SourceTree rather than the command line for basic commands, duh). But I have used both merge and rebase workflows and I find the merge one more straightforward (after all, force-pushing being part of the regular workflow sounds suspicious?).
Git is the Scala of VCS – it gives you many ways to do something, but there is no “right way”. This isn’t necessarily bad, as indeed there are many different scenarios where Git can be used. For the ones I’ve had (regular project in a regular company, with a regular semi-automated release & deployment cycle, doing regular agile), I’d always go for merge, with pull requests.
Published at DZone with permission of Bozhidar Bozhanov , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.