How to fix committing to the wrong Git branch?

前端 未结 11 1437
小鲜肉
小鲜肉 2020-11-29 14:09

I just made a perfectly good commit to the wrong branch. How do I undo the last commit in my master branch and then take those same changes and get them into my upgrade bran

11条回答
  •  广开言路
    2020-11-29 14:54

    To elaborate on this answer, in case you have multiple commits to move from, e.g. develop to new_branch:

    git checkout develop # You're probably there already
    git reflog # Find LAST_GOOD, FIRST_NEW, LAST_NEW hashes
    git checkout new_branch
    git cherry-pick FIRST_NEW^..LAST_NEW # ^.. includes FIRST_NEW
    git reflog # Confirm that your commits are safely home in their new branch!
    git checkout develop
    git reset --hard LAST_GOOD # develop is now back where it started
    

提交回复
热议问题