11

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 specify the filter that should be used on the files at checkout. But since I setup the filter after the checkout (clone), none of the files were processed.

How can I tell git to go through the repo, find all the .gitattributes files, and update (re-checkout, apply filter, whatever) all the files which have a smudge filter on them?

2 Answers 2

15

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\""
9
  • 2
    Doesn't work. Smudge not applied. I'm guessing git is intelligent enough to know the file wasn't modified, so it thinks it doesn't need to be checked out and ignores it. The only way I've been able to get git to re-checkout the file is to touch the file, and then check it out. But I want want to touch every file in the whole repo.
    – phemmer
    Commented Feb 9, 2014 at 1:41
  • @Patrick Strange, I have been doing that for months now successfully: github.com/VonC/compileEverything/blob/master/gitlab/…. Try a git checkout HEAD -- /path/to/your/repo
    – VonC
    Commented Feb 9, 2014 at 1:47
  • That does it, thanks. I'll add it to the script I use to set up the filter, but with a few tweaks: 1) add git stash save and git stash pop (if save did anything) before and after the checkout. This is just in case I was working on anything. 2) git checkout HEAD -- "$(git rev-parse --show-toplevel)" so I don't have to hard code the path.
    – phemmer
    Commented Feb 9, 2014 at 2:43
  • 1
    @AlexanderAmelkin I have edited the answer to include the "rm .git/index" step in order to force any filter or attribute directive to re-apply on checkout.
    – VonC
    Commented Oct 17, 2016 at 12:22
  • 1
    @AlexanderAmelkin Thank you for this feedback. I have included your comment and alias in the answer for more visibility.
    – VonC
    Commented Oct 18, 2016 at 8:59
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.