git: re-checkout files after creating smudge filter

后端 未结 2 1419
清歌不尽
清歌不尽 2020-12-01 08:53

Situation: I\'ve just cloned a git repo, and then I configure the smudge filter for the repo. There are .gitattributes files scattered around the repo that spec

相关标签:
2条回答
  • 2020-12-01 09:14

    Simply re-checkout everything.

    cd /path/to/your/repo
    git stash save
    rm .git/index
    git checkout HEAD -- "$(git rev-parse --show-toplevel)"
    git stash pop
    

    The smudge filter will be applied at that new checkout.

    Note, as seen in this answer, you need to remove the index in order to force the filter to run again.

    Alexander Amelkin comments below:

    I have created an alias 'reattr' to perform all those steps and now I am happy.

    reattr = !sh -c "\"git stash save; rm .git/index; git checkout HEAD -- \\\"$(git rev-parse --show-toplevel)\\\"; git stash pop\""
    

    (multi-line for readability)

    reattr = !sh -c "\"git stash save; \
                       rm .git/index; \
                       git checkout HEAD -- \\\"$(git rev-parse --show-toplevel)\\\"; \
                       git stash pop\""
    
    0 讨论(0)
  • 2020-12-01 09:19

    You can remove the Git index and let Git rescan it to aware the changes. Then you can checkout all the files which have a smudge filter on them.

    # remove Git index
    rm .git/index
    
    # rescan index
    git reset HEAD -- .
    
    # checkout all the files which have a smudge filter on them
    git ls-files --modified | grep -v .gitattributes | awk '{print "git checkout HEAD -- \""$1"\""}' | bash
    

    Note: Save your uncommitted changes before the re-checkout, otherwise, all your modifications on those smudge-filter-applied files will be overridden.

    0 讨论(0)
提交回复
热议问题