Move commits on master to a new branch

December 22, 2013

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!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: