Git cancel a revert

前端 未结 3 764
半阙折子戏
半阙折子戏 2020-12-29 19:32

In git let say I commit A and B

A---[B]

But then I revert with

git revert HEAD

So I am there now:

相关标签:
3条回答
  • 2020-12-29 20:14

    As you create a new commit which reverts another commit you can treat it like a normal commit.

    So basically you have many choices, such as

    • git rebase -i (remove the revert commit)
    • git reset --hard <commitID> (reset to the commit before the revert, you'll lose all local changes)
    • git reset --soft <commitID> (same as above but keeps local changes)
    • technically you can use git revert <commitId> to revert your revert
    0 讨论(0)
  • 2020-12-29 20:20

    You have two general choices:

    • Revert the revert commit (creating a second revert commit that takes you back to the original)
    • Throw away the revert commit with

    git reset --hard HEAD^

    The second option is only appropriate if you have not pushed your changes anywhere else. In fact, if you haven't pushed your first revert commit anywhere yet, you can simply use

    git reset --hard

    to roll back without creating any revert commits at all.

    0 讨论(0)
  • 2020-12-29 20:25

    If you haven't done it completely, i.e., in gitbash you see something like:

    Username@Host MINGW64 /d/code/your-project (feature|REVERTING)
    

    then you can use git revert --abort to abort.

    If you have done it.. just don't reset, the changes are still there. Use git reset to change the state. Instead of --hard, you can also use --soft(keep all the changes).

    git reset --soft HEAD^ // discard the last commit, keeping all the changes after that
    

    Also, if you want to revert more than 1 commits:

    git reset --soft HEAD~3 // discart last 3 commits. Don't know if it works for commits of others.
    
    0 讨论(0)
提交回复
热议问题