Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitignore doesn't ignore files in subdirectories

Tags:

git

gitignore

I have a .gitignore file in my repository top level folder with the following code:

# Compiled python modules.
*.pyc

# Backup gedit files.
/*~

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
/*.egg

When I try to run a git add . command the .gitignore does not make any effect, as several /*~ are added. The output for git status is the following:

new file:   uvispace/tests/test_messenger.py
new file:   uvispace/tests/test_messenger.py~
new file:   uvispace/tests/test_robot.py~
new file:   uvispace/uvirobot/__main__.py~
new file:   uvispace/uvirobot/messenger.py~
new file:   uvispace/uvirobot/move_base.py

Similar Questions

I have seen several very similar questions, like this one, or this one. They solution is to remove previously added files and then add again the whole repository with the following instructions:

git rm -rf --cached .
git add *

However, I tried it and there is no difference. Anyone has an idea why is this happening?

like image 470
Jalo Avatar asked Feb 06 '23 21:02

Jalo


2 Answers

The documentation about the gitignore-syntax can be found here: git-scm.com/docs/gitignore

One thing that probably is wrong is your /*~ because the single * doesn't work the way you expect:

For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

You have to use ** instead:

•A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

•A trailing "/**" matches everything inside. For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.

•A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

like image 99
Niklas P Avatar answered Feb 08 '23 09:02

Niklas P


You have to remove the leading slash so that the pattern *~ will match in all directories within the repo.

like image 41
Jack Bracken Avatar answered Feb 08 '23 09:02

Jack Bracken