How to avoid git conflicts when working with a team?

Pages: 1 2

This specific content was written 11 years ago. Please keep this in mind as it may be outdated and not adhering to best-practices.

Continuing…

Versioning PainIn the above image, the ‘!’ slice represents legitimate merge conflicts. The vast majority of awful merges come from lazy whitespace conventions or overly aggressive IDEs (or occasionally, overly-refactory developers). Before you do anything else, standardize your IDE settings and whitespace conventions. Then have everyone issue this command in their local repositories:

# Enable the repository's stock pre-commit hook
mv .git/hooks/pre-commit.sample .git/hooks/pre-commit

That pre-commit hook will conduct a series of checks every time you issue git commit, including a version of git diff --check, a command that checks if your committed changes introduce whitespace errors. The commit will be rejected if it does. If you really need to get around it, you can issue git commit --no-verify, but don’t: Whitespace errors are like the unexploded ordinance of version control. They’re merge conflicts waiting to happen.

If you want to clean up whitespace, refactor a file to improve its indentation, or otherwise make a large series of purely formatting changes, do it in isolated commits, and warn the team to check in their conflicting work in progress first.

The above quote made me realize our problem was I hadn’t defined a set of rules, conventions, and standards for the new guy to follow. As a result he decided to apply his own list of best practices on the existing code base.

Another issue is whether to separate different tasks into different classes (as opposed only to methods). Instead of having large classes containing multiple methods why not refactor and separate code into multiple small classes in order to further decrease the risk of git collisions? This is something I implemented by separating the code the new developer was responsible for and placing it in a completely separate class.

Thoughts? Happy development!



Menelaos Bakopoulos

Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts