git
  • git-branch
  • git-remote
  • 2013-10-19 183 views 1 likes 
    1

    有一次,我意外地用錯誤的作者電子郵件推送了一個提交到遠程倉庫。我用Git歷史改寫搞砸了分支

    git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Niklas Rosenstein'; GIT_AUTHOR_EMAIL='<<email>>'; GIT_COMMITTER_NAME='Niklas Rosenstein'; GIT_COMMITTER_EMAIL='<<email>>';" HEAD 
    

    重寫歷史。然後我用

    $ git push origin development 
        Enter passphrase for key '----': 
        To [email protected]_cip:rosenstein/beak.git 
        ! [rejected]  development -> development (non-fast-forward) 
        error: failed to push some refs to '[email protected]_cip:rosenstein/beak.git' 
        hint: Updates were rejected because the tip of your current branch is behind 
        hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') 
        hint: before pushing again. 
        hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
    
    $ git pull origin development 
        Enter passphrase for key '----': 
        From gitlab_cip:rosenstein/beak 
        * branch   development -> FETCH_HEAD 
        Merge made by the 'recursive' strategy. 
    
    $ git push origin development 
        Enter passphrase for key '----': 
        Counting objects: 430, done. 
        Delta compression using up to 2 threads. 
        Compressing objects: 100% (336/336), done. 
        Writing objects: 100% (402/402), 43.93 KiB | 0 bytes/s, done. 
        Total 402 (delta 262), reused 85 (delta 65) 
        To [email protected]_cip:rosenstein/beak.git 
         dd06969..2b2ddb4 development -> development 
    

    這可能是一個巨大的錯誤。

    graph

    我得到了所有提交了一倍!錯誤的提交人的作者是固定的(完全重複,然後更改),但原始提交也存在!

    我後來讀了我(可能)應該使用

    git push origin --force development fix 
    

    我可以以某種方式解決這一問題?

    回答

    1

    如果沒有人最近從您的遠程回購拉,您可以:

    git checkout development 
    
    # Make sure you don't have any work in progress 
    # That will cancel the merge (assuming you didn't make any new commits on it) 
    git reset --hard HEAD^1 
    
    # replace the development branch by your new history 
    git push origin --force development 
    

    (摘自的git rev-parse man pageSPECIFYING REVISIONS部分)

    我爲了選擇第一父使用^1代替~1 (這是你的開發分支),而不是第二個父(這是來自原產地/開發者,合併到你的分支)。
    也就是說,~1(第一個祖先)可能也會起作用。

    +0

    不是'HEAD〜1'而不是'HEAD^1'? –

    +0

    非常感謝,它工作正常(用'HEAD〜1')! –

    +1

    @NiklasR好的,我剛剛解釋了一下爲什麼我最初用'^ 1'去了。 – VonC

    相關問題