5

I have one project where I want to ignore a root file called .eslintrc.js. The team wants only to lint in the build tools not in their IDEs. I want to add this to the root on my machine and ignore it without using the .gitignore for that project.

I also do not want to add /.eslintrc.js to my ~/.gitignore_global file because i have other projects when i do not want to ignore the linting rules.

I have tried adding

~/code/repo-name/.eslintrc.js

in my ~/.gitignore_global file but then git does not recognize the rule.

How can I make a global git ignore rule that works in some projects and not others?

Can I set git to use full paths in ignoring something?

Arron
  • 767
  • 8
  • 11
  • I have just discovered a solution that is *ES Lint* specific so I do not need answers in that capacity but a git solution would be great because this same issue could easily be *webpack* or *browserslist* or _anything else_ specific. – Arron Sep 18 '18 at 15:06

2 Answers2

8

First, it's worth mentioning a tricky bit about ignore file patterns: if they contain a leading or an embedded slash, they apply across multiple name components, but if they do not, they apply to one name component (in whatever directory Git is traversing, recursively, at the time). This rule gets applied after removing a trailing slash if there is one, so that a .gitignore file that contains:

abc
def/ghi

ignores all files (and directories) named abc that occur from this point "downward", while ignoring only def/ghi that occurs at this point. The second line is exactly equivalent to /def/ghi.

Can I set git to use full paths in ignoring something?

No. Gitignore patterns are either relative to the directory in which the ignore file occurs (for most .gitignore files) or the project root (for .git/info/exclude and your personal ignore file). That is, suppose the project lives in /home/arron/project/proj1 and:

$ cd ~/project/proj1/
$ cat .gitignore
/abc
sub/file
$ cat sub/.gitignore
/dir/

All of these entries contain or begin with /, so the top level .gitignore suppresses complaints about the untracked file abc in the top level of the project plus complaints about the untracked file file in the directory sub. Meanwhile the directory sub has its own .gitignore that not only suppresses complaints about, but stops Git from even enumerating the contents of, the directory dir within sub, i.e., Git does not look inside project/proj1/sub/dir/ at all (hence neither discovers, nor complains about, any untracked files within that directory).

Note that if we modify both .gitignore files so that we have instead:

$ cat .gitignore
/abc
$ cat sub/.gitignore
/file
/dir/

the set of ignored files remains exactly the same.

torek
  • 448,244
  • 59
  • 642
  • 775
3

For separate local files that you can't put on projects .gitignore you can add them in .git/info/excludes just the same way as you do in regular .gitignore:

# Ignore local ESLint config
.eslintrc.js

This also works for folders, but there is alternative approach for them: add another .gitignore file inside that folder with just one line, containing *

oxfn
  • 6,590
  • 2
  • 26
  • 34