DEV Community

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

Mika Feiler on January 22, 2020

I just asked on Super User: Are .gitignore files from directories above the repository's or from home directory (~/.gitignore) loaded in any versi...
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