Committed on the Wrong Branch

The symptom

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

Terminal
$ git branch --show-current    # which branch am I on?
$ git log --oneline -1         # the commit that just landed there
In VS Code
  1. Look at the bottom-left status bar — the branch icon shows the current branch name (e.g. main)
  2. Open the Source Control panel → the Graph section shows your new commit sitting on that branch

No harm done yet. The commit is safe — it's just wearing the wrong label.

What it means

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.

main feature
The fix is just moving branch labels — the commit itself never moves.

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.

Fix 1 — the work should be on a NEW branch

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

Terminal
$ 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
In VS Code
  1. Click the branch name in the status barCreate new branch… — the new branch starts right here, your commit rides along
  2. Switch back to main with the same status-bar branch picker
  3. The rewind step has no safe one-click button — run git reset --hard HEAD~1 in the integrated terminal (Terminal → New Terminal), then switch to your new branch

Count 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.

Fix 2 — it belongs on an EXISTING 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

Terminal
$ git log --oneline -1        # copy the commit hash
$ git switch feature-login
$ git cherry-pick a1b2c3d
$ git switch main
$ git reset --hard HEAD~1
In VS Code
  1. Plain VS Code has no built-in cherry-pick button — use the integrated terminal (Terminal → New Terminal)
  2. With the GitLens extension, you can right-click the commit in its Commits view → Cherry Pick Commit… instead

cherry-pick makes the copy; the final reset --hard peels the original off main.

Only safe before pushing

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

Frequently Asked Questions

I committed to main instead of my branch — how do I move the commit?
Label the work where it is with 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.
Can I move a commit I already pushed?
Don't rewind a pushed main — others may have pulled it, and rewriting shared history causes conflicts. Instead, undo the pushed commit with git revert (see Undo Commits) and redo the work on the right branch.
What does git cherry-pick do?
It copies one existing commit onto your current branch — same changes, new commit with a new hash. Grab the hash from git log --oneline, switch to the target branch, then run git cherry-pick <hash>.
Will git reset --hard delete my commit?
No — it only moves the branch label back; the commit itself still exists in Git's database. If you reset before saving it under another branch, git reflog can find the hash so you can restore it — see Recover Lost Work.