<?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: X</title>
    <description>The latest articles on DEV Community by X (@ax_sh).</description>
    <link>https://dev.to/ax_sh</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%2F2145374%2F2afda5cb-b494-4b49-bf68-129b1b381c5f.jpg</url>
      <title>DEV Community: X</title>
      <link>https://dev.to/ax_sh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ax_sh"/>
    <language>en</language>
    <item>
      <title>Git Alias: Rescue Commits from the Wrong Branch</title>
      <dc:creator>X</dc:creator>
      <pubDate>Wed, 03 Dec 2025 05:34:48 +0000</pubDate>
      <link>https://dev.to/ax_sh/git-alias-rescue-commits-from-the-wrong-branch-104o</link>
      <guid>https://dev.to/ax_sh/git-alias-rescue-commits-from-the-wrong-branch-104o</guid>
      <description>&lt;p&gt;We've all been there. You've been coding away, making commits, feeling productive... then you realize with horror that you've been committing to &lt;code&gt;main&lt;/code&gt; instead of your feature branch. Your stomach drops. But don't panic—there's an easy fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Matters More Than Ever
&lt;/h2&gt;

&lt;p&gt;In the age of AI-assisted development, this problem is becoming increasingly common. Here's why:&lt;/p&gt;

&lt;h3&gt;
  
  
  The AI Workflow Reality
&lt;/h3&gt;

&lt;p&gt;When you're working with AI coding assistants like Claude, Cursor, or GitHub Copilot, your development flow changes dramatically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rapid iteration&lt;/strong&gt; - You're generating and testing code much faster than traditional coding&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context switching&lt;/strong&gt; - You jump between multiple features, experiments, and ideas quickly&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exploratory coding&lt;/strong&gt; - AI makes it easy to try "what if" scenarios without the usual friction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Less ceremony&lt;/strong&gt; - You spend less time in IDE scaffolding and more time actually writing code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This speed is powerful, but it comes with a cost: you're less likely to follow the traditional "create branch → switch → commit" ritual. You might ask your AI assistant to implement something, test it, iterate a few times, and suddenly realize you have 5-10 commits on &lt;code&gt;main&lt;/code&gt; that should be on a feature branch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why You Can't Just Wing It
&lt;/h3&gt;

&lt;p&gt;Some developers think "I'll just be more careful" or "I'll fix it manually when it happens." But here's why that doesn't work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Manual fixes are error-prone&lt;/strong&gt; - The traditional fix involves &lt;code&gt;git reset&lt;/code&gt;, cherry-picking, or rebasing. Miss one step and you could lose work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It breaks your flow&lt;/strong&gt; - Spending 10 minutes Googling git commands and carefully executing them kills the momentum AI coding gives you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It happens more often&lt;/strong&gt; - With AI, you're moving fast. This isn't a once-a-month mistake anymore—it could be weekly or daily.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team friction&lt;/strong&gt; - Accidentally pushing bad commits to &lt;code&gt;main&lt;/code&gt; because you forgot to move them creates problems for your whole team.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The Automation Mindset
&lt;/h3&gt;

&lt;p&gt;If you're embracing AI to speed up your development, you need to embrace automation for the mistakes too. This alias is a safety net that matches your new development speed. One command, three seconds, problem solved—and you're back to building.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;You have several commits on the wrong branch, but you haven't pushed them yet. You need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move those commits to a new branch&lt;/li&gt;
&lt;li&gt;Reset your current branch back to match the remote&lt;/li&gt;
&lt;li&gt;Not lose any of your work&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;Add this git alias to rescue your commits in one 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 config &lt;span class="nt"&gt;--global&lt;/span&gt; alias.move-commits &lt;span class="s1"&gt;'!f() { \
    CURRENT=$(git branch --show-current); \
    NEW_BRANCH=${1:-rescued-commits}; \
    git branch $NEW_BRANCH &amp;amp;&amp;amp; \
    git reset --hard origin/$CURRENT &amp;amp;&amp;amp; \
    git checkout $NEW_BRANCH; \
}; f'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Use It
&lt;/h2&gt;

&lt;p&gt;Once you've added the alias, using it is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Move commits to a branch named "feature-branch"&lt;/span&gt;
git move-commits feature-branch

&lt;span class="c"&gt;# Or use the default name "rescued-commits"&lt;/span&gt;
git move-commits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What It Does
&lt;/h2&gt;

&lt;p&gt;Here's the step-by-step breakdown:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Captures your current branch name&lt;/strong&gt; - So it knows which branch to reset&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creates a new branch at your current position&lt;/strong&gt; - This preserves all your commits&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resets your original branch to match the remote&lt;/strong&gt; - Using &lt;code&gt;git reset --hard origin/BRANCH&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Checks out the new branch&lt;/strong&gt; - So you can continue working right away&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Manual Installation
&lt;/h2&gt;

&lt;p&gt;If you prefer to edit your &lt;code&gt;~/.gitconfig&lt;/code&gt; file directly, add this to the &lt;code&gt;[alias]&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="nn"&gt;[alias]&lt;/span&gt;
    &lt;span class="py"&gt;move-commits&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"!f() { CURRENT=$(git branch --show-current); NEW_BRANCH=${1:-rescued-commits}; git branch $NEW_BRANCH &amp;amp;&amp;amp; git reset --hard origin/$CURRENT &amp;amp;&amp;amp; git checkout $NEW_BRANCH; }; f"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This assumes your remote is named &lt;code&gt;origin&lt;/code&gt;. If you use a different remote name, replace &lt;code&gt;origin/&lt;/code&gt; with your remote name in the alias.&lt;/li&gt;
&lt;li&gt;This only works for commits that haven't been pushed yet. If you've already pushed, you'll need a different approach.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;--hard&lt;/code&gt; reset means any uncommitted changes will be lost, so make sure everything is committed before running this command.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why This Works
&lt;/h2&gt;

&lt;p&gt;The trick is understanding that in git, branches are just pointers to commits. When you create a new branch, you're creating a new pointer to your current commit. Then you can safely move the old branch pointer back to where it should be, without losing any work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Save yourself the headache next time you commit to the wrong branch. Add this alias and rescue your commits with a single command.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Building tools that make developers faster is what I do. If your team &lt;br&gt;
is scaling AI-assisted development and needs someone who understands &lt;br&gt;
both the velocity and the chaos—let's talk.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>productivity</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
