Search code examples
gitgit-mergegit-merge-conflict

git practice exercise ( merging two branch and dealing with conflict)


I have to clone this repo : https://github.com/glo2003/H21-tp1-git and integrate both branches which are :

refactor/structure and fix/ingredients to the main branch

Main currently looks like this :

Framboise
Banane
Champignon

refactor/structure has a new folder called listes with the file liste.txt in it ( so the file structure is listes\listes.txt) :

- Framboise
- Banane
- Champignon

fix/ingredients :

Framboise
Banane
Pommes

The end result wanted is this one (so champignon is replaced by Pommes and the list has a new format with the ' - ' also the folder structure will be listes\listes.txt

- Framboise
- Banane
- Pommes

What i tried :

git clone https://github.com/glo2003/H21-tp1-git.git

git merge fix/ingredients

git merge refactor/structure

And i did git status to check to situation :

enter image description here

It looks like a listes.txt got deleted but not sure.

From there i wanted to fix the conflict, with vi listes.txt or vi listes\listes.txt but i don't see any Head tag for the conflict so i am a bit confused ? What am i doing wrong. Thanks a lot.


Solution

  • This is a file level conflict, not a textual conflict. To fix it, you manipulate the index: you say git add to preserve a file and git rm to remove a file. Then say git commit to make it so.

    For example, if you like what the restructure branch did, you can say

    $ git rm liste.txt
    $ git add listes/liste.txt
    $ git commit -m"finish merge"
    

    But then you have also accepted the contents of that file. If you wanted the contents to match main, you'd need to copy what's in the top level liste.txt into what's in listes/liste.txt before doing those file level things.


    In the comments, you've basically said you wish this were a text-level conflict so you could resolve it at text level. To make that so, you need to:

    1. Abort the merge.

    2. Rearrange the file structure on main to match the file structure on the branch, and add and commit.

    3. Now perform the merge, specifying it this way:

       git merge -Srecursive -Xno-renames refactor/structure
      

    This will give you the moved liste.txt as a text conflict that you can proceed to resolve in vi or whatever.

    So, here's how I solved it, from the start:

    $ git clone git@github.com:glo2003/H21-tp1-git.git
    $ cd H21-tp1-git/
    # create local branches
    $ git checkout fix/ingredients
    $ git checkout refactor/structure
    $ git checkout main
    $ git merge fix/ingredients
    $ git merge refactor/structure
    # file-level conflict, abort
    $ git merge --abort
    # mimic "theirs" file structure, and merge
    $ mkdir listes
    $ git mv liste.txt listes/liste.txt
    $ git add .
    $ git merge -Srecursive -Xno-renames refactor/structure
    # now we have the text level conflict you wanted, as we can see:
    $ cat listes/liste.txt
    <<<<<<< HEAD
    Framboise
    Banane
    Pommes
    =======
    - Framboise
    - Banane
    - Champignon
    >>>>>>> refactor/structure