Git Alias: Rescue Commits from the Wrong Branch

Published: (December 3, 2025 at 12:34 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Cover image for Git Alias: Rescue Commits from the Wrong Branch

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‑pronegit 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 main affect 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:

  1. Move those commits to a new branch.
  2. Reset the original branch to match the remote.
  3. 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

  1. Captures the current branch name – determines which branch to reset.
  2. Creates a new branch at the current commit – preserves all your work.
  3. Resets the original branch to the remote stategit reset --hard origin/BRANCH.
  4. 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. Replace origin/ with your remote name if different.
  • It only works for commits that haven’t been pushed. Pushed commits require a different approach.
  • The --hard reset 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.

Back to Blog

Related posts

Read more »

Branch development with git

!Cover image for Branch development with githttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-up...