Modify/delete conflicts occur when, on one branch in the merge operation, a file has been modified. At the same time, on the other branch, the same file has been deleted, making it impossible for the system to perform an automatic merge without human intervention.
If you are new to Git, be sure to read the previous guides in this Git beginner series.
Resolve modify/delete merge conflicts in Git
Firstly, open the repository you built in the previous article. To accompany this article, you may open my replica of the repository.
In the repository, there are three branches: main, conf-1, and conf2. Our intention is to trigger a merge conflict by merging conf-2 into main.
The only thing needed is the Git URL for later use in the terminal. Under the Code tab, click the Code button to display the URL.
Copy the repo URL to the clipboard.
You can now open up a terminal window. Clone the repo to a folder on your local machine as follows.
<p>git clone <a href=”url-of-repo” target=”_blank” rel=”nofollow”>url-of-repo</a></p>
<p>Cloning the demo repository</p>
<p>Go into the repo (it should be called <em>gitme</em>) with the <em>cd</em> command:</p>
<p>cd gitme</p>
At this point, only the main branch should be checked out (currently selected for work, like checking out a book from a library). You can confirm this by running git branch to see how many local branches exist.
git branch
The asterisk (*) next to main shows that this is the branch currently checked-out. When cloning a repository, the files in the remote-tracking branches are not immediately duplicated into your working directory. However, the full history of the repository, including all branches, is preserved in your local .git directory. Create a local version of the conf-2 branch using the git checkout command, as demonstrated below:
git checkout conf-2
By doing this, you can verify that an additional branch has been added to your local repository:
git branch
With our local repo ready, configure your email and username now to enable you to make commits where necessary.
git config user.name “YourUserName”
git config user.email “[email protected]”
List the files in the repo. You should see two files, check-system.py which we used in the previous article, and check-time.py.
ls
Listing the contents of a branch
To create a modify/delete conflict, I have already committed modifications to check-time.py to the main branch. These modifications were made after the creation of the conf-2 branch, making them an outstanding change for conf-2.
Delete check-time.py from the conf-2 branch as follows
rm check-time.py
This sets up a conflict in check-time.py: in the main branch, the file’s contents have been modified, while in the conf-2 branch, the file has been completely deleted. We’re now on the verge of encountering a modify/delete conflict between these two branches.
Add (-a) and commit the changes with a message (m) to the conf-2 branch.
git commit -am "delete check-time.py"
You can now switch to main as shown below.
git checkout main
Switching branches to main
It is time to attempt to merge in the changes from conf-2 into main. Merge in the changes from conf-2 with the git merge command.
git merge conf-2
You should receive notice of a modify/delete merge conflict.
A modify-delete conflict
You can run git status to get more details on the conflict and possible resolution steps.
git status
Viewing Git status after a merge conflict
To resolve this kind of conflict, you need to decide whether you want to keep the conflicted file. If you decide to keep the file, run git add check-system.py. If you decide to remove the file, run git rm to remove the file from the resulting commit. This also marks the conflict as resolved.
For this tutorial, remove the file:
git rm check-time.py
Resolving a modify-delete conflict
Commit the resolution with an appropriate message to finalize the process and complete the merge operation.
git commit -m "merge conf-2. rm check-time"
Committing the conflict resolution
Tips to avoid merge conflicts
To avoid merge conflicts, follow these best practices:
Use separate files for distinct features – By dividing the different aspects of your project into separate files whenever possible, the risks of having multiple collaborators editing the same file at the same time decrease, and as a result, the potential for conflicts during merges is minimized.
Merge as soon as possible – Postponing a merge increases the chances of divergence between two branches, leading to a higher risk of merge conflicts as there are more areas that could possibly conflict.
Pull before you push or merge – Pulling in changes that may have been made to the receiving branch before you try to merge is another way to avoid conflicts. By regularly using the git fetch and pull commands, you get an idea of what your collaborators are doing and can anticipate and react to potential conflicts locally, before you perform the merge.
Make modular commits– It’s best to make each commit cover only one logical change. Combining many logical changes into one large commit can lead to conflicts and errors. Large commits also make it harder for other collaborators to review your work properly, which means you might miss out on valuable feedback and discussions. Remember, the more difficult it is to review a commit, the greater the chances of problems in the future. So, it’s always better to keep your commits focused and easy to understand.