<?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: Tim Mouskhelichvili</title>
    <description>The latest articles on DEV Community by Tim Mouskhelichvili (@timmouskhelichvili).</description>
    <link>https://dev.to/timmouskhelichvili</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%2F857859%2F78ff4607-3ae6-4432-afb2-986afe11c841.jpeg</url>
      <title>DEV Community: Tim Mouskhelichvili</title>
      <link>https://dev.to/timmouskhelichvili</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timmouskhelichvili"/>
    <language>en</language>
    <item>
      <title>How To Split A Git Commit Into Multiple Ones?</title>
      <dc:creator>Tim Mouskhelichvili</dc:creator>
      <pubDate>Sat, 28 May 2022 23:01:02 +0000</pubDate>
      <link>https://dev.to/timmouskhelichvili/how-to-split-a-git-commit-into-multiple-ones-3g6f</link>
      <guid>https://dev.to/timmouskhelichvili/how-to-split-a-git-commit-into-multiple-ones-3g6f</guid>
      <description>&lt;p&gt;Sometimes, a developer might want to split a git commit into multiple ones. The reason for splitting a commit is typically to make the git history more readable or to reorder the commits to avoid conflicts. Luckily, this operation is possible using the reset command or an interactive rebase.&lt;/p&gt;

&lt;p&gt;Let's get to it 😎.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycrd5146bfivf3puq2sg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycrd5146bfivf3puq2sg.jpg" alt="git commit split" width="500" height="585"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Preparation
&lt;/h2&gt;

&lt;p&gt;The first thing to do is make sure you don't have any changes in your working directory.&lt;/p&gt;

&lt;p&gt;This can be done using the &lt;a href="https://timmousk.com/blog/git-status/" rel="noopener noreferrer"&gt;git status&lt;/a&gt; command.&lt;/p&gt;

&lt;p&gt;When running this command in the terminal, it should show no files. If it does show files, you will need to either commit the changes or discard them.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to split the most recent commit?
&lt;/h2&gt;

&lt;p&gt;To split the most recent commit, use the &lt;code&gt;git reset&lt;/code&gt; command to revert the latest commit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset HEAD~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;HEAD&lt;/code&gt; refers to the current branch. &lt;br&gt;
&lt;code&gt;HEAD~&lt;/code&gt; refers to the latest commit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then, create different commits with the desired files and messages and push them to the remote.&lt;/p&gt;

&lt;p&gt;But what if the commit you want to split is not the latest one? &lt;/p&gt;

&lt;p&gt;Easy. Use interactive rebasing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn8ceha07flr3ds3128t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdn8ceha07flr3ds3128t.jpg" alt="interactive rebase" width="528" height="473"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  How to split a commit farther back?
&lt;/h2&gt;

&lt;p&gt;You need to rewrite the git history with an interactive rebase to split a commit farther back.&lt;/p&gt;

&lt;p&gt;1 - First, find the commit hash with the &lt;code&gt;git reflog&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;2 - Then use the &lt;code&gt;git rebase&lt;/code&gt; command with the commit's hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git rebase &lt;span class="nt"&gt;-i&lt;/span&gt; HASH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3 - In the rebase edit screen, find the line with the commit that you want to split and replace &lt;code&gt;pick&lt;/code&gt; with &lt;code&gt;edit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;4 - Save and exit the rebase edit screen.&lt;/p&gt;

&lt;p&gt;5 - Reset the state to the previous commit with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git reset HEAD~
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6 - Create different commits with the desired files and messages.&lt;/p&gt;

&lt;p&gt;7 - Finish the rebase by typing this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git rebase &lt;span class="nt"&gt;--continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If something goes wrong, you can &lt;a href="https://timmousk.com/blog/git-rebase-abort/" rel="noopener noreferrer"&gt;abort the rebase&lt;/a&gt; and start over by using the &lt;code&gt;git rebase --abort&lt;/code&gt; command.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you can see, it is easy to split a commit into multiple ones.&lt;/p&gt;

&lt;p&gt;However, note that you will need to use the &lt;code&gt;git push --force&lt;/code&gt; command if you have already pushed to a remote.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffppro7oxeg06kc0zl95l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffppro7oxeg06kc0zl95l.jpg" alt="git split commit" width="612" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some other Git tutorials that I wrote for you to enjoy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://timmousk.com/blog/git-empty-commit/" rel="noopener noreferrer"&gt;How to push an empty git commit?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://timmousk.com/blog/git-pre-receive-hook-declined/" rel="noopener noreferrer"&gt;How to fix "git pre-receive hook declined"?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://timmousk.com/blog/git-delete-tag/" rel="noopener noreferrer"&gt;Delete a remote or local tag in Git&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://timmousk.com/blog/git-tag/" rel="noopener noreferrer"&gt;The complete guide on Git tags&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://timmousk.com/blog/git-uncommit/" rel="noopener noreferrer"&gt;How to uncommit your changes in git?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>git</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
