Git Alias: Rescue Commits from the Wrong Branch
Source: Dev.to

Why This Matters More Than Ever
In the age of AI‑assisted development, committing to the wrong branch is becoming increasingly common.
The AI Workflow Reality
When using AI coding assistants (Claude, Cursor, GitHub Copilot, etc.) your workflow changes dramatically:
- Rapid iteration – code is generated and tested much faster.
- Context switching – you jump between multiple features and experiments.
- Exploratory coding – trying “what if” scenarios is cheap.
- Less ceremony – you spend less time in IDE scaffolding and more time writing code.
This speed reduces the likelihood of following the traditional “create branch → switch → commit” ritual, leading to accidental commits on main.
Why You Can’t Just Wing It
- Manual fixes are error‑prone –
git reset, cherry‑picking, or rebasing can lose work if a step is missed. - It breaks your flow – spending minutes looking up git commands kills the momentum AI coding provides.
- It happens more often – with AI you may make this mistake weekly or daily.
- Team friction – accidental commits to
mainaffect the whole team.
The Automation Mindset
If you embrace AI to speed up development, you should also automate the recovery from mistakes. The following git alias acts as a safety net: one command, a few seconds, and you’re back on track.
The Problem
You have several local commits on the wrong branch (not yet pushed) and need to:
- Move those commits to a new branch.
- Reset the original branch to match the remote.
- Preserve all work.
The Solution
Add this git alias to rescue your commits in a single 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
# 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
- Captures the current branch name – determines which branch to reset.
- Creates a new branch at the current commit – preserves all your work.
- Resets the original branch to the remote state –
git reset --hard origin/BRANCH. - Checks out the new branch – lets you continue working immediately.
Manual Installation
If you prefer editing ~/.gitconfig directly, add the following 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
- The alias assumes your remote is named
origin. Replaceorigin/with your remote name if different. - It only works for commits that haven’t been pushed. Pushed commits require a different approach.
- The
--hardreset discards any uncommitted changes, so ensure everything is committed before running the alias.
Why This Works
In Git, branches are just pointers to commits. Creating a new branch adds a pointer to the current commit, after which you can safely move the original branch pointer back to the remote state 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.