How to avoid git conflicts in a team?

前端 未结 8 2221
情歌与酒
情歌与酒 2020-12-12 13:17

We are some developers who work on same project and we use git for the project. If two or more of us happen to work on same file, we receive git conflicts which are hard to

8条回答
  •  执笔经年
    2020-12-12 14:15

    Ordering.

    There are a few other things you can do that might help too. It will be clearer if I post them separately.

    Where you insert new things will help determine whether you create conflicts.

    Imagine a list of names of employees

    Andy, 
    Oliver, 
    Ivan,
    

    Then Brad and Patrick join and their names are added to the list. You add Brad and I add Patrick. We both add the names to the bottom of the list, and then use git to merge our lists. The result will be familiar to git users :-

    Merge branch 'Patrick' into Brad
    
    Conflicts:
        names.txt
    
    @@@ -1,4 -1,4 +1,8 @@@
      Andy, 
      Oliver, 
      Ivan,
      <<<<<<< HEAD
     +Brad,
      =======
    + Patrick,
      >>>>>>> Patrick
    

    Now suppose we had done the same thing but imposed a simple alphabetical ordering rule on our list. Now when we come to merge the two branches the results are a little more pleasing :-

    Andy,
    Ivan,
    Oliver,
    

    Add one name yourself and then merge the other person's change with git, to add the other name.

    Auto-merging names.txt
    Merge made by the 'recursive' strategy.
     names.txt | 1 +
     1 file changed, 1 insertion(+)
    

    And we get

    Andy,
    Brad,
    Ivan,
    Oliver,
    Patrick,
    

    Since we don't know who is going to join the company next we are effectively adding to the list randomly, and by inserting in random places, conflicts of location in the files are less likely.

提交回复
热议问题