0

My friend and I are working on the same project and we're using Github. I'm considering creating branches for each of us and then merge to the master.

I just did an experiment, I created 2 branches at the same time, and then modify the same file in the two branches. Then I tried to merge to the master. The first merge is successful, but the second not. It says there is a conflict.

Here is the problem. Suppose My friend and I clone the repo at the same time, and we work on the project, we might make changes both in the same file and in the different files. How could then finally merge our branches?

Or does it have another solution?

Pano
  • 2,099
  • 6
  • 16
  • 24

2 Answers2

1

Conflicts are very common and most likely will appear during your project while working together with someone else, there is no scape from them.

When it happens, the second person (the one with the conflicts) has to deal with them. There are many ways to do so, but the easier way, i think, is:

  1. from terminal and in the branch you want the result to be in (in this case, master), do git merge [branch-name]

  2. now, terminal will say there are conflicts. do git status to see in which files the conflicts are.

  3. access those files and edit them as you want them to be (github will maintain both conflicting codes with lots of >>>>>>> in the middle, just be sure of what you want to maintain).

  4. after solving everything (and you made sure your code is running the way you want to), do git merge --continue

ps: for more info, see: https://git-scm.com/docs/git-merge

Leticia
  • 76
  • 1
  • 4
0

When the conflict appears you have to resolve it. To do so, please take a look at the file mentioned in the error, eg.:

Auto-merging www/index.html
CONFLICT (content): Merge conflict in www/index.html
Automatic merge failed; fix conflicts and then commit the result.

The file should look like this:

<html>
  <body>
<<<<<<< HEAD
    <h1>Hello world!</h1>
=======
    <h1>Hello StackOverflow!</h1>
>>>>>>> master
  </body>
</html>

You can then make changes to the file resolving the conflict.

<html>
  <body>
    <h1>Hello StackOverflow!</h1>
  </body>
</html>

You can then make a commit containing the file.

Also check out this question on SO, especially if you'd like to use tool with GUI: What's the best visual merge tool for Git?.

Community
  • 1
  • 1
Defozo
  • 2,946
  • 6
  • 32
  • 51