How to revert multiple git commits?

后端 未结 13 2372
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 07:36

I have a git repository that looks like this:

A <- B <- C <- D <- HEAD

I want the head of the branch to point to A, i.e. I want B

相关标签:
13条回答
  • 2020-11-22 08:08

    The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use git revert in conjunction with git rev-list. The latter one will provide you with a list of commits, the former will do the revert itself.

    There are two ways to do that. If you want the revert multiple commits in a single commit use:

    for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-commit $i; done
    

    this will revert a group of commits you need, but leave all the changes on your working tree, you should commit them all as usual afterward.

    Another option is to have a single commit per reverted change:

    for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-edit -s $i; done
    

    For instance, if you have a commit tree like

     o---o---o---o---o---o--->    
    fff eee ffffd ccc bbb aaa
    

    to revert the changes from eee to bbb, run

    for i in `git rev-list eee^..bbb`; do git revert --no-edit -s $i; done
    
    0 讨论(0)
提交回复
热议问题