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 main instead of your feature branch. Your stomach drops. But don't panic—there's an easy fix.
Why This Matters More Than Ever
In the age of AI-assisted development, this problem is becoming increasingly common. Here's why:
The AI Workflow Reality
When you're working with AI coding assistants like Claude, Cursor, or GitHub Copilot, your development flow changes dramatically:
- Rapid iteration - You're generating and testing code much faster than traditional coding
- Context switching - You jump between multiple features, experiments, and ideas quickly
- Exploratory coding - AI makes it easy to try "what if" scenarios without the usual friction
- Less ceremony - You spend less time in IDE scaffolding and more time actually writing code
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 main that should be on a feature branch.
Why You Can't Just Wing It
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:
-
Manual fixes are error-prone - The traditional fix involves
git reset, cherry-picking, or rebasing. Miss one step and you could lose work. - It breaks your flow - Spending 10 minutes Googling git commands and carefully executing them kills the momentum AI coding gives you.
- It happens more often - With AI, you're moving fast. This isn't a once-a-month mistake anymore—it could be weekly or daily.
-
Team friction - Accidentally pushing bad commits to
mainbecause you forgot to move them creates problems for your whole team.
The Automation Mindset
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.
The Problem
You have several commits on the wrong branch, but you haven't pushed them yet. You need to:
- Move those commits to a new branch
- Reset your current branch back to match the remote
- Not lose any of your work
The Solution
Add this git alias to rescue your commits in one command:
git config --global alias.move-commits '!f() { \
CURRENT=$(git branch --show-current); \
NEW_BRANCH=${1:-rescued-commits}; \
git branch $NEW_BRANCH && \
git reset --hard origin/$CURRENT && \
git checkout $NEW_BRANCH; \
}; f'
How to Use It
Once you've added the alias, using it is simple:
# Move commits to a branch named "feature-branch"
git move-commits feature-branch
# Or use the default name "rescued-commits"
git move-commits
What It Does
Here's the step-by-step breakdown:
- Captures your current branch name - So it knows which branch to reset
- Creates a new branch at your current position - This preserves all your commits
-
Resets your original branch to match the remote - Using
git reset --hard origin/BRANCH - Checks out the new branch - So you can continue working right away
Manual Installation
If you prefer to edit your ~/.gitconfig file directly, add this to the [alias] section:
[alias]
move-commits = "!f() { CURRENT=$(git branch --show-current); NEW_BRANCH=${1:-rescued-commits}; git branch $NEW_BRANCH && git reset --hard origin/$CURRENT && git checkout $NEW_BRANCH; }; f"
Notes
- This assumes your remote is named
origin. If you use a different remote name, replaceorigin/with your remote name in the alias. - This only works for commits that haven't been pushed yet. If you've already pushed, you'll need a different approach.
- The
--hardreset means any uncommitted changes will be lost, so make sure everything is committed before running this command.
Why This Works
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.
Save yourself the headache next time you commit to the wrong branch. Add this alias and rescue your commits with a single command.
Building tools that make developers faster is what I do. If your team
is scaling AI-assisted development and needs someone who understands
both the velocity and the chaos—let's talk.
Top comments (0)