JavaEar 专注于收集分享传播有价值的技术资料

How to undo a revert in git?

I need to undo a revert done in git and pushed to a remote repository. The repository is not used by anyone else. I did this with the backthought that a revert is just another commit. Thus I tried git reset and then push but it did not seem to work. Does anyone know how this can be done?

-> git reset --hard HEAD~1
HEAD is now at ee7abe9 <msg>
-> git push -f origin myBranch
Total 0 (delta 0), reused 0 (delta 0)
remote: Processing changes: done    
To 'ssh://remoteRepo'
  ! [remote rejected] mo_cache-BLACK -> mo_cache-BLACK (non-fast forward)
error: failed to push some refs to 'ssh://remoteRepo'

4个回答

    最佳答案
  1. You're bypassing the Gerrit review (not using refs/for/) and trying to rewrite the history (-f). I think you don't have permissions for that. It will be easier if you:

    1. Go to the Gerrit web UI
    2. Search the Change you want to revert
    3. Click on the "Revert" button

    A change will be created, just review, approve and submit it.

  2. 参考答案2
  3. You can try

    git push origin +myBranch
    

    The + sign will force updating remote branch

  4. 参考答案3
  5. This can achieve in 2 ways:

    • If changes pushed on git. git checkout HEAD^^ -- . and then git add -A && git commit .. git push.
    • If changes are not pushed on git git reset --hard HEAD^ (be more careful with it, though!)
  6. 参考答案4
  7. I usually like to have all the commits on the branch path unless I've pushed a password by mistake or some sensitive information. Therefore I suggest that you "revert the revert" by applying git revert <commit id of your previous revert> then commit and push that to the origin.