Git

How to check for merge conflict in Git

Multiple users may work with multiple branches in any git repository to keep track of their project tasks. Sometimes it requires merging the content of one branch to another branch. This task can be done very easily by using the `git merge` command. But the merge conflict arises when two or more git users of the same project are working on the same file and doing the different tasks on that file, such as one user is adding content to the file and another user is deleting content from the file then it is not possible for the Git to select the correct file for the update. The merge conflict may also appear where the user modifies a local repository file in multiple branches. Git will mark the file as merge conflict, and the git users have to solve this issue before continuing the work. The ways to check the merge conflict for a local repository and solve this issue have been explained in this tutorial.

Prerequisites:

Install GitHub Desktop

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a local repository

You have to create a local repository to test commands used in this tutorial for checking and solving merge conflict.

Check the merge conflict:

You can create a new local repository or any existing repository to check the commands used in this part of this tutorial. I have used an existing local repository named bash and opened the repository folder from the terminal. Run the following commands to check the existing branch list, switch to the master branch, and create a file named setup.txt by using nano editor.

$ git branch
$ git checkout master
$ nano setup.txt

The following output shows that there are three branches in the repository, and the main branch is active initially. Next, the active branch has changed to master. The nano editor will be opened after executing the ` nano setup.txt ` command.

You can add any content to the file. The following content has been added in the setup.txt file here.

Follow the instructions…

Run the following commands to add the setup.txt file in the repository, commit the task with the commit message, and check the current status of the repository.

$ git add setup.txt
$ git commit -m "setup.txt is added"
$ git status

The following output shows that one file is inserted in the repository with the commit message, and the working tree is clean now for the master branch.

Run the following commands to change the current branch to the secondary and open the nano editor to add the content for the setup.txt file that has been edited already in the master branch.

$ git checkout secondary
$ nano setup.txt

The following output will appear after executing the above command.

You can add any content to the file. The following content has been added in the setup.txt file here.

Read the instructions…

Run the following commands to add the setup.txt file in the repository, commit the task with the commit message, and check the current status of the repository.

$ git add setup.txt
$ git commit -m "setup.txt is added for the secondary branch."
$ git status

The following output shows that the setup.txt file has been added to the secondary branch of the repository.

setup.txt file has been modified in master and secondary branches. Run the following commands to switch into the master branch and merge the content of the secondary branch to the master branch.

$ git checkout master
$ git merge secondary

The following output shows that the merge conflict has appeared because the same file has been modified in both benches.

Solve the merge conflict:

Run the following command to check the content of the setup.txt file before solving the merge conflict.

$ cat setup.txt

The following output shows that the setup.txt file contains the content added in both branches with some extra symbols. The sevenless characters (<<<<<<<) with HEAD has added before the committed content of the master branch, and the seven equal sign characters (=======) has added before the committed content of the secondary branch. The seven greater than characters (>>>>>>>) has added with the secondary branch name at the end of the file. Here, the less than character indicates the current branch’s edit. The equal sign indicates the end of the first edit. The greater than character indicates the end of the second edit.

Run the following command to check the current status of the repository.

$ git status

The following output shows that you can abort the merge operation or add the file again after edit and commit the task before executing the merge command again.

Open the file in the nano editor and modify the content based on the requirement by removing all symbols.

$ nano setup.txt

The following content has been added to the file by removing all previous content here.

Read the instructions properly…

Run the following commands to add the file, check the file’s current status, and complete the merge operation.

$ git add setup.txt
$ git status
$ git commit

The following output shows that the merge conflict has been fixed, and the secondary branch has merged after executing the `git commit` command.

Conclusion:

The ways of detecting and solving the local merge conflict of the git repository have been shown in this tutorial by using a demo local repository. I hope the concept of the merge conflict will be cleared for the readers and will solve this issue after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.