DEV Community

Discussion on: Git Merge vs Rebase

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Because a rebase moves commits (technically re-executes them), the commit date of all moved commits will be the time of the rebase and the git history loses the initial commit time. So, if the exact date of a commit is needed for some reason, then merge is the better option. But typically, a clean git history is much more useful than exact commit dates.

This is a non-issue, as git distinguishes between commit time and author time, the latter of which is kept when rebasing (or maybe there was a special flag for this to happen?), so you can clearly see when a commit was first created as well as when it was (last) put into the tree.

If the rebased branch has multiple commits that change the same line and that line was also changed in the base branch, you might need to solve merge conflicts for that same line multiple times, which you never need to do when merging. So, on average, there's more merge conflicts to solve.

I'm pretty sure git has a somewhat obscure option to remember merges, so it can figure out similar merges in the future automatically, precisely for this specific case. Can't remember what it's called (because I never really needed it), but I'm sure you can google it.


Overall, here's my take: Use rebase unless there was an actual branch in your work. At my workplace there's always lots of "merge changes from http://gitlab/some/project" type merge commits with one or two small commits on either side. Personally, I'd much rather just slap a --rebase on my git pull and have a less cluttered commit log, and leave the merges for cases where there was an actual branch in development, that is, one dev (or group thereof) was working on a feature separate from the main development version for a prolonged time. This way the commit graph paints a better picture of the actual development process, instead of giving you pointless insight into what coworker wanted to quickly commit a bunch of changes on the live system before pulling the newest main branch (something which should never really happen in the first place)