-1

I'm learning Git and I don't understand why if I do git pull from the same branch I don't have conflicts, but if I do git pull origin other-branch I have conflicts.

For example: case1: I have branch1 with a txt file and I have character "12", then I push it to github, then I modify with "13" on remote. Then I do git pull and everything is fine I don't have conflicts.

case2: I have the same branch1 and a txt file with "12", then I push it to github. After that I create another branch branch2 from branch1, and I modify "12" with "13", then push to remote to branch2. Then I go back to branch1 and do git pull origin branch2. This time I have conflicts like this:

Auto-merging bcde.txt
CONFLICT (content): Merge conflict in bcde.txt
Automatic merge failed; fix conflicts and then commit the result.
[pc@pc-PC bca (master *+|MERGING)]$ cat bcde.txt
<<<<<<< HEAD
12
=======
13

So I have "12" and pull "13" from same branch everything is fine, no conflicts, but if I have "12" and pull "13" from other branch this time I have conflicts. Can somebody explain me why I have conflicts?

Thank you!

elvis
  • 956
  • 9
  • 33
  • 56
  • `git pull` means *run two Git commands: first, `git fetch`, then a second one*. The default second one is `git merge` and `git merge` can have merge conflicts. It's not a matter of *branches* per se, it's a matter of the operation of the merge. – torek Dec 07 '21 at 14:17
  • That said, if you have only one branch name involved, you'll *often* be setting things up such that the merge in question has no conflicts. The tricky part here is that despite using one name `branch1`, you can in fact have two different branches, because you're using two different repositories: one locally on your laptop (or whatever computer), and one over on GitHub. Each one has *its own* branch names. – torek Dec 07 '21 at 14:20

1 Answers1

3

When you are comparing changes in the same branch git is able to see which one is newer and automatically overwrite the old one. It assumes that the code has just been updated to the newer version. When it comes to separate branches though, git thinks that the second branch could have been worked on in parallel to the first one and sees that a change has been made on the same line. When these two branches are then merged by you doing git pull origin branch2, git is not sure which branch is correct and so creates a conflict. This allows the user to decide which is the correct version.

Joshua Zeltser
  • 488
  • 2
  • 9