How to fix committing to the wrong Git branch?

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

    4 years late on the topic, but this might be helpful to someone.

    If you forgot to create a new branch before committing and committed all on master, no matter how many commits you did, the following approach is easier:

    git stash                       # skip if all changes are committed
    git branch my_feature
    git reset --hard origin/master
    git checkout my_feature
    git stash pop                   # skip if all changes were committed
    

    Now you have your master branch equals to origin/master and all new commits are on my_feature. Note that my_feature is a local branch, not a remote one.

提交回复
热议问题