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

后端 未结 2 779
[愿得一人]
[愿得一人] 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: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
    

提交回复
热议问题