4

Lets say I have ".foo" in my global .gitignore and that works great for all of my current projects (and lets say that there is a lot of them, so I want to keep this in my global ignore file).

Now, I'm starting a new project that uses .foo files everywhere.

How can I "undo" the exclusion of .foo files defined in my global ignore for just this specific project?

This means I can just git add . when working with the new project and have it pick up all the .foo files, but if I run git add . in any other project then the .foo files are ignored.

Matt Klein
  • 7,856
  • 6
  • 45
  • 46

3 Answers3

6

You can simply unignore a pattern by add a leading !, like this:

# Don't ignore .foo file
!.foo

If you put this in your local .gitignore, it will override settings done in the global .gitignore.

You can find the documentation for supported patterns in gitignore in the official documentation here

dubes
  • 5,324
  • 3
  • 34
  • 47
Jona
  • 1,218
  • 1
  • 10
  • 20
  • might I suggest adding the link to the git reference documentation to your answer? https://git-scm.com/docs/gitignore#_pattern_format . It may be helpful for someone with related questions – dubes Mar 27 '19 at 16:14
  • 1
    TIL you can make your own answer as community wiki! Thanks for showing me this possibility :-) – dubes Mar 27 '19 at 16:23
2

Every repository also has a local .gitignore which you can modify.

The accepted answer here tells you where to look:

How do I configure git to ignore some files locally?

To unignore simply add it to the local .gitignore file with a leading !

NewEyes
  • 407
  • 4
  • 15
  • I think OP is looking for "unignore" functionality. The comment by @Jona is pointing to the right path, just missing the context – dubes Mar 27 '19 at 16:07
  • You are right, I have added a sentence on how to unignore. – NewEyes Mar 27 '19 at 16:10
2

What you're looking for is negation. See https://git-scm.com/docs/gitignore#_pattern_format

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

For your case, simply add this to the .gitignore for the project:

!.foo

Check out this question for a more exhaustive explanation: Make .gitignore ignore everything except a few files


Note this important caveat in the docs:

It is not possible to re-include a file if a parent directory of that file is excluded.

So if you were trying to include only specific files in an excluded directory, you would have to change things around to include the directory, but exclude any files inside it, except the ones you want:

!excludedir
excludedir/*
!excludedir/*.foo
Sean the Bean
  • 5,222
  • 5
  • 38
  • 40