{"componentChunkName":"component---src-templates-legacy-tutorial-page-js","path":"/oa/tutorials/git-to-know-branching/conflicts/","result":{"pageContext":{"tutorial":{"id":"T0E6OlR1dG9yaWFsLTE3MA==","slug":"git-to-know-branching","title":"Git to Know Branching","previewText":"Branches aren't just for trees!\nLearn how to create and manage branches effectively with this handy, step by step tutorial.\n","heroImagePath":"https://raw.githubusercontent.com/MakeSchool-Tutorials/Git-to-Know-Branching/master/cover.png","heroImageFile":{"childImageSharp":{"gatsbyImageData":{"layout":"constrained","placeholder":{"fallback":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD6ElEQVQ4y42RX0xcRRTGLwu+YKxGk1ZD60N3Gl7qCwoxmmqkaGO0aXzQxNYHHzSxPmliNBqNYrVWU+auRYnWSA1SoYFuNPhQ2vLn3pUWUigiKBQKhYXddZe1bFl22b33zvnMzN1rtm2aOMnkzJw553e+c0YDkZZzRAkAuTVB9AkRrViOiIAoBhRvxIgoZjkiCqKkI8SrMgdA6T9ZSyMi7ToYgE+JCAAhmbVRP7iMdwcSeG8ggZe6w9jdNY83jSiylgMSQsUBeKUYqhXDImkLx/5MOQAJgKh/MUN1wUV65MQ83fbVBG1pnqILsYyqmM7ZzuHQHK6tW9dBNQ+2mnew71TUecuMq7JZy1H2YnwdO0+G8UDrNGaurksXcrYg2cmO7wbFC22jcJRaF6pg0TUL+3v/FjXtC/Th+WX5SN/8kUTH9IoChJbWMJbIqnPrZAq/RTIqZk/rCG6vPy1e7hzD1UxeQSUw8svsKh4+sWDVBRfx/rll1dLBC3GUN02gL5yGt7rm0jIOxpILfPbHYdr8RS/uOnAm1z+XlL5+zXKoGsBqy2QKVT/N2x+ccxXqo8vkaxzH5uZJ9ITT6F7IoC4YxmMdYalQFX2mZRgbPj5tdYxHZc5Mat26vzBDF/r12AreMOK2fG0YSVB50wTdc/QvbG+Zws72GezuiuLxjgVygYQnvh+yjo0syvDLa3m7wpthWeFjqgXR6nBczcoOjC6jtHGcqtrm6KnOK9imm6htn6ba4BINqBkKazCsZnw574iKAqPM+2UFJZJKSQ3t0HDC3tJ8Cc/9GqFdJ+eJcYMqdYOqW6bQv5S1CmOdXcs7FcUMzXEcr23lSGYd1f7P7kfZe7oieLLzClUGTNp6uA/3ft5njcbWlLKpZKbiBkGqZQUVQkiHehDCnem34yvY0RG2nw7Oq5Y3HuyxWkeXFOxazlUmcyRIbqVQLnnxoJ5SOVMA6aMTKdQcn7XvO9TrwWZzDv2nzIMpdcXLgxa3AHKVNo4k8MPFiFKWyORvavMmWDHUtm+Agh4CkAJwKWs5/x92C2iptFlbbF3JWpuKfbeEMW74GDel1Rg3S+T9bApa+Ufd2oYDZ3zHf4+U5QsFmgYXyk5NJ7TKgOlTeYEBmVfq50Ypc7fPA8ntY3rIvQeGpC3xc0Or1E2tvnemTHvwiDpv01WiG/fleWX93p0bUqG5l+mhOxVMD93NuPkiOzJUUii0l3HzUVVQD21k3HzeTTZrGTdeY4GBOxg33mHc+IxxQ5e5khpj3GjzN/TJc5BxI+Jv6JGATYW3s37eL+9V8sy4uY9xY4Rxs4bpoe2MG3N+brzu58bbjBu7/gWOSk/VhPcKJgAAAABJRU5ErkJggg=="},"images":{"fallback":{"src":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/5aead/cover.png","srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/e9fba/cover.png 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/15e42/cover.png 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/5aead/cover.png 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/d6138/cover.png 400w","sizes":"(min-width: 200px) 200px, 100vw"},"sources":[{"srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/b79cb/cover.avif 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/6d0de/cover.avif 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/f2685/cover.avif 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/4ff31/cover.avif 400w","type":"image/avif","sizes":"(min-width: 200px) 200px, 100vw"},{"srcSet":"/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/dbc4a/cover.webp 50w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/d8057/cover.webp 100w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/2e34e/cover.webp 200w,\n/mediabook/static/a69ceb219f4e17b4f09e707a84068da8/416c3/cover.webp 400w","type":"image/webp","sizes":"(min-width: 200px) 200px, 100vw"}]},"width":200,"height":200}}},"pages":{"nodes":[{"id":"T0E6OlBhZ2UtMTM1Ng==","title":"Branching Out","slug":"branching","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tNTQxNw==","title":"Branching Out","htmlContent":"<div class=\"quote\">\n<p><strong>If you ever talk to a great programmer, you'll find they know their tools like an artist knows their paintbrushes.</strong></p>\n\n<p>-Bill Gates</p>\n</div><p>This tutorial details a step-by-step strategy to enhance your development workflow called <strong>git branching</strong>.</p><p>Branching is often referred to as the \"killer feature\" of Git. Let's explore why in the following section, the Purpose of Branching.</p><h2>Purpose of Branching</h2><p>Successful teams follow <strong>three rules to integrate git into their daily workflow</strong>:</p><ol>\n<li><strong>Commit early and often.</strong></li>\n<li>\n<strong>Don't break main.</strong>\n\n<ul>\n<li>The code in <code>origin/main</code> <strong>always</strong> works.</li>\n<li>\n<strong>No errors</strong>, no UI glitches.</li>\n<li>\n<strong>No exceptions</strong>.</li>\n</ul>\n</li>\n<li>\n<strong>Use branches.</strong> A branch can represent:\n\n<ul>\n<li>A brand new <strong>feature</strong>.</li>\n<li>\n<strong>Bug fixes</strong>.</li>\n<li>Works <strong>in progress</strong>.</li>\n<li>Individual contributions <strong>that aren't ready to be deployed</strong>.</li>\n</ul>\n</li>\n</ol><h2>Command Overview</h2><p>The commands we'll be running in each step are listed below for your future reference. Save it in your notes!</p><div class=\"solution\">\n<p></p>\n<pre>$ <span class=\"nb\">cd</span> ~/dev/repos/\n$ git clone git@github.com:YOUR_USERNAME/git-branchy.git\n$ <span class=\"nb\">cd</span> git-branchy\n$ git pull origin main\n$ git checkout -b develop\n$ git add .\n$ git commit -m <span class=\"s2\">\"[add] code from step 5 of sweet git tutorial\"</span>\n$ git pull origin main\n$ git push origin develop\n$ git checkout main\n$ git pull origin main\n$ git merge develop\n$ git add .\n$ git commit -m <span class=\"s2\">\"[add] merge the develop branch\"</span>\n$ git push origin main\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQxOA==","title":"Let's Go!","htmlContent":"<p>Sold? Let's try it out! Follow the step-by-step guide below to create and push your own branch to GitHub!</p>"},{"id":"T0E6OlNlY3Rpb24tNTQxOQ==","title":"Step 1 - Create a New GitHub Repository","htmlContent":"<div class=\"action\">\n<p>\nFirst, create a <a href=\"https://github.com/new\" target=\"_blank\">new GitHub repository</a> to serve as a playground for this tutorial. Call it <code>git-branchy</code>. Initialize the repo with a <code>README</code> file.</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyMA==","title":"Step 2 - Create a Local Clone","htmlContent":"<div class=\"action\">\n<p>\nOn your <strong>local</strong> machine, <code>cd</code> to the project directory. In git, this is known as your <strong>working directory</strong>, which was created when you executed <code>git clone</code> at the beginning of the project. Clone the remote repository you made in Step 1.</p>\n<pre>$ <span class=\"nb\">cd</span> ~/dev/repos\n$ git clone git@github.com:YOUR_USERNAME/git-branchy.git\n\nCloning into <span class=\"s1\">'git-branchy'</span>...\nremote: Enumerating objects: <span class=\"m\">233</span>, <span class=\"k\">done</span>.\nremote: Counting objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">233</span>/233<span class=\"o\">)</span>, <span class=\"k\">done</span>.\nremote: Compressing objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">158</span>/158<span class=\"o\">)</span>, <span class=\"k\">done</span>.\nremote: Total <span class=\"m\">233</span> <span class=\"o\">(</span>delta <span class=\"m\">120</span><span class=\"o\">)</span>, reused <span class=\"m\">173</span> <span class=\"o\">(</span>delta <span class=\"m\">62</span><span class=\"o\">)</span>, pack-reused <span class=\"m\">0</span>\nReceiving objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">233</span>/233<span class=\"o\">)</span>, <span class=\"m\">35</span>.28 KiB <span class=\"p\">|</span> <span class=\"m\">5</span>.04 MiB/s, <span class=\"k\">done</span>.\nResolving deltas: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">120</span>/120<span class=\"o\">)</span>, <span class=\"k\">done</span>.\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyMQ==","title":"Step 3 - Pull Updates Early and Often","htmlContent":"<div class=\"action\">\n<p>\nUpdate your <strong>local</strong> repository with the latest revision of the <code>origin/main</code> branch.</p>\n<pre>$ git pull origin main\n\nremote: Counting objects: <span class=\"m\">6</span>, <span class=\"k\">done</span>.\nremote: Total <span class=\"m\">6</span> <span class=\"o\">(</span>delta <span class=\"m\">2</span><span class=\"o\">)</span>, reused <span class=\"m\">2</span> <span class=\"o\">(</span>delta <span class=\"m\">2</span><span class=\"o\">)</span>, pack-reused <span class=\"m\">4</span>\nUnpacking objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">6</span>/6<span class=\"o\">)</span>, <span class=\"k\">done</span>.\nFrom ssh://github.com/YOUR_USERNAME/git-branchy\nbranch            main     -&gt; FETCH_HEAD\n383e9d7..7766d57  main     -&gt; origin/main\nUpdating 383e9d7..7766d57\nFast-forward\nREADME.md <span class=\"p\">|</span> <span class=\"m\">25</span> ++++++++++++++++++-------\n<span class=\"m\">1</span> file changed, <span class=\"m\">18</span> insertions<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">7</span> deletions<span class=\"o\">(</span>-<span class=\"o\">)</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyMg==","title":"Step 4 - Creating a Branch","htmlContent":"<p>Now that your <strong>local</strong> <code>main</code> branch is up-to-date with your team's latest changes, create a branch locally by <strong>running the exact command</strong> below. There are other ways to do this, but <strong>this one is foolproof</strong>.</p><p>The syntax below creates a new branch named <code>develop</code> based on an existing branch, <code>main</code>.</p><div class=\"action\">\n<p>\nRun the following command to create the <code>develop</code> branch locally:</p>\n<pre>$ git checkout -b develop\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyMw==","title":"Step 5 - Generate a Changeset","htmlContent":"<p>Write that fancy new feature, fix that bug, experiment; it's showtime!</p><div class=\"action\">\n<p>\nCreate a new file. Add some code. We trust you --- you've been doing this a while! For example, I added a file named <code>test.js</code> to my directory.</p>\n\n<p>Once complete, run the following commands to add and commit your changeset to your local repository.</p>\n<pre>$ git add .\n$ git commit -m <span class=\"s2\">\"[add] code from step 5 of sweet git tutorial\"</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyNA==","title":"Step 6 - Integrate Upstream Changes","htmlContent":"<div class=\"action\">\n<p>\nIntegrate upstream changes from <code>origin/main</code>, which adds them to our <strong>local</strong> branch.</p>\n<pre>$ git pull origin main\n\nFrom ssh://github.com/YOUR_USERNAME/git-branchy\n * branch            main     -&gt; FETCH_HEAD\nAlready up-to-date.\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyNQ==","title":"Step 7 - Push Your New Branch","htmlContent":"<div class=\"action\">\n<p>\nPush your new branch, as well as your changeset, to <code>origin/develop</code>.</p>\n<pre>$ git push origin develop\n\nCounting objects: <span class=\"m\">16</span>, <span class=\"k\">done</span>.\nDelta compression using up to <span class=\"m\">4</span> threads.\nCompressing objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">16</span>/16<span class=\"o\">)</span>, <span class=\"k\">done</span>.\nWriting objects: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">16</span>/16<span class=\"o\">)</span>, <span class=\"m\">1</span>.49 KiB <span class=\"p\">|</span> <span class=\"m\">1</span>.49 MiB/s, <span class=\"k\">done</span>.\nTotal <span class=\"m\">16</span> <span class=\"o\">(</span>delta <span class=\"m\">14</span><span class=\"o\">)</span>, reused <span class=\"m\">0</span> <span class=\"o\">(</span>delta <span class=\"m\">0</span><span class=\"o\">)</span>\nremote: Resolving deltas: <span class=\"m\">100</span>% <span class=\"o\">(</span><span class=\"m\">14</span>/14<span class=\"o\">)</span>, completed with <span class=\"m\">14</span> <span class=\"nb\">local</span> objects.\nTo ssh://github.com/YOUR_USERNAME/git-branchy.git\n<span class=\"o\">[</span>new branch<span class=\"o\">]</span>      <span class=\"s1\">'develop'</span> -&gt; <span class=\"s1\">'develop'</span>\n</pre>\n</div><p>This branch is now available on your <code>origin</code> &ndash; simply open up GitHub (or GitLab, Heroku, Bitbucket, etc) to verify!</p><p>Once complete, it's a good idea to ask a friend or colleague to review your code.</p><p>Once your code is bug-free and release-ready, merge it to the <code>main</code> branch.</p><div class=\"info\">\n<p>\nRepeat this process each time you finish a part of the feature or need to take a break. Branches are considered works in progress &ndash; so commit and push to your branche(s) as much as possible; <strong>even if it's not done</strong>! If something goes terribly wrong, <code>origin/develop</code> serves as a history, backup, and record of your valuable work!</p>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTQyNg==","title":"Next Step","htmlContent":"<p>Ready for primetime? Proceed to the next page to learn how to merge your code into the <code>origin/main</code> branch on GitHub.</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTM1Nw==","slug":"merging","title":"Merging to Main"},"previous":{"id":"T0E6OlBhZ2UtMTM1Nw==","slug":"merging","title":"Merging to Main"}},{"id":"T0E6OlBhZ2UtMTM1Nw==","title":"Merging to Main","slug":"merging","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tNTM1MA==","title":"Merging to Main","htmlContent":"<div class=\"quote\">\n<p><strong>Talk is cheap, show me the code!</strong></p>\n\n<p>-Linus Torvalds</p>\n</div><p>Ready to merge to <code>origin/main</code>?</p><p>Let's make it happen using the following three step process!</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1MQ==","title":"Step 1 - Move to Main Branch","htmlContent":"<div class=\"action\">\n<p>\nIn your local repository directory, run the following command to switch to the <code>main</code> branch.</p>\n<pre>$ git checkout main\n\nSwitched to branch <span class=\"s1\">'main'</span>\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM1Mg==","title":"Step 2 - Pull the Latest Changes","htmlContent":"<div class=\"action\">\n<p>\nPull the latest changes from <code>origin/main</code>, integrating the changesets into your local repository.</p>\n<pre>$ git pull origin main\n\nFrom github.com:YOUR_USERNAME/git-branchy\n* branch            main     -&gt; FETCH_HEAD\nAlready up to date.\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM1Mw==","title":"Step 3 - Merge!","htmlContent":"<div class=\"action\">\n<p>\nLet's merge by running the command below!</p>\n<pre>$ git merge develop\n\nMerge made by the <span class=\"s1\">'recursive'</span> strategy.\ntest.js              <span class=\"p\">|</span>  <span class=\"m\">0</span>\n<span class=\"m\">1</span> files changed, <span class=\"m\">1</span> insertions<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">0</span> deletions<span class=\"o\">(</span>-<span class=\"o\">)</span>\ncreate mode <span class=\"m\">100644</span> test.js\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM1NA==","title":"Step 4 - Push to GitHub","htmlContent":"<div class=\"action\">\n<p>\nAdd or stage your changes, commit them, and push them to GitHub.</p>\n<pre>$ git add .\n$ git commit -m <span class=\"s2\">\"[add] merge the develop branch\"</span>\n$ git push origin main\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM1NQ==","title":"Next Step","htmlContent":"<p>We've practiced what to do in successful merge scenarios. Awesome! But what happens when things go awry? Follow the next step of the tutorial to practice how to deal with merge conflicts!</p>"}]},"next":{"id":"T0E6OlBhZ2UtMTM1OA==","slug":"conflicts","title":"Coping with Conflicts"},"previous":{"id":"T0E6OlBhZ2UtMTM1OA==","slug":"conflicts","title":"Coping with Conflicts"}},{"id":"T0E6OlBhZ2UtMTM1OA==","title":"Coping with Conflicts","slug":"conflicts","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tNTM1Ng==","title":"Coping with Conflicts","htmlContent":"<div class=\"quote\">\n<p><strong>Don&rsquo;t worry if it doesn&rsquo;t work right. If everything did, you&rsquo;d be out of a job.</strong></p>\n\n<p>-Mosher&rsquo;s Law of Software Engineering</p>\n</div><p>Let's practice what to do when your branch doesn't merge flawlessly with <code>main</code>.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1Nw==","title":"Step 1 - Creating a Conflict Script","htmlContent":"<p>In order to explore merge conflicts, we'll need to generate one in your repository!</p><div class=\"action\">\n<p>\nCreate a new file in the root of the repository named <code>make-conflict.sh</code>, and ensure it can run using the <code>chmod</code> utility.</p>\n<pre>$ touch make-conflict.sh\n$ chmod +x make-conflict.sh\n</pre>\n<p>[action]\nPaste the following code inside <code>make-conflict.sh</code> and save the file.</p>\n<pre>touch my_code.sh\ngit add my_code.sh\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo Hello\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'initial'</span>\ngit checkout -b new_branch\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo \\\"Hello World\\\"\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'first commit on new_branch'</span>\ngit checkout main\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo \\\"Hello World!\\\"\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'second commit on main'</span>\ngit merge new_branch\n</pre>\n<p>[action]\nRun the script using the command below. Examine the last two lines of output. What happened?</p>\n<pre>$ ./make-conflict.sh\n\n<span class=\"o\">[</span>main <span class=\"o\">(</span>root-commit<span class=\"o\">)</span> cc780d8<span class=\"o\">]</span> initial\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>\n create mode <span class=\"m\">100644</span> my_code.sh\nSwitched to a new branch <span class=\"s1\">'new_branch'</span>\n<span class=\"o\">[</span>new_branch 3cb9aa2<span class=\"o\">]</span> first commit on new_branch\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">1</span> deletion<span class=\"o\">(</span>-<span class=\"o\">)</span>\nSwitched to branch <span class=\"s1\">'main'</span>\n<span class=\"o\">[</span>main 20700cb<span class=\"o\">]</span> second commit on main\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">1</span> deletion<span class=\"o\">(</span>-<span class=\"o\">)</span>\nAuto-merging my_code.sh\nCONFLICT <span class=\"o\">(</span>content<span class=\"o\">)</span>: Merge conflict in my_code.sh\nAutomatic merge failed<span class=\"p\">;</span> fix conflicts and <span class=\"k\">then</span> commit the result.\n</pre>\n</div><p>Git wasn't able to merge the changeset automatically!</p><p>It needs your help in order to rectify the changeset.</p><p>Next, let's learn how to inform Git about the changes we want to keep, and the changes we want to discard.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1OA==","title":"Step 2 - Viewing the Conflict","htmlContent":"<div class=\"action\">\n<p>\nTo see the beginning of the merge conflict in your file, open <code>my_code.sh</code> in your editor and search the file for the conflict marker <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>.</p>\n<pre><span class=\"o\">&lt;&lt;&lt;&lt;&lt;&lt;</span>&lt; HEAD\n<span class=\"nb\">echo</span> <span class=\"s2\">\"Hello World!\"</span>\n<span class=\"o\">=======</span>\n<span class=\"nb\">echo</span> <span class=\"s2\">\"Hello World\"</span>\n&gt;&gt;&gt;&gt;&gt;&gt; new_branch\n</pre>\n</div><p>When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</code>.</p><p>Next, you'll see <code>=======</code>, which divides your changes from the changes in the other branch, followed by <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt; new_branch</code>, the branch we attempted to merge into <code>main</code>.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1OQ==","title":"Step 3 - Merge Manually","htmlContent":"<div class=\"action\">\n<p>\nDecide if you want to keep only your branch's changes, keep only the other branch's changes, or make a brand new change, which may incorporate changes from both branches.</p>\n\n<p>Delete the conflict markers <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>, <code>=======</code>, <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code> and make the changes you want in the final merge.</p>\n\n<p>In this example, both changes are incorporated into the final merge.</p>\n\n<p>Here's what the <code>my_code.sh</code> file looks like in it's final form:</p>\n<pre>Hello World\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM2MA==","title":"Step 4 - Push Those Changes","htmlContent":"<div class=\"action\">\n<p>\nAdd or stage your changes, commit them, and finally, perform your last push to GitHub.</p>\n<pre>$ git add .\n$ git commit -m <span class=\"s2\">\"[fix] merge conflict\"</span>\n$ git push origin main\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM2MQ==","title":"Acheivement Unlocked","htmlContent":"<h2>Mega Merge Main</h2><p><strong>Way to level up your Git skills --- you're ready to handle branches, merges, and conflicts in your day-to-day projects!</strong></p>"}]},"next":null,"previous":null}]}},"page":{"id":"T0E6OlBhZ2UtMTM1OA==","title":"Coping with Conflicts","slug":"conflicts","sections":{"nodes":[{"id":"T0E6OlNlY3Rpb24tNTM1Ng==","title":"Coping with Conflicts","htmlContent":"<div class=\"quote\">\n<p><strong>Don&rsquo;t worry if it doesn&rsquo;t work right. If everything did, you&rsquo;d be out of a job.</strong></p>\n\n<p>-Mosher&rsquo;s Law of Software Engineering</p>\n</div><p>Let's practice what to do when your branch doesn't merge flawlessly with <code>main</code>.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1Nw==","title":"Step 1 - Creating a Conflict Script","htmlContent":"<p>In order to explore merge conflicts, we'll need to generate one in your repository!</p><div class=\"action\">\n<p>\nCreate a new file in the root of the repository named <code>make-conflict.sh</code>, and ensure it can run using the <code>chmod</code> utility.</p>\n<pre>$ touch make-conflict.sh\n$ chmod +x make-conflict.sh\n</pre>\n<p>[action]\nPaste the following code inside <code>make-conflict.sh</code> and save the file.</p>\n<pre>touch my_code.sh\ngit add my_code.sh\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo Hello\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'initial'</span>\ngit checkout -b new_branch\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo \\\"Hello World\\\"\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'first commit on new_branch'</span>\ngit checkout main\n<span class=\"nb\">echo</span> <span class=\"s2\">\"echo \\\"Hello World!\\\"\"</span> &gt; my_code.sh\ngit commit -am <span class=\"s1\">'second commit on main'</span>\ngit merge new_branch\n</pre>\n<p>[action]\nRun the script using the command below. Examine the last two lines of output. What happened?</p>\n<pre>$ ./make-conflict.sh\n\n<span class=\"o\">[</span>main <span class=\"o\">(</span>root-commit<span class=\"o\">)</span> cc780d8<span class=\"o\">]</span> initial\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>\n create mode <span class=\"m\">100644</span> my_code.sh\nSwitched to a new branch <span class=\"s1\">'new_branch'</span>\n<span class=\"o\">[</span>new_branch 3cb9aa2<span class=\"o\">]</span> first commit on new_branch\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">1</span> deletion<span class=\"o\">(</span>-<span class=\"o\">)</span>\nSwitched to branch <span class=\"s1\">'main'</span>\n<span class=\"o\">[</span>main 20700cb<span class=\"o\">]</span> second commit on main\n <span class=\"m\">1</span> file changed, <span class=\"m\">1</span> insertion<span class=\"o\">(</span>+<span class=\"o\">)</span>, <span class=\"m\">1</span> deletion<span class=\"o\">(</span>-<span class=\"o\">)</span>\nAuto-merging my_code.sh\nCONFLICT <span class=\"o\">(</span>content<span class=\"o\">)</span>: Merge conflict in my_code.sh\nAutomatic merge failed<span class=\"p\">;</span> fix conflicts and <span class=\"k\">then</span> commit the result.\n</pre>\n</div><p>Git wasn't able to merge the changeset automatically!</p><p>It needs your help in order to rectify the changeset.</p><p>Next, let's learn how to inform Git about the changes we want to keep, and the changes we want to discard.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1OA==","title":"Step 2 - Viewing the Conflict","htmlContent":"<div class=\"action\">\n<p>\nTo see the beginning of the merge conflict in your file, open <code>my_code.sh</code> in your editor and search the file for the conflict marker <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>.</p>\n<pre><span class=\"o\">&lt;&lt;&lt;&lt;&lt;&lt;</span>&lt; HEAD\n<span class=\"nb\">echo</span> <span class=\"s2\">\"Hello World!\"</span>\n<span class=\"o\">=======</span>\n<span class=\"nb\">echo</span> <span class=\"s2\">\"Hello World\"</span>\n&gt;&gt;&gt;&gt;&gt;&gt; new_branch\n</pre>\n</div><p>When you open the file in your text editor, you'll see the changes from the HEAD or base branch after the line <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</code>.</p><p>Next, you'll see <code>=======</code>, which divides your changes from the changes in the other branch, followed by <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt; new_branch</code>, the branch we attempted to merge into <code>main</code>.</p>"},{"id":"T0E6OlNlY3Rpb24tNTM1OQ==","title":"Step 3 - Merge Manually","htmlContent":"<div class=\"action\">\n<p>\nDecide if you want to keep only your branch's changes, keep only the other branch's changes, or make a brand new change, which may incorporate changes from both branches.</p>\n\n<p>Delete the conflict markers <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>, <code>=======</code>, <code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code> and make the changes you want in the final merge.</p>\n\n<p>In this example, both changes are incorporated into the final merge.</p>\n\n<p>Here's what the <code>my_code.sh</code> file looks like in it's final form:</p>\n<pre>Hello World\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM2MA==","title":"Step 4 - Push Those Changes","htmlContent":"<div class=\"action\">\n<p>\nAdd or stage your changes, commit them, and finally, perform your last push to GitHub.</p>\n<pre>$ git add .\n$ git commit -m <span class=\"s2\">\"[fix] merge conflict\"</span>\n$ git push origin main\n</pre>\n</div>"},{"id":"T0E6OlNlY3Rpb24tNTM2MQ==","title":"Acheivement Unlocked","htmlContent":"<h2>Mega Merge Main</h2><p><strong>Way to level up your Git skills --- you're ready to handle branches, merges, and conflicts in your day-to-day projects!</strong></p>"}]},"next":null,"previous":null}}},"staticQueryHashes":[]}