Skip to main content
Code and think

GIT Committed to the wrong branch

When working with GIT it can happen that you committed to the wrong branch (or perhaps you created your working branch from the wrong one). In such case, the following commands should be able to rescue you:

Case a) you committed to master instead of your working branch: git branch new-branch-name git reset HEAD~ --hard git checkout new-branch-name

Case b) you committed to the wrong working branch: git reset HEAD~ --soft git stash git checkout -b name-of-the-correct-branch git stash pop git add . git commit -m "your message here";

A simpler way to solve the second case is to cherry-pick: git checkout name-of-the-correct-branch git cherry-pick master git checkout master git reset HEAD~ --hard

Reference