53

In .gitignore I have a rule that excludes a path settings.php

However, that rule has to be disregarded (the file has to be included in git), but for special reasons (the .gitignore file gets frequently reset to default from outside) I prefer not to change the .gitignore itself, but rather to override it by setting a counter-rule in the core.excludesfile file.

So in the relevant core.excludesfile file I put: !settings.php to override the above rule. But it does NOT work. I suspect that rules in .gitignore have priority over the ones in the global file with exclusions (this is how I understand https://www.kernel.org/pub/software/scm/git/docs/gitignore.html).

Is there any way to override a rule set in .gitignore by a rule in core.excludesfile or elsewhere?

1
  • 1
    gitignore isn't sophisticated enough to handle the use case that you're trying to solve by resetting the gitignore file and excluding certain ignore files, etc. For your use case with settings.php, a more typical approach would be to commit a templatized version, settings.php.example and use a tool like make or another templating language to generate the settings.php at build time. The values for settings in this example would be stored in some other configuration file or in environmental variables, which isn't committed to git for security reasons. 12factor.net/config Nov 24, 2018 at 16:44

3 Answers 3

54

Yes! It turns out that when adding a file to a repository you can force it to override .gitignore:

git add -f <filename>

or

git add --force <filename>

Source: https://www.kernel.org/pub/software/scm/git/docs/git-add.html

4
  • 2
    Yes, this is a useful answer, but it seems a bit impractical to have to override .gitignore manually during git add. I am still hoping there is a way to override it more generically. Or is that impossible?
    – Vacilando
    Jul 20, 2014 at 8:27
  • 3
    Once you add a file to the repository, you don't need to add -f it anymore every time you modify it.
    – pqnet
    Nov 28, 2018 at 0:17
  • 3
    basically files are ignored if 1) untracked and 2) not listed in .gitignore or other similar configurations. Adding the file to the repository will effectively prevent it from being ignored again (until you git rm it)
    – pqnet
    Nov 28, 2018 at 0:19
  • it should be added that this doesn't appear to override rule in .git/info/exclude which imho is nice and expected, nevertheless doesn't seem documented
    – CervEd
    Apr 29, 2021 at 7:26
41

There is an easy way how to override rules in a .gitignore file -- simply make a new .gitignore file on a deeper level. E.g. in my case I was able to make git ignore my settings.php file by adding "!settings.php" into a new .gitignore file in the same folder where settings.php lives.

Reference: https://git-scm.com/docs/gitignore

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".

21

For those who don't mind modifying the .gitignore file, you can override a rule by adding ! in front of a filename or folder. Use * to select the files in a folder and ** to select the files in subfolders recursively.

Override an ignore for a specific file:

!file.js

Override an ignore for the files in a directory:

!assets/*

Override an ignore for the files in a directory and its subdirectories:

!assets/**
1
  • 1
    I will note that negative rules are exactly the kind of rules that may cause headaches for other developers. For example, one of my collaborators recently decided to ignore files without an extension (usually binary executables) by first ignoring * then unignoring *.*, and suddenly my core.excludesfile that I use to ignore things like .*.swp (I am the only developer in my group using vim) no longer works. Jul 27, 2022 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.