9

Our company uses git to to keep track of document/code changes, with several people making changes and pushing to a central repo. Most of them are novices when it comes to git or command line tools in general. For them we have scripts to update or publish their local repo after making changes.

Are there any workflows that work better for situations like these, to minimize merge conflicts occurring that have to be sorted out by someone more experienced with git?

mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81
bluegray
  • 600
  • 8
  • 18
  • 4
    Besides teaching your co-workers how to use tools? It is really the only way out. No scripts and sugar can substitute for actually knowing what's going on. – pmr Nov 18 '12 at 20:50
  • At all pmr is right: You don't help anyone, if you safe them from learning their tools. Beside this most IDEs have a nice integration of git, thus it's quite easy to branch and merge. – KingCrunch Nov 18 '12 at 20:53
  • Yes of course, teaching everyone to use git properly is the first step, but that is not always possible. I don't aim to eliminate merge conflicts, I know its inevitable. But I'm sure some workflows are better at preventing merge conflicts, hence the question. – bluegray Nov 18 '12 at 21:02
  • Oh, and: (1) In most cases developers are working on different files anyway, (2) git is quite good in merging changes even within a single file (as long as there are not in the same chunk), 3) it's fine to teach your people the branch/merge/remote-commands first and the 180 others later (;)) and (4) your co-workers could/should help each other with problems (not only with git). It's not that hard at the end. – KingCrunch Nov 18 '12 at 21:58

4 Answers4

6

Git is not a replacement for proper development practices. It will not code better for you.

If two developers are touching the exact same code, git has no way of making their job easier, if it's a merge conflict, it'll be one no matter if you're on a branch or not.

Short term solution: use proper local and remote branches to encapsulate work on different features. Use branch diffs (or github pull requests) to review feature sets and help go over diffs and conflicts.

Long term: fix your code, adapt your it to your team and vice versa, and use proper development practices.

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • 1
    +1 for feature branches. We used to have long running branches (one per team in the company) with lots of code getting committed to them. Then some poor sod had to merge that branch into master. Now our team uses small short lived feature branches with the person who mostly worked on that feature merging that code to master when it is ready. Getting code to master quicker reduces conflicts and when you do get conflicts, it is now much more often the person who actually made the conflicting code change is resolving it. – theon Nov 18 '12 at 23:12
  • same opinion here: http://codepen.io/duongphuhiep/post/git-merge-conflicts#how-to-reduce-conflicts-6 – Hiep Mar 21 '17 at 00:28
6

The number one reason for merge conflicts is the time between each merge.
The longer you wait between each merge, the surer you get to see merge conflicts.

You can minimize that by choosing a merge workflow (like git flow for instance) which will advocate for branch per feature, and facilitate the isolation of tasks.
But as long as a common set of files is involved (in two different developments), you will end up with merge conflicts, especially if you wait too long.

So with a distributed VCS, learn to publish (push/pull) regularly, and learn to rebase before merging.

Not only will that decrease the number of conflicts, it will also reduce the number of semantic conflicts: those are merges which seem automatic (no conflicts), but produce an incorrect code.
See "Better, simpler example of 'semantic conflict'?".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Nothing magical. Mostly discipline.

If people have to do something like pull requests or to wait for commits to be reviewed before being merged with some "main" repo or branch, do try to make the time for review as short as possible.

You should probably try to make branch lifetime short, but that really depends on the kind of project you are working on.

Make it a rule that all commits must be as small as possible. Make it a rule that commits must change only one thing. Make it a rule not to mix cosmetic changes (like whitespace) with functional changes and significant refactoring. (Don't forbid whitespace changes completely - just separate them from other changes.)

Beyond version control itself, try to assign tasks to coders in a way that diminishes the chance that they change the same code on the same week or so.

Amir E. Aharoni
  • 1,308
  • 2
  • 13
  • 25
-2

There's an article written here that will help you understand better, "A successful Git branching model". It states that you should separate different types of branches, which include:

  • master
  • hotfixes
  • release branches
  • develop
  • feature branches
Parham
  • 1,302
  • 1
  • 12
  • 19