undo “git filter-branch … — --all”? in one command?

后端 未结 2 780
[愿得一人]
[愿得一人] 2021-02-09 02:26

I started messing around with git filter-branch. The --all option rewrites all branches and tags. Cool. git filter-branch creates a backup of all re

相关标签:
2条回答
  • 2021-02-09 02:49

    Ehmm, move all refs from .git/refs/original to their place in .git/refs/heads? I mean literally move, these are plain text files. Also you may take a look into .git/packed-refs if it exists.

    0 讨论(0)
  • 2021-02-09 02:55

    Here's a simple and safe way to undo a filter-branch:

    git fetch . +refs/original/*:*
    

    If the currently checked out branch (HEAD) is one of the branches to be restored, this command will fail. You can run git checkout --detach beforehand to let git fetch update all branches. Remember to checkout a branch after running git fetch!

    Once the refs have been successfully restored, you can delete the refs/original refs safely with this command:

    git for-each-ref refs/original --format='delete %(refname) %(objectname)' | git update-ref --stdin
    

    Old answer:

    git for-each-ref refs/heads refs/tags \
            --format='git update-ref "%(refname)" "%(refname)@{1 hour ago}"'
    

    For added safety you could test whether the filter-branch actually did update the ref:

    git for-each-ref refs/heads refs/tags --format='
            { git reflog -1 "%(refname)" | sed -n "/filter-branch: rewrite$/Q1"; } \
                      || git update-ref -m "reset to before filter-branch" \
                                 %(refname) %(refname)@{1}' | sh -x
    
    0 讨论(0)
提交回复
热议问题