Merging Git conflicts with ease

Now let's look at another improvement that we can bring to git. Most tasks are easy to execute from the command line, but some tasks, such as merging, require a specialist's eye for understanding the different format.

Let's open the feature file from our previous chapter, edit it, add a new line, and save it:

git diff
Merging Git conflicts with ease

The git diff command will show us colored text explaining the differences between the git file and the modified file, but some people find this format hard to understand:

Merging Git conflicts with ease

Luckily, we can tell git to use external tools when it comes to merge and one external tool that we can use is called Meld. Let's install it using the following:

sudo apt install meld
Merging Git conflicts with ease

After this, we can run the following command:

git difftool
Merging Git conflicts with ease

It will ask us if we want to launch Meld as an external program for viewing the file. It's also giving us a list of tools that it can use for displaying the difference. Hit y to open Meld:

Merging Git conflicts with ease

Now we can easily see the two files side by side and the differences between them. We can see that 1 has been changed to 2 and a new line has been added. Based on this output we can easily decide if we want to add it or not. Let's commit the file as it is.

Next, we will look at merge conflicts. Let's manually create a branch called test and edit the same file, commit it, and then switch back to the develop branch. Let's update the same file, commit it, and then try to merge the test branch: and, of course, there is a merge conflict.

For resolving the conflict, we will be using the following command:

git mergetool
Merging Git conflicts with ease
Merging Git conflicts with ease

Again, it offers to open Meld. In Meld we can see the three files:

  • On the left is the file from our current branch
  • On the right is the file from the remote branch
  • In the middle is the resulting file that will be created

Let's say that we decide the correct version for the feature is 4 and that we also want to add of text:

git commit -a
Merging Git conflicts with ease

You can see the predefined commit message. Don't forget to remove the temporary file that was created at the merge:

Merging Git conflicts with ease

In general, most modern IDEs offer plugins for working with git, including merging and diffs. We recommend that you get more acquainted with the command-line tools, because then you don't need to learn a new git plugin when switching from one IDE to another.

The git command works the same way across Linux, Mac, and Windows. It is a tool that developers use a lot and being fluent in it will boost your productivity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.190