git branch feature-name, rewind main with git reset --hard HEAD~1, then git switch feature-name. Branches are just movable labels, so the commit never actually moves — only the labels do.Guide
You committed — then noticed the status bar says the wrong branch. Nothing is lost; you just move the label.
You meant to commit on feature-login, but you were actually on main.
Usually you notice one of two ways: the branch name in VS Code's status bar (bottom-left) says
main, or you check where the commit landed:
Confirm where the commit went
$ git branch --show-current # which branch am I on?
$ git log --oneline -1 # the commit that just landed there No harm done yet. The commit is safe — it's just wearing the wrong label.
A branch is not a container — it's a movable label pointing at a commit. Your commit exists exactly once, in one place; the fix is never "move the commit", it's "move the labels so the right branch points at it". That's why every fix below is quick and safe.
Mental model: think of branch names as sticky notes on commits. git branch adds a note, git reset peels one off and sticks it on an earlier commit. The commits underneath never change.
Most common case: the commit was meant to start a feature branch that doesn't exist yet. Put a
new label where you are, rewind main, and hop over:
Move the commit to a brand-new branch
$ git branch feature-login # label the work where it is
$ git reset --hard HEAD~1 # move main back one commit (two commits: HEAD~2)
$ git switch feature-login git reset --hard HEAD~1 in the integrated terminal (Terminal → New Terminal), then switch to your new branchCount your commits first with git log --oneline — reset back exactly as many as you made on the wrong branch.
Read it slowly: step 1 saves the work under a new name. Step 2 moves the main label
back to where it was before your commit — the commit doesn't vanish, because
feature-login is still pointing at it. Step 3 puts you on the right branch.
If the branch already exists, use git cherry-pick — it copies one commit onto your
current branch (same changes, new commit) — then remove the original from main:
Copy the commit over, then clean up main
$ git log --oneline -1 # copy the commit hash
$ git switch feature-login
$ git cherry-pick a1b2c3d
$ git switch main
$ git reset --hard HEAD~1 cherry-pick makes the copy; the final reset --hard peels the original off main.
Both fixes rewind main with git reset --hard, which rewrites history.
That's completely fine while the commit only exists on your machine. But if you already
pushed it, other people may have pulled it — rewinding now would put your
history in conflict with theirs.
For a pushed commit, leave main alone: undo it with
git revert (a new commit that reverses the old one), or simply
redo the work properly on a branch and open a fresh pull request.
Escape hatch: reset one commit too far, or reset before labelling? The commit still exists — Recover Lost Work shows how git reflog finds it again.
Up next: the other "where am I?" surprise — the status bar shows a commit hash instead of a branch name. That's Detached HEAD, and it's less scary than it sounds.
FAQ
git branch feature-name, rewind main with git reset --hard HEAD~1, then git switch feature-name. Branches are just movable labels, so the commit never actually moves — only the labels do.git revert (see Undo Commits) and redo the work on the right branch.git log --oneline, switch to the target branch, then run git cherry-pick <hash>.git reflog can find the hash so you can restore it — see Recover Lost Work.