Git undo pushed merge and delete its history

前端 未结 2 1190
一个人的身影
一个人的身影 2021-01-28 08:52

I made an accidental merge to master and pushed it, now master has all the commits from dev. I want to revert the commits from master and delete its history without changing dev

2条回答
  •  心在旅途
    2021-01-28 09:10

    The last commit can be removed with: git reset --hard HEAD^.

    Sometimes there might be situations, when you need to remove a commit from the "middle" of branch. Here comes to the rescue interactive rebase: git rebase -i ^. You just need to drop an unwanted commit (it should appear at the top of the list).

    If the changes you undone were available in remote, they also should be removed from there with force push: git push --force.

    However, rewriting the history of a branch, that has been shared with someone, is not a good way of undoing changes. Instead, consider to use git revert . This is the more robust and correct way in this situation.

    I'd recommend to read Git Branching - Rebasing. Chapter "The Perils of Rebase" is explaining why rewriting the public history is not a good idea.

提交回复
热议问题