BreakingExpress

The best way to resolve Git merge conflicts

Don’t panic while you encounter a merge battle. With a bit skilled negotiation, you possibly can resolve any battle.

Suppose you and I are engaged on the identical file known as index.html. I make some modifications to the file, commit them, and push the modifications to the distant Git repository. You additionally make some modifications to the identical file, commit them, and begin pushing the modifications to the identical Git repository. However, Git detects a battle as a result of the modifications you made battle with the modifications I made.

Here’s how one can resolve the battle:

  1. Fetch and merge the newest modifications from the distant repository:

    $ git pull
  2. Identify the a number of conflicting information:

    $ git standing
  3. Open the conflicting file utilizing a textual content editor:

    $ vim index.html
  4. Resolve the battle. The conflicting modifications are marked by <<<<<<< HEAD and >>>>>>>. You want to decide on which modifications to maintain and which to discard. Manually edit the file to mix the conflicting modifications. 

    Here’s an instance:

    <<<<<<< HEAD
    <div class="header">
    <h1>Sample textual content 1</h1>
    </div>
    =======
    <div class="header">
    <h1>Sample textual content 2</h1>
    </div>
    >>>>>>> feature-branch

    In this instance, I modified the web site heading to Sample textual content 1, whilst you have modified the heading to Sample textual content 2. Both modifications have been added to the file. You can now resolve which heading to maintain or edit the file to mix the modifications. In both case, take away the markers that point out the start and finish of the modifications, leaving solely the code you need:

    <div class="header">
    <h1>Sample textual content 2</h1>
    </div>
  5. Save your entire modifications, and shut your editor.

  6. Add the file to the staging space:

    $ git add index.html
  7. Commit the modifications:

    $ git commit -m "Updated h1 in index.html"

    This command commits the modifications with the message Resolved merge battle.

  8. Push the modifications to the distant repository:

    $ git push

Resolution

 Merge conflicts are a great cause to focus your modifications on code. The extra you modify in a file, the larger the potential for battle. You ought to make extra commits with fewer modifications every. You ought to keep away from making a monolithic change that mixes a number of characteristic enhancements or bug fixes into one. Your mission supervisor will thanks, too, as a result of commits with clear intent are simpler to trace. A Git merge battle could appear intimidating at first, however now that you understand how to do it, you will see that it is simply resolved.

Agil has greater than 6 years of expertise as a technical author that specialises in producing correct, clear, and concise documentation for software program merchandise. Agil has the power to speak technical concepts to a wide range of audiences, together with builders, engineers, and finish customers.


This work is licensed beneath a Creative Commons Attribution-Share Alike 4.0 International License.
Exit mobile version