{"id":8641,"date":"2024-01-10T00:14:17","date_gmt":"2024-01-10T00:14:17","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/steps-to-resolve-merge-conflicts-in-git-dealing-with-modify-delete-issues\/"},"modified":"2025-01-20T11:36:54","modified_gmt":"2025-01-20T11:36:54","slug":"steps-to-resolve-merge-conflicts-in-git-dealing-with-modify-delete-issues","status":"publish","type":"post","link":"https:\/\/cheapwindowsvps.com\/blog\/steps-to-resolve-merge-conflicts-in-git-dealing-with-modify-delete-issues\/","title":{"rendered":"Steps to Resolve Merge Conflicts in Git: Dealing with Modify\/Delete Issues"},"content":{"rendered":"<div>In my previous article, I discussed merge conflicts, specifically <a href=\"https:\/\/4sysops.com\/archives\/resolve-content-line-merge-conflicts-in-github\/\" target=\"_blank\" rel=\"nofollow noopener\">line merge conflicts, and how to resolve them on GitHub<\/a>. In this article, I will focus on another type of merge conflict, the modify\/delete conflict, and how to resolve it using Git.<\/div>\n<p>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 <a href=\"https:\/\/4sysops.com\/archives\/a-git-merge-example\/\" target=\"_blank\" rel=\"nofollow noopener\">perform an automatic merge<\/a> without human intervention.<\/p>\n<p>If you are new to Git, be sure to read the previous guides in <a href=\"https:\/\/4sysops.com\/archives\/a-gitgithub-beginner-tutorial\/\" target=\"_blank\" rel=\"nofollow noopener\">this Git beginner series<\/a>.<\/p>\n<h2>Resolve modify\/delete merge conflicts in Git<\/h2>\n<p>Firstly, open the repository you built in the <a href=\"https:\/\/4sysops.com\/archives\/resolve-content-line-merge-conflicts-in-github\/\" target=\"_blank\" rel=\"nofollow noopener\">previous article<\/a>. To accompany this article, you may open <a href=\"https:\/\/github.com\/Gboom345\/gitme.git\" target=\"_blank\" rel=\"nofollow noopener\">my replica of the repository<\/a>.<\/p>\n<p>In the repository, there are three branches: main, <em>conf-1<\/em>, and <em>conf2<\/em>. Our intention is to trigger a merge conflict by merging <em>conf-2<\/em> into <em>main<\/em>.<\/p>\n<p>The only thing needed is the Git URL for later use in the terminal. Under the <strong>Code<\/strong> tab, click the <strong>Code<\/strong> button to display the URL.<\/p>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Viewing-the-repository-URL.png\" target=\"_blank\" rel=\"nofollow noopener\">Viewing the repository URL<\/a><\/div>\n<p>Copy the repo URL to the clipboard.<\/p>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Copying-the-repository-URL.png\" target=\"_blank\" rel=\"nofollow noopener\">Copying the repository URL<\/a><\/div>\n<p>You can now open up a terminal window. Clone the repo to a folder on your local machine as follows.<\/p>\n<p>&lt;p&gt;git clone &lt;a href=&#8221;url-of-repo&#8221; target=&#8221;_blank&#8221; rel=&#8221;nofollow&#8221;&gt;url-of-repo&lt;\/a&gt;&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;Cloning the demo repository&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;Go into the repo (it should be called &lt;em&gt;gitme&lt;\/em&gt;) with the &lt;em&gt;cd&lt;\/em&gt; command:&lt;\/p&gt;<\/p>\n<p>&lt;p&gt;cd gitme&lt;\/p&gt;<\/p>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Moving-into-the-local-repository.png\" target=\"_blank\" rel=\"nofollow noopener\">Moving into the local repository<\/a><\/div>\n<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.<\/p>\n<pre>git branch<\/pre>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Listing-local-branches.png\" target=\"_blank\" rel=\"nofollow noopener\">Listing local branches<\/a><\/div>\n<p>The asterisk (*) next to <em>main<\/em> 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 <em>conf-2<\/em> branch using the <em>git checkout<\/em> command, as demonstrated below:<\/p>\n<pre><\/pre>\n<p>git checkout conf-2<\/p>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Checking-out-conf-2.png\" target=\"_blank\" rel=\"nofollow noopener\">Checking out conf-2<\/a><\/p>\n<p>By doing this, you can verify that an additional branch has been added to your local repository:<\/p>\n<p>git branch<\/p>\n<p>With our local repo ready, configure your email and username now to enable you to make commits where necessary.<\/p>\n<p>git config user.name &#8220;YourUserName&#8221;<\/p>\n<p>git config user.email &#8220;YourName@YourMail.com&#8221;<\/p>\n<p>List the files in the repo. You should see two files, <em>check-system.py<\/em> which we used in the previous article, and <em>check-time.py<\/em>.<\/p>\n<pre>ls<\/pre>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Listing-the-contents-of-a-branch.png\" target=\"_blank\" rel=\"nofollow noopener\">Listing the contents of a branch<\/a><\/p>\n<p>To create a modify\/delete conflict, I have already committed modifications to <em>check-time.py<\/em> to the <em>main<\/em> branch. These modifications were made after the creation of the <em>conf-2<\/em> branch, making them an outstanding change for <em>conf-2<\/em>.<\/p>\n<p>Delete <em>check-time.py<\/em> from the conf-2 branch as follows<\/p>\n<pre>rm check-time.py<\/pre>\n<p>This sets up a conflict in <em>check-time.py<\/em>: in the <em>main<\/em> branch, the file&#8217;s contents have been modified, while in the <em>conf-2<\/em> branch, the file has been completely deleted. We&#8217;re now on the verge of encountering a modify\/delete conflict between these two branches.<\/p>\n<p>Add (-<em>a<\/em>) and commit the changes with a message (<em>m<\/em>) to the <em>conf-2 <\/em>branch.<\/p>\n<pre>git commit -am \"delete check-time.py\"<\/pre>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Committing-changes.png\" target=\"_blank\" rel=\"nofollow noopener\">Committing changes<\/a><\/p>\n<p>You can now switch to <em>main <\/em>as shown below.<\/p>\n<pre>git checkout main<\/pre>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Switching-branches-to-main.png\" target=\"_blank\" rel=\"nofollow noopener\">Switching branches to main<\/a><\/div>\n<p>Switching branches to main<\/p>\n<p>It is time to attempt to merge in the changes from <em>conf-2<\/em> into <em>main<\/em>. Merge in the changes from <em>conf-2<\/em> with the <em>git merge<\/em> command.<\/p>\n<pre>git merge conf-2<\/pre>\n<p>You should receive notice of a <strong>modify\/delete<\/strong> merge conflict.<\/p>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/A-modify-delete-conflict.png\" target=\"_blank\" rel=\"nofollow noopener\">A modify-delete conflict<\/a><\/div>\n<p>A modify-delete conflict<\/p>\n<p>You can run git status to get more details on the conflict and possible resolution steps.<\/p>\n<pre>git status<\/pre>\n<div><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Viewing-Git-status-after-a-merge-conflict.png\" target=\"_blank\" rel=\"nofollow noopener\">Viewing Git status after a merge conflict<\/a><\/div>\n<p>Viewing Git status after a merge conflict<\/p>\n<p>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 <em>git add check-system.py<\/em>. If you decide to remove the file, run <em>git rm<\/em> to remove the file from the resulting commit. This also marks the conflict as resolved.<\/p>\n<p>For this tutorial, remove the file:<\/p>\n<pre>git rm check-time.py<\/pre>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Resolving-a-modify-delete-conflict.png\" target=\"_blank\" rel=\"nofollow noopener\">Resolving a modify-delete conflict<\/a><\/p>\n<p>Commit the resolution with an appropriate message to finalize the process and complete the merge operation.<\/p>\n<pre>git commit -m \"merge conf-2. rm check-time\"<\/pre>\n<p><a href=\"https:\/\/4sysops.com\/wp-content\/uploads\/2024\/01\/Committing-the-conflict-resolution.png\" target=\"_blank\" rel=\"nofollow noopener\">Committing the conflict resolution<\/a><\/p>\n<h2>Tips to avoid merge conflicts<\/h2>\n<p>To avoid merge conflicts, follow these best practices:<\/p>\n<p><strong>Use separate files for distinct features<\/strong> \u2013 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.<\/p>\n<p><strong>Merge as soon as possible<\/strong> \u2013 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.<\/p>\n<p><strong>Pull before you push or merge<\/strong> \u2013 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 <em>git fetch<\/em> and <em>pull<\/em> 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.<\/p>\n<p><strong>Make modular commits<\/strong>\u2013 It&#8217;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&#8217;s always better to keep your commits focused and easy to understand.<\/p>\n<h2>Subscribe to 4sysops newsletter!<\/h2>\n<h2>Conclusion<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>In my previous article, I discussed merge conflicts, specifically line merge conflicts, and how to resolve them on GitHub. In this article, I will focus on another type of merge conflict, the modify\/delete conflict, and how to resolve it using Git. Modify\/delete conflicts occur when, on one branch in the merge operation, a file has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8642,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,95,96],"tags":[],"class_list":["post-8641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-articles","category-devops","category-git"],"_links":{"self":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=8641"}],"version-history":[{"count":1,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8641\/revisions"}],"predecessor-version":[{"id":10457,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/8641\/revisions\/10457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/8642"}],"wp:attachment":[{"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=8641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=8641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=8641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}