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.
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.
_10git fetch origin_10git switch -c feature/search # new branch at your current commit_10git 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:
_10git fetch origin_10git 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:
_10git switch -c feature/search_10git 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:
_10git push -u origin feature/search
Using reset instead
You will often see this version, which does the same thing from the main side:
_10git branch feature/search # create the branch, stay on main_10git reset --hard origin/main # move main back_10git 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:
_10git switch -c feature/search origin/main_10git cherry-pick a1b2c3d 0a1b2c3_10git 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:
_10git switch feature/search_10git cherry-pick origin/main..main_10git 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:
- Create the feature branch from your latest commit and push it, so the work is saved.
- Undo the commits on main with
git revert, which adds new commits that reverse the changes without rewriting history.
_10git branch feature/search_10git push -u origin feature/search_10git revert --no-edit origin/main~2..origin/main_10git 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/mainis out of date and main ends up in the wrong place. - Running
git reset --hardwith uncommitted changes. They are gone.git reflogcan 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..Bmeans commits reachable from B but not from A.
Related reading
#Was this resource helpful?