How to fix committing to the wrong Git branch?

前端 未结 11 1398
小鲜肉
小鲜肉 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:42

    If you have a clean (un-modified) working copy

    To rollback one commit (make sure you note the commit's hash for the next step):

    git reset --hard HEAD^
    

    To pull that commit into a different branch:

    git checkout other-branch
    git cherry-pick COMMIT-HASH
    

    If you have modified or untracked changes

    Also note that git reset --hard will kill any untracked and modified changes you might have, so if you have those you might prefer:

    git reset HEAD^
    git checkout .
    

提交回复
热议问题