Search code examples
gitversion-controlgit-merge

Git Replace Files During Merge to Master From Branch


I am trying to merge a branch to master, but git says there are conflicts. Since the branch I am trying to merge to master has newer changes, I want all of that to go and replace the code in the master. Why is git complaining that there are conflicts rather than replacing the entire file in the master with the file in the branch I am trying to merge. I would like to know how I could resolve this issue and tell git to basically replace all files in master with whatever the changes that have been made in the branch I am trying to merge.


Solution

  • Git can merge content automatically without a problem. But sometimes it fails. We call this merge conflict.

    When a conflict appears Git will merge what it cans, and when it can't it will leave you a sign to tell you to merge this manually. >>> and <<< are one of those special markets.

    When a merge conflicts happen?

    Git tracks lines in file. A merge conflict happens when the exact same line is modified in separate branches. So Git will be confused wich change will keep the one from the first branch or the second and boom here a conflict appear. You can think of conflict as the poor Git get confused to understand it well :).

    What does Merge Conflict Indicators Tells?

    • <<<<<<< HEAD: All the lines between this indicator until the new one appear shows what's in the current branch.
    • ||||||| merged common ancestors: All the lines between this indicator until the new one appear shows what the original lines were.
    • ======= This is the end indicator of the original lines. All the lines between this indicator until the new one appears tells you what is on the branch that's being merged in.
    • >>>>>>> heading-update This the end indicator of the branch that's being merged in lines.

    How to resolve a conflict merge?

    To solve a conflict you need to choose which line to keep and remove the lines with indicators.

    What next?

    After removing lines with conflicts indicators + select which lines to keep. Just save the file stage it and commit it.

    And here we go! You did it. It's not that tricky ;).

    Example:

    Here what's you get in the file after conflict:

    #print hello 
    <<<<<<< HEAD 
            print("Hello")
    ||||||| merged common ancestors
            print("Hi")
    =======
            print("Holla")
    >>>>>>> heading-update
    
    #print good luck
    print("Good luck")
    

    So:

    • print("Hello"): This what is in the current branch.
    • print("Hi"): The original one.
    • print("Holla"): what is on the branch that's being merged in.

    So decide which one from those to keep and remove all others with the indicators. Or you can create a new one. In the end, we get a neat thing like this for example:

    #print hello 
    print("Ohayo :D")
    
    #print good luck
    print("Good luck")
    

    Read more here :).

    All the best :).