Why git keeps ignoring files within certain directory despite I stated it should not

Viewed 75

I have setup git under my home assistant directory. The suggest the following configuration:

# Example .gitignore file for your config dir.
# An * ensures that everything will be ignored.
*
# You can whitelist files/folders with !, these will not be ignored.
!*.yaml
!.gitignore
!*.md

Then I added a couple of whitelist AFTER the git was initialize and several commits were made:

# Example .gitignore file for your config dir.
# An * ensures that everything will be ignored.
*
# You can whitelist files/folders with !, these will not be ignored.
!*.yaml
!.gitignore
!*.md
!packages/
!esphome
!syslog-ng/
!python_scripts/**
# Ignore folders.
.storage

So the problem is that files under certain directories are still ignored, despite I clearly stated they should not. For example, under python_scripts folder all .py files are being ignored. Funny .yaml files are not ignored. I know there are several questions like this, and several answers, but I think my case is different. I think the fact that I whitelisted certain extensions makes git thing that only files with that extension should be unignored. I can't add !*.py to the gitignore because I only want python files under the pythons_scripts folder to be not ignored.

I already tried several variations like !python_scripts/**/*.py without luck. And also my real objective is to whitelist the entire folder, all contents. I even tried to add a .gitignore file to that subfolder with the content !*, no luck, git keeps saying:

āžœ  config git:(lovelace.yaml) āœ— git add python_scripts/*
The following paths are ignored by one of your .gitignore files:
python_scripts
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

Some extra debugging information. Git version

git --version
git version 2.32.0

As you can see, if I add a test.py file git says it ignores it because the * on the main gitignore, despite the clear !* on the nested gitignore.

touch python_scripts/test.py
git check-ignore -v python_scripts/test.py
.gitignore:3:*  python_scripts/test.py
python_scripts/.gitignore
!*#

However, yaml files that are whitelisted at root are not ignored:

ls python_scripts
insultar_invitados.py  services.yaml          test.py
git check-ignore -vv python_scripts/services.yaml
# no output

OS info:

uname -a
Linux a0d7b954-ssh 5.10.70 #1 SMP Mon Oct 4 23:24:22 UTC 2021 x86_64 Linux
cat /etc/alpine-release
3.14.1
1 Answers

If your intention is to ignore all toplevel elements by default, and whitelist some of them, replace * with /* :

# A '/*' will ignore all elements in the current directory,
# but will not apply to sub directories
/*

The relevant part of git help gitignore is in the "Pattern Format" section :

  • If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.

This also applies to * :

  • with * : even if you explicitly named packages/ in your toplevel gitignore, everything inside this directory is "caught" by that * rule ; you would have to add explicit include rules in packages/.gitignore (1)
  • with /* : the "catch all" pattern is only applied to the toplevel directory

(1) extra note : when testing on my machine (git 2.33.1 running on linux), adding !* to packages/.gitignore does make the content of packages "visible" to git add.
What git version + OS are you running ? Do you have other .gitignore files interfering with that one ?

Here is a command to debug why a file is ignored :

git check-ignore -v path/to/file
Related