git merge and conflict for new developers

Git merge and conflict

Now we see that most developers feel anxiety when they have to merge the code and

If some conflicts come then it becomes worse. This dismay comes from the thought of losing the developed code or it may get corrupt. We will try to overcome this unease.

Whenever we need to take a code from another developer or need some dependent code or feature in our branch then we perform git merge. The command for git merge is

git merge

It merges the specified BRANCH_NAME in the current branch.

If there is no conflict then this merge happens without any issue.

Now problem happens when conflict happens. This conflict occurs when we try to merge a code or try to pull ( is fetch + merge) a code from the repo. Let’s try to understand this conflict first.

Conflict as we know is a dispute between two entities about certain things. This is more of a real-life meaning. If we go to our code then it goes like two code pieces try to be placed at the same place/line. This creates a conflict and this comes to a place where we need to manually resolve the conflict. For this refer to the below image.

conflict resolve

While resolving we need to decide about which code we need to place at which place? If we are merging a branch of some other developer then it becomes easy, because we can connect to that developer and discuss how those codes need to be placed. But the issue comes when we are merging the latest code and that conflicts with our developed code.

In this issue, we always have to keep the code that we are getting from the master branch and then in accordance with that, we need to add our code. While doing this how we decide which code is from master and which is from our developed code. For this, we have multiple options and different tools. Tools like WinMerge, KDiff3, Meld, Beyond Compare, etc. if you want to know about these tools comment about that tool and we will have a discussion about that tool. if you are using any IDE like visual studio code, eclipse, or visual studio then they have inbuilt features to get different differences and their management.

I normally prefer opening conflict file in notepad and then merging it. While merging you

Will get your conflict code below the pattern.

  • <<<<<<< HEAD
  • =======
  • >>>>>>> new_branch_to_merge_later

Now understand is like this

  • <<<<<<< HEAD
  • Code Here is a code from your current branch
  • ======= This is a divider of code
  • Code here is a code comping from the branch which you want to merge in your current branch
  • >>>>>>> new_branch_to_merge_later

With this, you just need to decide which code you need to keep after which, or if you want to discard any code then you can do that also.

This is not much tedious or difficult process. Just follow the steps mentioned and we can get through this. We just need to be clear about which code is our developed code and which code is coming from another branch for merging. If we refer above details then it becomes easy to understand. Still, some confusion or doubt then Do drop a comment about section or portion to discuss it.

Tejbahadur Singh

Always learner enthusiast finding solutions of technical issues that exist. Web, Blockchain, cybersecurity, web/app security researcher. I am a nerd who can be found reading books, consuming online contents and reading on internet.

Leave a Reply

Your email address will not be published. Required fields are marked *