This solution appears to work only for certain, patched versions of git. See a new answer pointing to workarounds and another answer and subsequent comments for a hint which versions may work.
I wrote a blog post on how to effectively use the excludesfile
for different branches, like one for public github and one for heroku deployment.
Here’s the quick and dirty:
$ git branch public_viewing
$ cd .git/
$ touch info/exclude_from_public_viewing
$ echo "path/to/secret/file" > info/exclude_from_public_viewing
then in the .git/config file add these lines:
[core]
excludesfile = +info/exclude
[branch "public_viewing"]
excludesfile = +info/exclude_from_public_viewing
Now all the global ignore stuff is in the info/exclude
file and the branch specific is in the info/exclude_from_public_viewing
Hope that helps!
http://cogniton-mind.tumblr.com/post/1423976659/howto-gitignore-for-different-branches