How do I ignore files in a directory in Git?
What is the proper syntax for the
.gitignore file to ignore files in a directory?
Would it be
- Keep file on remote, but cloning repo via Git will ignore that specific file
- Can I 'git commit' a file and ignore its content changes?
- What is wrong with my gitignore?
- Ignored files are still visible in Commit Changes window - IntelliJ 13
- Why is git ignoring my changed file?
- Should the .babelrc file be gitignored?
config/databases.yml cache/* log/* data/sql/* lib/filter/base/* lib/form/base/* lib/model/map/* lib/model/om/*
/config/databases.yml /cache/* /log/* /data/sql/* /lib/filter/base/* /lib/form/base/* /lib/model/map/* /lib/model/om/*
10 Solutions collect form web for “How do I ignore files in a directory in Git?”
A blank line matches no files, so it can serve as a separator for readability.
A line starting with
#serves as a comment.
An optional prefix
!which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.
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
fooand 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 relative to the location of the
.gitignorefile (relative to the toplevel of the work tree if not from a
Otherwise, git treats the pattern as a shell glob suitable for consumption by
FNM_PATHNAMEflag: wildcards in the pattern will not match a
/in the pathname. For example,
A leading slash matches the beginning of the pathname. For example,
You can find more here
git help gitignore
It would be the former. Go by extensions as well instead of folder structure.
I.e. my example C# development ignore file:
#OS junk files [Tt]humbs.db *.DS_Store #Visual Studio files *.[Oo]bj *.user *.aps *.pch *.vspscc *.vssscc *_i.c *_p.c *.ncb *.suo *.tlb *.tlh *.bak *.[Cc]ache *.ilk *.log *.lib *.sbr *.sdf ipch/ obj/ [Bb]in [Dd]ebug*/ [Rr]elease*/ Ankh.NoLoad #Tooling _ReSharper*/ *.resharper [Tt]est[Rr]esult* #Project files [Bb]uild/ #Subversion files .svn # Office Temp Files ~$*
I thought I’d provide an update from the comments below. Although not directly answering the OP’s question, see the following for more examples of
Community wiki (constantly being updated):
.gitignore for Visual Studio Projects and Solutions
More examples with specific language use can be found here (thanks to Chris McKnight’s comment):
Paths which contain slashes are taken to be relative to the directory containing the .gitignore file – usually the top level of your repository, though you can also place them in subdirectories.
So, since in all of the examples you give, the paths contain slashes, the two versions are identical. The only time you need to put a leading slash is when there isn’t one in the path already. For example, to ignore foo only at the top level of the repository, use
/foo. Simply writing
foo would ignore anything called foo anywhere in the repository.
Your wildcards are also redundant. If you want to ignore an entire directory, simply name it:
The only reason to use wildcards the way you have is if you intend to subsequently un-ignore something in the directory:
lib/model/om/* # ignore everything in the directory !lib/model/om/foo # except foo
A leading slash indicates that the ignore entry is only to be valid with respect to the directory in which the .gitignore file resides. Specifying
*.o would ignore all .o files in this directory and all subdirs, while
/*.o would just ignore them in that dir, while again,
/foo/*.o would only ignore them in /foo/*.o.
If you want to put a .gitignore file at the top level and make it work for any folder below it use
E.g. to ignore all
*.map files in a
/src/main/ folder and sub-folders use:
Both examples in the question are actually very bad examples that can lead to data loss!
My advise: never append
/* to directories in .gitignore files, unless you have a good reason!
A good reason would be for example what Jefromi wrote: “if you intend to subsequently un-ignore something in the directory”.
The reason why it otherwise shouldn’t be done is that appending
/* to directories does on the one hand work in the manner that it properly ignores all contents of the directory, but on the other hand it has a dangerous side effect:
If you execute
git stash -u (to temporarily stash tracked and untracked files) or
git clean -df (to delete untracked but keep ignored files) in your repository, all directories that are ignored with an appended
/* will be irreversibly deleted!
I had to learn this the hard way. Somebody in my team was appending
/* to some directories in our .gitignore. Over the time I had occasions where certain directories would suddenly disappear. Directories with gigabytes of local data needed by our application. Nobody could explain it and I always hat to re-download all data. After a while I got a notion that it might have to do with
git stash. One day I wanted to clean my local repo (while keeping ignored files) and I was using
git clean -df and again my data was gone. This time I had enough and investigated the issue. I finally figured that the reason is the appended
I assume it can be explained somehow by the fact that
directory/* does ignore all contents of the directory but not the directory itself. Thus it’s neither considered tracked nor ignored when things get deleted. Even though
git status and
git status --ignored give a slightly different picture on it.
How to reproduce
Here is how to reproduce the behaviour. I’m currently using Git 2.8.4.
A directory called
localdata/ with a dummy file in it (
important.dat) will be created in a local git repository and the contents will be ignored by putting
/localdata/* into the
.gitignore file. When one of the two mentioned git commands is executed now, the directory will be (unexpectedly) lost.
mkdir test cd test git init echo "/localdata/*" >.gitignore git add .gitignore git commit -m "Add .gitignore." mkdir localdata echo "Important data" >localdata/important.dat touch untracked-file
If you do a
git status --ignored here, you’ll get:
On branch master Untracked files: (use "git add <file>..." to include in what will be committed) untracked-file Ignored files: (use "git add -f <file>..." to include in what will be committed) localdata/
Now either do
git stash -u git stash pop
git clean -df
In both cases the allegedly ignored directory
localdata will be gone!
Not sure if this can be considered a bug, but I guess it’s at least a feature that nobody needs.
I’ll report that to the git development list and see what they think about it.
The first one. Those file paths are relative from where your .gitignore file is.
It would be:
config/databases.yml cache log data/sql lib/filter/base lib/form/base lib/model/map lib/model/om
or possibly even:
config/databases.yml cache log data/sql lib/*/base lib/model/map lib/model/om
in case that
form are the only directories in lib that do have a
basesubdirectory that needs to be ignored (see it as an example of what you can do with the asterics).
I’m maintaining a GUI and CLI based service that allows you to generate
.gitignore templates very easily at https://www.gitignore.io.
You can either type the templates you want in the search field or install the command line alias and run
$ gi swift,osx
A sample .gitignore file can look like one below for a Android Studio project
# built application files *.apk *.ap_ # files for the dex VM *.dex # Java class files *.class # generated files bin/ gen/ # Local configuration file (sdk path, etc) local.properties #Eclipse *.pydevproject .project .metadata bin/** tmp/** tmp/**/* *.tmp *.bak *.swp *~.nib local.properties .classpath .settings/ .loadpath YourProjetcName/.gradle/ YourProjetcName/app/build/ */YourProjetcName/.gradle/ */YourProjetcName/app/build/ # External tool builders .externalToolBuilders/ # Locally stored "Eclipse launch configurations" *.launch # CDT-specific .cproject # PDT-specific .buildpath # Proguard folder generated by Eclipse proguard/ # Intellij project files *.iml *.ipr *.iws .idea/ /build build/ */build/ */*/build/ */*/*/build/ *.bin *.lock YourProjetcName/app/build/ .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store .gradle/ app/build/ *app/build/ # Local configuration file (sdk path, etc) local.properties /YourProjetcName/build/intermediates/lint-cache/api-versions-6-23.1.bin appcompat_v7_23_1_1.xml projectFilesBackup build.gradle YourProjetcName.iml YourProjetcName.iml gradlew gradlew.bat local.properties settings.gradle .gradle .idea android build gradle