Resolve Merge Conflict on Git Instanly

Irwan Syarifudin
2 min readOct 26, 2021

--

Have you ever had a conflict when merging your branch with the master branch?. Please don’t merge with git push — force 🙅🏻‍♂️

Now this post will help you to resolve conflict on your local git. We will switch over to “master” and merge “<your_branch>”. Example your branch is add-new-scenario. And for you console lovers, immediately follow the following steps:

Get the latest source from master

$ git checkout master$ git pull origin master

Merge it

$ git checkout add-new-scenario$ git merge origin masterAuto-merging features/step_definitions/cobain_steps.rbCONFLICT (content): Merge conflict in features/cobain.feature

Don’t panic! Git has identified a conflict and will not allow you to automatically merge with your branch “add-new-scenario” with “master”. The conflict here pertains to the same line on sales_invoice.feature with different content on both branches. And next:

Fix the conflict code

Open cobain.feature file. Find the line that has the conflict

Scenario Outline: User not able cobain daftarExamples:| case_id | field | error_type |<<<<<<< HEAD| 64079 | phone | empty |========================| 12121 | name | emoji |>>>>>>> master

The two branch codes are separated by ======. Now our job is to fix it. Please eliminate one of these codes. Remove one of change code manually and clean up <<<<<<< HEAD, >>>>>>> master, and ======. It might look be a little troublesome. But, enjoy …

For those of you who use visual studio code and has already integration with git tools. You can choose instantly to fix that with select current or incoming or both code changes.

There are 3 provisions for you fix it:

  • If you select accept current change, its mean would ignore completely what you merge, and keep what you had. The result is you keep the code you have (case_id → 64078).
  • If you select accept incoming change, its mean would ignore completely what you had, and keep what you merge. The result is you keep the code you have (case_id → 12121).
  • If you select both changes, its mean what you merge and what you had. The result is the both changes code go into your code (case_id 12121 and 64078).

Finishing

Once you are done with resolving the conflict code, let’s commit the change.

$ git add features/cobain.feature$ git commit -m "fix conflict"

A new commit has been created as a result of the conflict resolution. Let’s push to ship it to make a PR.

$ git push -u origin add-new-scenario

Good luck !

--

--