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:
-
Fetch and merge the newest modifications from the distant repository:
$ git pull
-
Identify the a number of conflicting information:
$ git standing
-
Open the conflicting file utilizing a textual content editor:
$ vim index.html
-
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 toSample 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>
-
Save your entire modifications, and shut your editor.
-
Add the file to the staging space:
$ git add index.html
-
Commit the modifications:
$ git commit -m "Updated h1 in index.html"
This command commits the modifications with the message
Resolved merge battle
. -
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.