44

I am learning git-flow and I just did git flow feature finish <feature-name>, which merged my feature branch to develop and removed it.

Instead of this, I want to push the feature branch to github, so I can merge it after a peer review.

So the question is, how do I 'undo' this command. Or in other words , how can I move my last two commits from develop to my feature branch?

2 Answers 2

83

These steps should do the trick:

Get the sha's needed:

git log

<sha1> is the commit right before the merge
<sha2> is the last commit on develop before you started working on the feature

git checkout develop
git checkout -b feature/<feature-name>
git reset <sha1> --hard
git checkout develop
git reset <sha2> --hard

Push your feature branch.

3
  • 2
    I had to read this few times before I realized why reset on the feature branch (third line). Looks obvious in hidsight :) Thanks! (btw the <sha's> get lost in the markup)
    – hakunin
    Commented Nov 23, 2012 at 14:15
  • 4
    If git flow feature finish was your last action, you can just do git reset --hard HEAD^ in both branches instead of using commit's shas, as the commits would be the lasts in boths cases.
    – clemlatz
    Commented Dec 3, 2016 at 10:17
  • 2
    And if like me, you're trying to undo a hotfix finish, you also need to reset the last commit in the master branch.
    – clemlatz
    Commented Dec 3, 2016 at 10:22
0

I found it easiest to just update master (we call it default) back to origin, with either

git checkout -B master origin/master

or the equivalent newer command

git switch -C master origin/master

checkout -B and switch -C (or --force-create) both move the branch to the new location if it already exists.

Update After thinking about it later, I half-considered deleting this, though it worked for me, because I suspect it might only work for the fast-forward case, rather than a regular merge. That also might explain why git reset --hard <SHA-from-reflog> didn't work for me and got me into a confused state, i.e., because I "reset" my local master rather than the feature branch head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.