Home > Software engineering >  Git merge between master branch and outdated but with news lines feature branch, merge result wrong
Git merge between master branch and outdated but with news lines feature branch, merge result wrong

Time:02-19

Main branch:

-test.txt
-A
-B
-C
-D

Feature branch: branch out from main, and adds -E in a new line

In the meantime, line -C is deleted on main branch.

Now if I want to do a pull request, first i merge current main branch to my feature branch. However, in the merge conflicts 1, i can only either choose the result to be ABD(main branch) or ABCDE(feature).

I expect the result to look like "ABDE". (update from main branch, but also add the new line E from feature branch)

What do i need to do in order to achieve the expect result? Thanks in advance

CodePudding user response:

I expect the result to look like "ABDE". What do i need to do in order to achieve the expect result?

Edit the file to look like "ABDE", add the file, and commit.

CodePudding user response:

Merge-conflict from start to finish

Setup

I created the following git alias for easier display. (All lines beginning with a $ are entered in the terminal:

$ git config --global --add alias.lx "log --all --decorate --oneline --graph"

Init ABCD commit

$ mkdir test && cd test
$ echo -e 'A\nB\nC\nD' > test.txt
$ git init
$ git add test.txt
$ git commit -m "Initial ABCD commit"

Feature branch

$ git checkout -b feature
$ echo 'E' >> test.txt
$ git commit -am "Add feature E"

Back on main

$ git checkout main
$ echo -e 'A\nB\nD' > test.txt
$ git commit -am "Remove feature C"

The conflict (?)

Here on git --version 2.35.1, I cannot reproduce your conflict:

$ git merge feature
Auto-merging test.txt
Merge made by the 'ort' strategy.
 test.txt | 1  
 1 file changed, 1 insertion( )

$ cat test.txt 
A
B
D
E

$ git lx
* 248cb4c (HEAD -> main) Merge branch 'feature' into main
|\
* | 64d82c9  Remove feature C
| * c8038f5 (feature) Add feature E
|/  
* fb88cb0 Initial ABCD commit

So in this simple case, Git is smart enough to figure a good way out how to combine both changes.

A conflict (for real)

So let's make it more difficult; first, let's first undo the merge on branch main by resetting it to the commit before the merge. Either specify the commit ID by hand, or use HEAD^. (If you are unsure which commit this refers to, simply git show HEAD^ and confirm it is "Remove feature C".

$ git show HEAD^
$ git reset --hard HEAD^
$ git lx
* 64d82c9 (HEAD -> main) Remove feature C
| * c8038f5 (feature) Add feature E
|/  
* fb88cb0 Initial ABCD commit

Create a more difficult change on main:

$ echo 'FE' >> test.txt
$ git commit -am "Add FE"

And now, finally, a merge-conflict:

$ git merge feature
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

$ cat test.txt
A
B
D
<<<<<<< HEAD
FE
=======
E
>>>>>>> feature

Git stops any attempt to do the thinking for you. Helpful command git status advises you how to proceed: you can either

  • "fix conflicts and run git commit" or
  • take a step back and "use git merge --abort to abort the merge"

The resolution

The step "fix conflicts" is simply you opening test.txt in a text editor and figuring out, how you want test.txt to look in the end. Only you can decide what the correct resolution for the above changes are. Simply remove all conflict markers and leave the file tidy. For example, I chose the following resolution state for test.txt:

A
B
D
E
FE (separate from E!)

The explanatory text was introduced because of the confusion E next to FE might cause. This is to highlight that you may perform any edit you seem fit which "resolves the conflict".

Once you are done, look at git status for instructions: now that you have modified test.txt, "git add <file> to mark resolution" appears, so we do that (and git status then advises us to use commit to conclude the merge):

$ git add test.txt
$ git commit -m "Complicated merge of 'feature' into main"
$ git lx
*   d12730f (HEAD -> main) Complicated merge of 'feature' into main
|\  
| * c8038f5 (feature) Add feature E
* | 864eea7 Add FE
* | 64d82c9 Remove feature C
|/  
* fb88cb0 Initial ABCD commit

  • Related