DEV Community

Mika Feiler
Mika Feiler

Posted on

I just asked: Are .gitignore files from directories above the repository's or from home directory loaded in any versions of Git?

I just asked on Super User:

Are .gitignore files from directories above the repository's or from home directory (~/.gitignore) loaded in any versions of Git?

~/.gitignore is mentioned by some advisory web postings despite not working in the version of Git on my system, and my system manual is unclear about how far up Git will walk looking for a .gitignore, although only mentions $XDG_CONFIG_HOME/git/ignore as a home directory global gitignore file location.

My problem stems from, to manage my home directory dotfiles i have presently chosen to use Git with a custom GIT_DIR environment variable setting, which does fine with not having Git default to that repository in any subdirectories of my ~.

But, as .gitignore is hardcoded and can't be selected by an environment variable (which i would like to be like "${GIT_DIR%/}ignore"), being afraid that on some of my systems ~/.gitignore could end up applying either globally or in repositories in the subdirectories of ~, i resorted to adding the below snippet to my ${GIT_DIR}/info/exclude and then staging the ${GIT_DIR}/info/exclude file into the repository, keeping all my gitignore entries that are to apply to my dotfiles management repository in there (and in .gitignore files in dotfiles-only subdirectories). What i don't like about it is that it's pretty dirty and it might cause minor problems sometimes.

The snippet mentioned above, with .gd being my $GIT_DIR:

/.gd/**
!/.gd/info/
!/.gd/info/exclude
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
_marekj profile image
Marek Jay • Edited

git looks for a .gitignore file in your current local clone (the dir with .git dir) but you can DRY your gitignore for all your projects by maintaining a global gitignore in a ~/.gitignore for example and you tell git where it is with a core.excludesfile section declaration.

example:

git config --global core.excludesfile

.. should tell you if you have it(if any) If not, you can declare the file where your global user gitignore file is with:

git config --global core.excludesfile "$HOME"/.gitignore

Examine your global git config with git config --global --list or brute force just open your $HOME/.gitconfig file

Once you set up a global .gitignore excludesfile you will not need to keep pasting the same .gitingnore in each repo you work in, thus you have sensible defaults and DRY in play. All the best

Collapse
 
mkf profile image
Mika Feiler

so ~/.gitignore as a custom filename to be mentioned in ~/.gitconfig under core.excludesfile is an alternative to ~/.config/git/ignore, but can we say that we are sure no outdated version of Git ever had ~/.gitignore hardcoded, and no outdated version of Git walked further up than $GIT_WORK_TREE looking for .gitignore?

Collapse
 
_marekj profile image
Marek Jay • Edited

git does not walk UP anywhere, if I can say that. It only acts on the current directory where .git lives; but it also uses the global file (user space, the global in this context means You, the user, not everyone on the machine) located at ~/.gitconfig, which governs how git behaves for You, the user (global may be confusing to people) in all the repos you work on that machine (repo, meaning a working dir with .git dir)

The declaration core.exludesfileasks you to point to a file that holds the global (to you) ignore patterns you want to apply to all REPOS you have in your workspace. (git-scm.com/docs/gitignore)

Where the ignore file is located is irrelevant, but by some sensible default I would suggest ~/.gitingnore, yet it can be ~/gitlistaignorancja or ~/whatever-blabla-git ... - and no, git never hardcodes the ~/.gitignore name (only local .git/gitignore) . It does respect the GIT_DIR/.gitignore because it treats it as a WORKSPACE local ignore patterns (in addition to what's in global excludesfile)

Git does not walk up to any dirs up to find stuff (unlike NPM if you ever worked with it). It only looks at your local WORKING DIR (local gitconfig), the global (user space) declarations in ~/.gitignore and the system (indeed global to all users) file. Hope this makes sense.

Collapse
 
just1602 profile image
Justin Lavoie • Edited

Hello,

Normally, you have to declare your global ignore file in your ~/.gitconfig, like here. Than, the ignored files in your global ignore file will be ignore everywhere by git.

Generally, the global ignore file is use to ignore stuff like your IDE files / project fodlers, if you want to ignore some files in your home config, maybe you should create a .gitignore file in your home coonfig repository.

I hope it'll help you ! :)

Collapse
 
kiliman profile image
Kiliman

Based on a quick test on macOS, it appears that git only goes up the parent directory if there is a .git folder (a git repo).

I created a ~/.gitignore file with node_modules.

Then I created a project ~/Projects/scratch/testignore and added a folder node_modules. I tried git status and got the error: fatal: not a git repository (or any of the parent directories): .git

When I ran git init then git status it showed node_modules as untracked. Once I added a .gitignore file, node_modules was ignored.

So basically, you shouldn't have any issues with your ~/.gitignore file interfering with subdirectories as long as there is an intervening folder without a .git repo, like ~/Projects.

Collapse
 
mkf profile image
Mika Feiler • Edited

the problem is that it is only a test on the newest version. and i work on systems that are not only old debian but also not maintained by me — university systems. so i would have to test on each of these. i need an answer from someone familiar with git changelog in this aspect. there must have been some versions that did ~/.gitignore, there is so much advisory posting mentioning it

thanks for effort