← Back to all fixes
Git

Committed to main instead of feature branch

The Issue

You ran `git commit` and `git push` but forgot to switch to your feature branch first.

Root Cause

Committed while on main. If not yet pushed, easy to fix. If pushed to a protected branch, need git revert.

How to Fix
  1. If NOT yet pushed: `git reset HEAD~1` to undo the commit while keeping changes, then switch branches and recommit
  2. If pushed to main: use `git revert <sha>` to create an undo commit and push it
  3. Move the changes to the right branch: `git stash` after reset, then `git checkout feature-branch && git stash pop`
  4. Prevent future accidents: enable branch protection rules on main to require PRs