goelakash
goelakash

Reputation: 2519

Add different commits from a single branch to different branches

Newbie question:

I made 3 commits to master branch, instead of opening 3 new branches for the changes (they were in different contexts).

What is the easiest way to cherry-pick specific commits and apply them to a new branch?

Upvotes: 1

Views: 29

Answers (1)

VonC
VonC

Reputation: 1323753

You can start 3 branches from master~3, in order to begin before the three incorrect commits:

git checkout -b fix1 master~3
git cherry-pick master~2

git checkout -b fix2 master~3
git cherry-pick master~1

git checkout -b fix3 master~3
git cherry-pick master

(Assuming the three commits were fix1, fix2 and fix3, from oldest master~2 to most most recent master SHA1 commit)

Then you can reset master (provided you did not push it already, or if you are alone working on master)

git checkout master
git reset hard @~3

Upvotes: 2

Related Questions