This is a problem that i encounter sometimes: I’ve committed on the master or dev branch when in fact i wanted to do those commits on a feature branch like feature-foobar. Let’s fix that.

Important: don’t do this when you’ve already pushed your commits to a remote.

  1. Checkout the branch in which you made the commits to be moved. e.g.: git checkout master
  2. Create and checkout the feature branch where you wanted the commits to be in te first place. e.g.: git checkout -b feature-foobar
  3. Checkout the main branch again. (See 1).
  4. Reset this branch a number of commits (use git log --decorate) to see how many commits you want gone from the main branch and moved to the new one. Use this number in the reset command. In my case, i want 5 commits moved: git reset HEAD~5

And that’s it really. Simple as that!