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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-1.png" data-lazy- height="137" src="data:image/svg xml,” width=”762″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-2.png" data-lazy- height="173" src="data:image/svg xml,” width=”807″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-3.png" data-lazy- height="76" src="data:image/svg xml,” width=”807″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-4.png" data-lazy- height="244" src="data:image/svg xml,” width=”808″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-5.png" data-lazy- height="141" src="data:image/svg xml,” width=”805″>

Solve the merge conflict:

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

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-6.png" data-lazy- height="136" src="data:image/svg xml,” width=”810″>

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

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-7.png" data-lazy- height="218" src="data:image/svg xml,” width=”805″>

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

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/How-to-check-for-merge-conflict-in-Git-8.png" data-lazy- height="222" src="data:image/svg xml,” width=”811″>

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

<img alt="Fahmida Yesmin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/channel-logo-150×150.jpg609fd2163220e.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.