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 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 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.
$ 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 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 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.
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.
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.
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 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.