#15 Git: merging conflicts in git

In the previous post, we have seen what is branching and why it is important. In this post, we will see one major concept of git branching i.e. merging conflicts. Merging conflicts arise when we try to merge two branches having the same changes. For example, John is working on some files and another developer Jay is also working on the same file, so when one of them will try to merge the code, git will get confused about which code to merge and that’s when merging conflicts arise. So we will see how to avoid these merging conflicts and if arises, then how to solve them.

Why Merging conflicts arises?

As I stated above merging conflicts arises when we try to merge the two working tree for one file. Suppose Developer A develops a User Interface for a website and Developer B is also working on the same files and when they’ll try to merge their code there will be merging conflicts. Sometimes these conflicts are tedious and time-consuming, so it is better to avoid them.

How can we avoid conflicts?

Since conflicts are time-consuming and hectic people try to avoid them. We can avoid them by keeping one rule in mind i.e. we should not touch other developer’s files. For example, If John is working on index.html then no other than John should touch that file, if somebody thinks that something wrong in index.html then that person should report to John to make appropriate changes. This is how we can lower the possibilities of arising conflicts.

Now, we will see one basic example of resolving merge conflicts. I’ll be using VS code for editing if you have not downloaded and installed it you can download it from here.

So, I made one change in master branch only. The change is that I have added bootstrap navbar. After that I staged the changes and committed them.

After adding bootstrap code my website will look something like this with that navbar.

Now, suppose I want to make changes in this navbar, but I am not sure whether my project manager will like it or not. So to do that what I will do is, I’ll create one branch and I will try my changes on that branch only. I will not touch this master branch. If my manager likes the changes on the navbar, then with permission I will merge that branch into the master branch.

So as we know to create a new branch we use the following command:

git checkout -b branch_name

Above you can see the branch name(in blue) in front of the prompt after executing the command. It got changed to navbar-new-feature from master. Now, this branch also has the same code as the master branch.

Below as you can see I made some changes in navbar of navbar-new-feature branch and committed the changes.

Navbar in navbar-new-feature branch

Now I changed the branch to master using git checkout command

Here, I made some changes in navbar and committed the changes.

navbar in master branch

You can see the slight difference between the navbar of branch master and the navbar-new-feature. Now I’ll again switch back to the navbar-new-feature branch and try to commit some changes to raise conflicts.

navbar in navbar-new-feature branch

Now, as you can see I made some changes in the navbar-new-feature branch. Don’t worry about the changes I made. I made them to just show you the merging of conflicts. Now I’ll switch back to the master branch and I’ll try to merge the navbar-new-feature branch in master.

It is showing Merge conflicts in index.html. Now here VS code editor will simplify our work. We will take a look at it, what it shows after executing this command.

Here as you can see there are conflicts which are enclosed in <<<<<<< HEAD(Current Change) and >>>>>> navbar-new-feature(Incoming Change). So there is code from the master branch which is after <<<<<<<HEAD this, and there is code from merging branch i.e. navbar-new-feature before >>>>>> navbar-new-feature this. and both the changes are separated by hyphens(————).

Above each conflict there are four options Accept Current Changes, Accept Incoming Changes, Accept Both Changes, and Compare Changes. If we click on Accept Current Changes then-current changes will be kept and Incoming changes will be discarded. Same for the Accept Incoming Changes and If you Accept Both Changes then both the changes will be kept. And if you click on Compare Changes then changes will be compared.

As you can see in the above images I have selected Accept Current Changes for the first change. And I’ll choose to Accept Incoming Changes for the second change since I want the changes from the navbar-new-feature branch.

Above image show the complete merging of two branches. But our work is not over here, we need to commit the changes.

In above image I have committed the changes, after this commit the branches are merged.

This is the final output that we get after merging the master and navbar-new-feature branches. This is a simple example which I have shown you. It is not necessary that conflicts will be this much easier. They can be more tedious and time-consuming to solve. Also, they can arise due to multiple people handling the same file.

Now our merging is successful, I want to introduce some commands

Show last commit of each branch

There is one command which shows the last commits happened in each branch.

git branch -v

As you can see there are multiple branches and we are able to see the last commit’s hash and message.

Listing merge and unmerged branches

If the project is very big, then it is not easy to remember which branch is merged and which is not. So there are two commands which let us know which branches are merged and which are not.

git branch --merged

So as you can see above, command returned the list of branches which are merged.

git branch --no-merged

In above image we can see the branch which is not merged.

Deleting the branch

Git provides the commands to delete the branches.

git branch -d branch_name

Using this command we can only delete the branches which are merged. If we try to delete the branch which is not merged then it will throw an error.

In above image Bootstrap-Ui branch is deleted because it is a merged branch.

When we try to delete the unmerged branch with above command see what happens

Here we are getting errors when we try to delete the unmerged branch. Still, if you want to delete this branch then there is one command which lets us delete this branch. But it will erase the data.

git branch -D branch_name

So as you can see Css-Ui branch is deleted because of this command. You can also delete merged branches with this command. This is called forced deletion of git branches.

Leave a Comment

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