Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override a rule set in .gitignore

Tags:

git

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?

like image 723
Vacilando Avatar asked Jul 19 '14 20:07

Vacilando


People also ask

How do I edit a .gitignore file?

You can edit your . gitignore file for your repo by going to the Settings view in Team Explorer, then selecting Repository Settings. Select Edit for your . gitignore .

How do I ignore a .gitignore class file?

The easiest and most common way to ignore files is to use a gitignore file. Simply create a file named . gitignore in the repository's root directory. Then, add names and patterns for any files and directories that should not be added to the repository.

How do I ignore a file without adding to Gitignore?

To ignore untracked files, you have a file in your git folder called . git/info/exclude . This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.


2 Answers

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

like image 166
Sam-Graham Avatar answered Oct 31 '22 09:10

Sam-Graham


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".

like image 35
Vacilando Avatar answered Oct 31 '22 09:10

Vacilando