thesecretmaster thesecretmaster - 5 months ago 17
Git Question

Undoing an old fast foreward commit

Looking back at some work I did, there was a fast forward commit that really makes it hard to tell what was worked on, so I would like to move that commit to it's own branch. Here is my current situation:


And lets say that C is the one commit I want to move. C also (from the FF commit) has it's own branch that is on it (
). I want it to look like:

\ /
\ /

So C is on
and C2 is a merge commit and A, B, and D are all on

Here is some extra info:

$ git reflog show branch2
245c52c branch2@{0}: commit: C
7c2a064 branch2@{1}: branch: Created from HEAD

$ git reflog show master
c0a8aff master@{0}: commit: D
244c52c master@{1}: merge branch2: Fast-forward
8a93b2f master@{2}: commit: B
fe872d8 master@{3}: commit: A


If you have:

 A--B--C--D (master)

You can reset master and redo the merge (in a non-FF way), then cherry-pick D (or rebase, if D is actually multiple commits)

git checkout master
git branch tmp # mark D if you need to
git reset --hard B
git merge --no-ff branch2

     \   /
      --C (branch2)
          --D (tmp)


git cherry-pick D

If D was actually composed of multiple commits, the 'tmp' branch would come in handy:

git rebase --onto master branch2 tmp

That would replay all commits after branch2 (so excluding C), up until tmp HEAD (that is D) on top of master (C2)