How to fix committing to a wrong branch in Git

  1. Make sure you are on the affected branch: git checkout wrong-branch
  2. Soft-reset the HEAD to the previous commit. In the words of git status, this will leave all your changed files as “changes to be committed”: git reset --soft HEAD^
  3. Stash away the uncommited changes resulting from the previous step: git stash
  4. Apply the stashed changes onto the right branch: git checkout right-branch && git stash pop
  5. Fix any resulting merge conflicts, then run git commit -a
  6. If necessary, push the modified (wrong) branch to the remote: git checkout wrong-branch && git push origin --force