0

In my project's root directory there are directories like 'tools':

tools/evaluate/test/
tools/evaluate2

Under test, there are some .py and .csv files. I want to ignore all files except .py, so in my .gitignore, I have this entry:

!tools/**/*.py

I want to recursively ignore all non-python files under tools. What's wrong with that?

Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
marlon
  • 6,029
  • 8
  • 42
  • 76
  • Have you already committed some of those non-py files previously? – BeRT2me Sep 27 '22 at 20:14
  • 1
    I think you may have just forgotten a slash after the bang. Possible duplicate of [this thread](https://stackoverflow.com/questions/6794717/git-ignore-certain-files-in-sub-directories-but-not-all) – JohnAlexINL Sep 27 '22 at 20:17
  • @BeRT2me Yes. Some py files are already in the repository. – marlon Sep 27 '22 at 20:25
  • !/tools/**/*.py: This doesn't work either. @JohnAlexINL – marlon Sep 27 '22 at 20:29
  • @marlon I asked about the NON-PY files, not the PY files. – BeRT2me Sep 27 '22 at 20:48
  • `!tools/**/*.py` only means "but keep the .py files". You have to have a rule to exclude that place before it. https://github.com/kenmueller/gitignore#everything-inside-of-a-folder-except-for-some-files – zapl Sep 27 '22 at 21:14
  • @BeRT2me I might have committed some csv files before, but today I added two new csv files and want to ignore them. Also, I checked the directory in remote git repository, there is only one .sh file and all others are .py files. – marlon Sep 27 '22 at 21:43

2 Answers2

1

if the files you are trying to ignore have been already committed, you need to remove them from the staging area as well, that's done by:

git rm --cached !tools/**/*.py

check the status:

git status

add the files you want to delete to .gitignore i assume manually, i don't know of an automatic way, then finally:

git add .gitignore
git commit -m "Remove unused files"
Hannon qaoud
  • 785
  • 2
  • 21
0

Two parts are needed here:

# Recursively ignore everything in tools that has an extension:
**/tools/**/*.*

# Except .py files recursively in tools:
!tools/**/*.py
BeRT2me
  • 12,699
  • 2
  • 13
  • 31