How to undo “git push --mirror”?

前端 未结 2 861
北恋
北恋 2021-01-07 19:41

On a git/github project I am working on a branch. Upon a push, it said the following:

git push
To git@github.com:...
 ! [rejected]        master -> master         


        
2条回答
  •  天命终不由人
    2021-01-07 20:21

    You pushed to the default push target, git@github.com. This means, that git@github.com was a remote in you source repo.

    That implies, that the refs deleted from the server would still be in the remote locally after the push. Do not update the remotes (!).

    Verify this by doing

    git branch -a
    

    on the side you pushed from (local).

    It will probably show the refs that were deleted from the remote server.

    [to be continued]

    You could do something like:

    for-each-ref refs/remotes/origin | while read sha type name
    do 
        git branch "rescue_$(basename "$name")" "$sha"
    done
    

    to recover the branches locally. They will be named prefixed with rescue_ just as a precaution (in case you get funny or conflicting ref names).

    Replace origin with the name of your remote


    Test script

    In case you want to test the procedure in a controlled environment, here is my approach condensed to minimum steps (execute in an empty dir, e.g. /tmp/work)

    git init A; (cd A; touch test; git add test; git commit -m initial; git branch test1; git branch test2; git branch test3)
    git clone A B
    (cd B; git push --mirror origin; git branch -a)
    cd A
    git for-each-ref refs/remotes/origin | while read sha type name; do git branch "rescue_$(basename "$name")" "$sha"; done
    git branch -a
    

    Note how in this version, I cd into A - which would be your github repo. You could git clone --mirror git@github.com:... local_rescue in order to get a suitable local version of that.

    I recommend you play around getting to terms with the procedure before trying it out. It never hurts to backup your repositories along the way.

提交回复
热议问题