Jalo Jalo - 9 months ago 58
Git Question

.gitignore is not working

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

# Compiled python modules.

# Backup gedit files.

# Setuptools distribution folder.

# Python egg metadata, regenerated from source files by setuptools.

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?

Answer Source

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.