Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Git Ignore with absolute path

Tags:

git

gitignore

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?

like image 985
Arron Avatar asked Sep 18 '18 14:09

Arron


People also ask

Is there a global Git ignore?

You can set up a global . gitignore file that lists files that will be ignored on all Git projects. This is the preferred way of ignoring editor artifacts, toolchain log files, OS files, and other miscellaneous crap.

How do I ignore an entire directory in Git?

. gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.

How do I ignore a specific file in Git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.


1 Answers

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.

like image 179
torek Avatar answered Sep 18 '22 15:09

torek