---
title: "How do I move commits I made on main to a new branch?"
description: "Committed to main by mistake? Create a branch at your current commit, then move main back to match the remote, so your work lives on its own branch."
url: https://www.danny.engineering/answer/git/how-do-i-move-commits-i-made-on-main-to-a-new-branch
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# How do I move commits I made on main to a new branch?

## The short answer


If you have not pushed yet, create the new branch where you are, switch to it, then move main back to where the remote has it. Your commits stay on the new branch and main is clean again.


```bash
git fetch origin
git switch -c feature/search      # new branch at your current commit
git branch -f main origin/main    # move main back to the remote's version
```


Nothing is copied or rewritten. Your commits were already in the repository; you are only changing which branch names point at them.


## Why it happens


A branch in Git is a label on a commit. When you commit while on main, the main label moves forward to your new commit. Nothing about the commits themselves says "main".


So "moving" commits to another branch is really two steps: put a new label on your latest commit, and move the main label back to the commit it should be on. Because no commits are rewritten, this is safe as long as main has not been pushed with your work on it.


## How to fix it


### You have not pushed


This is the common case. Fetch first so `origin/main` is up to date, then check how far ahead you are:


```bash
git fetch origin
git log --oneline origin/main..main
```


That lists the commits that are on your main but not on the remote. Those are the ones that will move. Then:


```bash
git switch -c feature/search
git branch -f main origin/main
```


`git branch -f` moves a branch you are not currently on, so it does not touch your working tree or any uncommitted changes. You stay on the feature branch, ready to push it and open a pull request:


```bash
git push -u origin feature/search
```


### Using reset instead


You will often see this version, which does the same thing from the main side:


```bash
git branch feature/search          # create the branch, stay on main
git reset --hard origin/main       # move main back
git switch feature/search
```


It works, but `git reset --hard` also throws away any uncommitted changes in your working tree. Commit or stash them first, or use the `git branch -f` version above, which avoids the risk.


### Move only some of the commits


If main had a mix of commits that belong there and commits that do not, put the right ones on the new branch with cherry-pick:


```bash
git switch -c feature/search origin/main
git cherry-pick a1b2c3d 0a1b2c3
git branch -f main origin/main
```


If some of those commits really should stay on main, point main at the right commit instead of `origin/main`.


### Move them to an existing branch


If the branch you meant to commit to already exists, replay the commits onto it:


```bash
git switch feature/search
git cherry-pick origin/main..main
git branch -f main origin/main
```


The range `origin/main..main` means "commits on main that are not on origin/main", applied oldest first. Resolve any conflicts as they come up.


### You already pushed to main


Now other people may have pulled your commits, so moving main backwards would rewrite shared history. Do not force push main. Instead:

1. Create the feature branch from your latest commit and push it, so the work is saved.
2. Undo the commits on main with `git revert`, which adds new commits that reverse the changes without rewriting history.

```bash
git branch feature/search
git push -u origin feature/search
git revert --no-edit origin/main~2..origin/main
git push
```


Adjust the range to match the commits you pushed. Be aware that when the feature branch is later merged into main, Git sees the original commits as already merged and reverted. You may need to revert the revert, or rebuild the feature branch from fresh commits.


## Common pitfalls

- Forgetting to fetch first, so `origin/main` is out of date and main ends up in the wrong place.
- Running `git reset --hard` with uncommitted changes. They are gone. `git reflog` can recover commits, but not changes that were never committed.
- Force pushing main to undo a push. On a shared repository this breaks everyone else's copy; protected branches usually block it anyway.
- Moving the commits but leaving the new branch unpushed. Until it is pushed, the work only exists on your machine.
- Getting the cherry-pick range backwards. `A..B` means commits reachable from B but not from A.

## Related reading

- [Git documentation: git branch](https://git-scm.com/docs/git-branch)
- [Git documentation: git cherry-pick](https://git-scm.com/docs/git-cherry-pick)
