{"id":72433,"date":"2023-04-04T09:07:31","date_gmt":"2023-04-04T09:07:31","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=72433"},"modified":"2023-04-04T09:07:31","modified_gmt":"2023-04-04T09:07:31","slug":"how-to-resolve-git-merge-conflicts","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=72433","title":{"rendered":"How to resolve Git merge conflicts"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How to resolve Git merge conflicts<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/agantony\" class=\"username\">agantony<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Tue, 04\/04\/2023 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Suppose you and I are working on the same file called <strong>index.html<\/strong>. I make some changes to the file, commit them, and push the changes to the remote Git repository. You also make some changes to the same file, commit them, and start pushing the changes to the same Git repository. However, Git detects a conflict because the changes you made conflict with the changes I made.<\/p>\n<p>Here&#8217;s how you can resolve the conflict:<\/p>\n<ol type=\"1\">\n<li>\n<p>Fetch and merge the latest changes from the remote repository:<\/p>\n<pre>\n<code>$ git pull<\/code><\/pre>\n<\/li>\n<li>\n<p>Identify the one or more conflicting files:<\/p>\n<pre>\n<code>$ git status<\/code><\/pre>\n<\/li>\n<li>\n<p>Open the conflicting file using a text editor:<\/p>\n<pre>\n<code>$ vim index.html<\/code><\/pre>\n<\/li>\n<li>\n<p>Resolve the conflict. The conflicting changes are marked by <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD<\/code> and <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;<\/code>. You need to choose which changes to keep and which to discard. Manually edit the file to combine the conflicting changes.\u00a0<\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre>\n<code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD\n&lt;div class=\"header\"&gt;\n&lt;h1&gt;Sample text 1&lt;\/h1&gt;\n&lt;\/div&gt;\n=======\n&lt;div class=\"header\"&gt;\n&lt;h1&gt;Sample text 2&lt;\/h1&gt;\n&lt;\/div&gt;\n&gt;&gt;&gt;&gt;&gt;&gt;&gt; feature-branch<\/code><\/pre>\n<p>In this example, I changed the website heading to\u00a0<code>Sample text 1<\/code>, while you have changed the heading to\u00a0<code>Sample text 2<\/code>. Both changes have been added to the file. You can now decide which heading to keep or edit the file to combine the changes. In either case, remove the markers that indicate the beginning and end of the changes, leaving only the code you want:<\/p>\n<pre>\n<code>&lt;div class=\"header\"&gt;\n&lt;h1&gt;Sample text 2&lt;\/h1&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Save all of your changes, and close your editor.<\/p>\n<\/li>\n<li>\n<p>Add the file to the staging area:<\/p>\n<pre>\n<code>$ git add index.html<\/code><\/pre>\n<\/li>\n<li>\n<p>Commit the changes:<\/p>\n<pre>\n<code>$ git commit -m \"Updated h1 in index.html\"<\/code><\/pre>\n<p>This command commits the changes with the message <code>Resolved merge conflict<\/code>.<\/p>\n<\/li>\n<li>\n<p>Push the changes to the remote repository:<\/p>\n<pre>\n<code>$ git push<\/code><\/pre>\n<\/li>\n<\/ol>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More on Git<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/what-is-git?intcmp=7013a000002CxqLAAS\">What is Git?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-git?intcmp=7013a000002CxqLAAS\">Git cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-markdown?intcmp=7013a000002CxqLAAS\">Markdown cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/git?intcmp=7013a000002CxqLAAS\">New Git articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Resolution<\/h2>\n<p>\u00a0Merge conflicts are a good reason to focus your changes on code. The more you change in a file, the greater the potential for conflict. You should make more commits with fewer changes each. You should avoid making a monolithic change that combines multiple feature enhancements or bug fixes into one. Your project manager will thank you, too, because commits with clear intent are easier to track. A Git merge conflict may seem intimidating at first, but now that you know how to do it, you&#8217;ll see that it&#8217;s easily resolved.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Don&#8217;t panic when you encounter a merge conflict. With a little expert negotiation, you can resolve any conflict.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/04\/code_computer_laptop_hack_work.png\" width=\"1041\" height=\"584\" alt=\"Coding on a computer\" title=\"Coding on a computer\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/git\" hreflang=\"en\">Git<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2023\/04\/cc-by-sa-4-4.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to resolve Git merge conflicts agantony Tue, 04\/04\/2023 &#8211; 03:00 Suppose you and I are working on the same file called index.html. I make some changes to the file, commit them, and push the changes to the remote Git repository. You also make some changes to the same file, commit them, and start pushing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":72434,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-72433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/72433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=72433"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/72433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/72434"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}