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

  • Cloning a repo without tags?
  • Who pushed commits into remote repository?
  • How to commit on Github and automatically mark done on Basecamp?
  • How to undo “git commit --amend” done instead of “git commit”
  • Push to Remote URL with TortoiseGit
  • Get git diff for any merged branch
  • config/databases.yml




  • What does GitHub for Windows/Mac “publish” do?
  • format-patch for a single file
  • How can I specify which branch should be checked out in a fresh clone?
  • git fetch insufficient permission for adding an object to repository database .git/objects?
  • Expand variables on config.xml PhoneGap
  • git push fails unless I do a gc on the remote repo first
  • 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 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 relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).

    • 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 or tools/perf/Documentation/perf.html.

    • A leading slash matches the beginning of the pathname. For example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.

    You can find more here

    git help gitignore
    man 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
    #Visual Studio files
    #Project files
    #Subversion files
    # 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 .gitignore syntax.

    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!

    Some background

    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)
    Ignored files:
      (use "git add -f <file>..." to include in what will be committed)

    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:


    or possibly even:


    in case that filter and 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

    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
    # files for the dex VM
    # Java class files
    # generated files
    # Local configuration file (sdk path, etc)
    # External tool builders
    # Locally stored "Eclipse launch configurations"
    # CDT-specific
    # PDT-specific
    # Proguard folder generated by Eclipse
    # Intellij project files
    # Local configuration file (sdk path, etc)
    Git Baby is a git and github fan, let's start git clone.