How to undo a successful “git cherry-pick”?

后端 未结 6 1760
无人及你
无人及你 2021-01-30 02:12

On a local repo, I\'ve just executed git cherry-pick SHA without any conflicts or problems. I then realized I didn\'t want to do what I just did. I have not pushed

6条回答
  •  梦毁少年i
    2021-01-30 02:40

    If possible, avoid hard resets. Hard resets are one of the very few destructive operations in git. Luckily, you can undo a cherry-pick without resets and avoid anything destructive.

    Note the hash of the cherry-pick you want to undo, say it is ${bad_cherrypick}. Do a git revert ${bad_cherrypick}. Now the contents of your working tree are as they were before your bad cherry-pick.

    Repeat your git cherry-pick ${wanted_commit}, and when you're happy with the new cherry-pick, do a git rebase -i ${bad_cherrypick}~1. During the rebase, delete both ${bad_cherrypick} and its corresponding revert.

    The branch you are working on will only have the good cherry-pick. No resets needed!

提交回复
热议问题