tags:

views:

73

answers:

3

The situation: master is at X; quickfix1 is at X + 2 commits

then i started working on quickfix2, but by accident took quickfix1 as the source branch to copy, not the master. Now quickfix2 is at X + 2 commits + 2 relevant commits

Not I want to have a branch with quickfix2, but without the 2 commits that belong to quickfix1.

I tried to create a patch from a certain revision in quickfix2, but the patch doesn't preserve the commit history. Is there a way to save my commit history, but have a branch without changes in quickfix1?

Thanks!

+3  A: 

You can use git cherry-pick to just pick the commit that you want to copy over.

Probably the best way is to create the branch out of master, then in that branch use git cherry-pick on the 2 commits from quickfix2 that you want.

DJ
+4  A: 

This is a classic case of rebase --onto:

 # let's go to current master (X, where quickfix2 should begin)
 git checkout master

 # replay every commit *after* quickfix1 up to quickfix2 HEAD.
 git rebase --onto master quickfix1 quickfix2 

So you should go from

o-o-X (master HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
              \
               q2a--q2b (quickfix2 HEAD)

to:

      q2a'--q2b' (new quickfix2 HEAD)
     /
o-o-X (master HEAD) (quickfix2 HEAD)
     \ 
      q1a--q1b (quickfix1 HEAD)
VonC
Thanks a lot for the detailed answer!One thing, the changes ended up in my "tmp" branch. I double checked that i checked out the quickfix2 branch, but it still rebased into tmp. But that's easy to change :) Thanks!
alex
@alex: true, the sequence was much simpler than I thought. I have edited my answer to reflect the simpler `rebase --onto` command I should have used.
VonC
A: 

I believe it's:

git checkout master
git branch good_quickfix2
git checkout good_quickfix2
git cherry-pick quickfix2^
git cherry-pick quickfix2
Matthew Flaschen