tags:

views:

481

answers:

2

As the title suggests, I am curious as to why so many people tout Git as a superior alternative to branching/merging over SVN. I am primarily curious because SVN merging sucks and I would like an alternative solution.

How does Git handle merging better? How does it work?

For example, in SVN, if I have the following line:

Hello World!

Then user1 changes it to:

Hello World!1

then user2 changes it to:

Hello World!12

Then user2 commits, then user1 commits, SVN would give you a conflict. Can Git resolve something simple as this?

+7  A: 

That is called on merge with conflict, and no VCS will ever solve that for you.
You will have to manually solve the merge yourself.

As mentioned in Why merging in git is better than SVN, the actual difference is in the history recording of commits:

That allows Git to remember what has already merged, reducing considerably the conflicts occurrences.

So, when it comes time to do merge from 5b over to the (a) branch, we can use information in the DAG to know that 3b and 2b are already done


So it is the merge worflow that Git will handle much more gracefully than SVN:
See Merge Git vs. SVN for concrete examples.

VonC
@VonC: But yours has pretty picture, and link to answer about SVN's inability to merge, which I couldn't really provide any specifics for. +1!
Jefromi
@Jefromi: yes, I like pretty picture ;) I just added a reference to another SO question which illustrates nicely the difficulties (for SVN) to manage the "more complex merge patterns" you allude to in your answer.
VonC
How'd you make that picture?
John Stoneham
@John: I didn't: Eirk Sink did include it, in his website http://www.ericsink.com/. You will find other instances of those diagrams in http://www.ericsink.com/entries/dvcs_dag_1.html. Erik mentions in http://www.ericsink.com/entries/dvcs_dag_2.html "Several people asked me how I drew those really cool diagrams": "My DAG pictures were drawn by SourceGear's graphic artist, John Woolley, who also did all the artwork for the Evil Mastermind comic books. John is doing the layout and illustration work for my upcoming source control book as well."
VonC
+6  A: 

The specific conflict you mention is always unresolvable. There's simply no way for a merge tool to know which version should be kept.

Git is probably better than SVN at dealing with resolvable conflicts, though. Its primary merge strategy is recursive, which finds the common ancestor of two commits changing the same file and does a three-way merge. It also has some built-in capability for recording and reusing conflict resolutions (git-rerere) and a variety of other merge strategies for special cases.

Git's advantage in merging is that it's part of the history. A merge commit is a commit with two parents. Git's model of history (a directed acyclic graph) expects there to be commits like this. This means that further merges in the future work exactly how they should. Always. (Yes, there are sometimes conflicts, but they're real conflicts, not an inability to handle the merge.)

SVN, on the other hand, just tries to track where merges occurred, but its model is still inherently linear. The history still just has a single string of commits, with merge tracking information providing extra help. From what I've heard, SVN can't always handle more complex merge patterns correctly. (One example is a reflective merge - merging A into B then B into A.)

Jefromi
I was first! For 50 seconds!... But let's face it, your answer is much more detailed and precise ;) +1
VonC