I want to ignore certain files within a branch without having to rely on a tracked .gitignore
file that will be overwritten during merges with other branches.
I’ve closely followed a Stack Overflow answer along with the linked blog post, but my repo doesn’t seem to be recognizing the specified excludesfile
in my .git/config
. The Git documentation (git-config
, gitignore
) doesn’t seem to show how to specify and address an excludes file for a specific branch.
My .git/config
looks like this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
excludesfile = +/info/exclude
[branch "myspecialbranch"]
excludesfile = +/info/exclude_specialbranch
My .git/info/exclude
file looks like this (by default, I didn’t touch it):
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
I don’t have a .gitignore
file in my “specialbranch”.
If I try to ignore a file like info.php
, my .git/info/exclude_specialbranch
file looks like this:
info.php
… but it doesn’t ignore the file.
If I run git status --ignored
, it only lists the .DS_Store
file from the default exclude
file.
However, if I add info.php
to my .git/info/exclude
file:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
.DS_Store
info.php
it ignores the file perfectly.
Note that it still works without the line excludesfile = +/info/exclude
under [core]
in .git/config
. Git seems to know where the system-wide exclude
is without me having to tell it.
I have a feeling that .git/config
isn’t recognizing the address of my custom excludes file:
excludesfile = +/info/exclude_specialbranch
… most likely because of the use of +
to describe the location of the .git
directory.
What’s the correct method for addressing custom exclude files in the (more recent) versions of Git?
I’m running OSX 10.9.5 and Git version 2.2.1.
You’re trying to achieve something that Git does not support. The blog post is the original source of this hoax that the Stack Overflow answer only parroted. As noted in comments under that answer, even the original blog post contains discussion that brings out that the solution does not work and it links to a newer blog post that mentions that even the author is unable to reproduce the behavior.
Why it does not work? If you read man gitignore
and man git-config
, you’ll find just core.excludesfile
referenced. No branch.<name>.excludesfile
there. The core.excludesfile
is meant to enable you to exclude e.g. Vim .swp files or other temporary stuff your software uses.
core.excludesfile
In addition to
.gitignore
(per-directory) and.git/info/exclude
, Git looks into this file for patterns of files which are not meant to be tracked. "~/
" is expanded to the value of$HOME
and "~user/
" to the specified user’s home directory. Its default value is$XDG_CONFIG_HOME/git/ignore
. If$XDG_CONFIG_HOME
is either not set or empty,$HOME/.config/git/ignore
is used instead. See gitignore(5).
I believe that the best approximation of a per-branch excludes file is achieved using the post-checkout hook and realization of the .gitignore
via a symlink.
Each branch would have e.g. a .gitignores
directory with files named after the corresponding branches. Then there would be a .gitignores/__default
file that would be used by default. The .gitignore
would be excluded by all the excludes files and would be created by the post-checkout hook as a symlink to the corresponding file in .gitignores
.
If you don’t want to track the excludes files, you may do the same with .git/info/exclude
file as a symlink to .git/info/excludes/__default
etc.