<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fredrick Abisai</title>
    <description>The latest articles on DEV Community by Fredrick Abisai (@abisaifredrick).</description>
    <link>https://dev.to/abisaifredrick</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F367224%2F9b2c0a95-da2d-4ba8-8829-8ae0b28d4d4f.jpg</url>
      <title>DEV Community: Fredrick Abisai</title>
      <link>https://dev.to/abisaifredrick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abisaifredrick"/>
    <language>en</language>
    <item>
      <title>fatal: refusing to merge unrelated histories</title>
      <dc:creator>Fredrick Abisai</dc:creator>
      <pubDate>Fri, 09 Aug 2024 16:10:45 +0000</pubDate>
      <link>https://dev.to/abisaifredrick/fatal-refusing-to-merge-unrelated-histories-3bb4</link>
      <guid>https://dev.to/abisaifredrick/fatal-refusing-to-merge-unrelated-histories-3bb4</guid>
      <description>&lt;p&gt;Have you ever encountered this error &lt;code&gt;fatal: refusing to merge unrelated histories.&lt;/code&gt;, while working with git? Well, I have and it wasn't fun. How did I get to that error, you may ask yourself.&lt;/p&gt;

&lt;p&gt;This was how it all went down: &lt;/p&gt;

&lt;p&gt;I generated an angular project and as you know angular by default comes with git initialized. I then went to create a project in GitHub and added an MIT License file then I came back to add a remote URL by this command in my Angular project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   git remote add origin git@github.com:user/example-project.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Upon writing the &lt;code&gt;git push origin main&lt;/code&gt; then the error occurred. &lt;/p&gt;

&lt;p&gt;After a length and thorough research on this error this is what I discovered:&lt;/p&gt;

&lt;h2&gt;
  
  
  Causes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Incompatibility between local repository and remote repository.
&lt;/h3&gt;

&lt;p&gt;This mostly occurs in new projects, if there are some changes in a new remote repository and you have a new repository locally which there are also changes which are not pushed. This is what happened to me.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Initiation of merge between two branches with no shared history.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  3. Corrupted &lt;code&gt;.git&lt;/code&gt; directory.
&lt;/h3&gt;

&lt;p&gt;In a nutshell, this error commonly occurs in situations where we are integrating disparate repositories or initiating a merge between branches with no shared history.&lt;/p&gt;

&lt;p&gt;What is the way forward then, you may ask. Well here is how to solve it and avoid the irritation the error brings: &lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Use Allow Unrelated Histories Flag (&lt;code&gt;--allow-unrelated-histories&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;This is a flag which can be used when merging two different branches or when pulling from same branch with incompatible changes.&lt;/p&gt;

&lt;p&gt;When merging, this flag can be used as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git merge --allow-unrelated-histories &amp;lt;branch-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When pulling, this flag can be used as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git &amp;lt;branch-name&amp;gt; origin master --allow-unrelated-histories
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.   Create a new Commit
&lt;/h3&gt;

&lt;p&gt;Create a new empty commit that connects unrelated branches then merge those branches.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Empty Commit
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit --allow-empty -m "Merge unrelated branches"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Merge unrelated branches
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git merge &amp;lt;branch-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how do we prevent this error from totally occurring? &lt;/p&gt;

&lt;h2&gt;
  
  
  Prevention
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Repository Initialization
&lt;/h3&gt;

&lt;p&gt;When setting up a new repository, it’s advisable to initialize it using an existing project. This approach links their histories, helping to prevent the issue of unrelated histories.&lt;/p&gt;

&lt;p&gt;More information can be found &lt;a href="https://git-scm.com/docs/git-merge" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading! Until next time ✋&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>webdev</category>
      <category>help</category>
    </item>
  </channel>
</rss>
