0

I have a master branch where I took 2 branches and each contain a new function added in the same file at same line

function test1() {
    console.log("test")
}

function test2() {
    console.log("test")
}

file example

and now when merging the 2 changes git will say there is conflict in the name of the function

and I want git 2 add the 2 function above each other like this: needed example

but what actually happens when I try to take the 2 changes: what happens when merging

  • Git uses the concept of hunk for the diff between files. Basically it divides your code into chunks and detects those that are equal and those that have been added/deleted. In your case the file is divided into 2 hunks: the first one is the first line, which is different in your branches, and the second one is the rest, which is the other 2 lines and are the same in your branches. – Marco Luzzara Feb 08 '21 at 12:27

2 Answers2

0

Well what you stumbled upon is completely natural and is called conflict. Conflicts are not resolved automatically, you need to do it manually. Set your mergtool to VS Code (or any good code editor that can handle git operations like merging) and run git mergetool - it should nicely mark what differs and it is UP TO YOU how to handle that.

Any attempt to automatically merge conflicts will result in unexpected output, as you can see on your own example.

Automatical conflicts resolving is rare and should be used with care (for example git rerere).

EDIT

In order to set VS Code as git editor (mergetool or difftool) follow links in the description:

Using VSCode as git mergetool and difftool

How to use Visual Studio Code as the default editor for Git MergeTool

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Thank you first, I am searching for an option that allows me to merge the conflicts manually in vscode where the whole change/commit is marked and I can copy it all and as the example adding the 2 changes above each others, like a vscode extension or something similar – Ahmad Amr Ebeid Feb 09 '21 at 08:06
  • @AhmadAmrEbeid See my edit, I posted resources describing how to do that :) Also, if my answer has helped you you should mark it as accepted (green check mark on the left) and optionally upvote. – Michał Turczyn Feb 09 '21 at 08:12
  • I am already using VScode to resolve the conflicts, my problem is that I want a tool that can allow easier selection of the whole change so I can copy it whole and added say under the other change, not the conflict only. – Ahmad Amr Ebeid Feb 09 '21 at 08:24
  • @AhmadAmrEbeid But is VS code your mergetool in GIT? What happens if you run `git mergetool` in a console? – Michał Turczyn Feb 09 '21 at 08:25
  • If I click on accept both changes, will that affect the whole repo or will it my change work like it will put my work in the next line after I git pull ?? – Dhanjay Bhardwaj Feb 09 '21 at 10:35
0

Well, everything is normal.

Let's take a look at how conflict occurs. It is not that simple but let it be simle:

  1. If you change same line in 2 different branches, git asks you to manually solve the conflict because git can not decide which one to select. It is up to you to how to solve conflicts.

  2. If you change same line in 2 different branches but the changes are exactly the same, git doesn't create a conflict. Nothing to solve, they are same.

According to this information, let's analyze your question.

Both branches have changed 1th, 2nd and 3rd lines. However, 2nd and 3rd lines are changed exactly the same. So git will say "There is a conflict only at 1st line".

If you want to solve the conflict as you wanted, you just manually add 2nd and 3rd lines after solving conflict before commiting. Git stays edit state while solving conflicts. You can change the file whatever you want.

Prihex
  • 342
  • 2
  • 11