Victor Bjelkholm Victor Bjelkholm - 2 months ago 11
Git Question

Make git ignore files

I'm trying to make git ignore some of my files and I found one description about how you could do this


Add a file in the root directory
called .gitignore and add some files
to it: (comments begin with hash)
*.log db/schema.rb db/schema.sql

Git automatically ignores empty
directories. If you want to have a
log/ directory, but want to ignore all
the files in it, add the following
lines to the root .gitignore: (lines
beginning with ‘!’ are exceptions)

log/* !.gitignore

Then add an empty .gitignore in the
empty directory:

touch log/.gitignore

So I made a file called .gitignore in my folder for my project and wrote the following in it:


But when I commit, the files isent excluded from the commit...


According to man gitignore:


A gitignore file specifies intentionally untracked files that git should ignore. Note that all the gitignore files really concern only files that are not already tracked by git; in order to ignore uncommitted changes in already tracked files, please refer to the git update-index --assume-unchanged documentation.

So it doesn't help if you've already added them. It's mostly for preventing the addition in the first place. That way, you can ignore .tmp files and add a whole directory without worrying that you'll add the .tmp files.

I believe you can remove them from the index with:

git rm --cached file_to_stop_tracking_but_dont_want_to_delete.txt


Also, the .gitignore needs to be at the base directory or at least above where those directories are. Also, take the "*" out of the directories:


And be careful of phpMyAdmin/ vs /phpMyAdmin vs phpMyAdmin. Also from man gitignore:

  • If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git).

  • If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname without leading directories.

  • Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html. A leading slash matches the beginning of the pathname; for example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.