git ignore files

10

I started playing with git a bit for a personal repository. Installing it on the Mac was trivial thanks to the git OS X installer. From download page to a working repository was probably less than 5 minutes.

Since I’m most familiar with Subversion, I’m finding the Git – SVN Crash Course very helpful. The Git tutorial is also very helpful.

One of the first issues I ran into was wanting to ignore the common .DS_Store file which shows up in Mac directories. The basics on where ignores are picked up is covered fairly well in the man pages and boils down to looking at:

  • command-line args (where appropriate)
  • .gitignore file in the git repository directories
  • $GIT_DIR/info/exclude
  • a file specified by the core.excludes property

Which one you want to use depends on how widely you want this setting to be shared. If other users will necessarily need this as part of their repository, then it should be in .gitignore so they get it with their repo. Ignores that are specific to one user’s workflow should go in the exclude file and files that should always be ignored should be stored in the last option. As far as I can tell, I want the last one.

One question I had was what $GIT_DIR was. I did find a file like this at /usr/local/git/share/git-core/templates/info/exclude but setting patterns there seemed to have no effect.

Instead I ended up putting this in a file specified by the core.excludes property. The properties are stored at ~/.gitconfig and you can either modify it directly or use some of the git commands. I needed to set the “core.excludesfile” property. It took me a while to understand that “core” is the section and “excludesfile” is the property so the relevant portion of the config file looks like this:

[core]
        excludesfile = "/path/to/.gitexcludes"

and the .gitexcludes file just contains the pattern “.DS_Store”.

Hope that helps someone in the future…

Comments

10 Responses to “git ignore files”
  1. Jakub Narębski says:

    $GIT_DIR means where repository is. Usually this is .git directory in top directory of your repository’s working area.

  2. fractious says:

    Perfect, thanks for that, just what I was looking for.

    One point worth sharing: I wanted the excludes file in the root of my home dir. Setting excludesfile = "~/.gitexcludes" didn’t work for me but using the full path from / did.

    Mac OS X 10.5.4

  3. Thanks for all the useful tips and links! I am also making the switch from SVN to git, and definitely plan on giving the article you linked to a try. Now that a great open source tool like github is available, I feel as though I need to get with the times and give git a try.

    Thanks again for the great article!

  4. wdm says:

    Thanks, that saved some head scratching.

    To clarify, it’s .DS_Store without quotes.

    Mercurial sets up its’ .hgignore file for you by default. Just saying ;)

  5. Phil Harnish says:

    A full path is apparently necessary fractious said. Too bad. @wdm In my version, git was already ignoring .DS_Store from $GIT_DIR/info/exclude for whatever reason.

  6. Hello,

    Thanks for the tip.
    So a full path is indeed necessary.
    In the end, I had to do

    git config –global core.excludesfile “/Users/nrolland/.gitignore”

    and the content of the .gitignore file is
    macflashy:~ nrolland$ cat ~/.gitignore
    *.o
    build/**/*
    .DS_Store
    *.app
    *.swp
    *~.nib
    *.pbxuser
    *.perspective
    *.perspectivev3
    *.mode1v3
    *.mode2v3

  7. corysama says:

    Putting quotes around the path for the excludesfile did not work for me. Without the quotes, it does work however:

    git config –global core.excludesfile /Users/corysama/.gitignore

  8. finnw says:

    Beware of UTF-8 byte-order marks at the start of your excludes file. I just got bitten by this when my editor inserted a BOM. It took me about an hour to figure out why git seemed to be paying no attention to the excludes file.

  9. uxp says:

    To everyone that is having an issue with the:
    git config –global core.excludes …

    command, it is because you are surrounding the path in quotes, which treats it as a literal string, instead of a path.

    git config –global core.excludes ~/.gitignore

    is the proper command. it can be substituted as:

    git config –clobal core.excludes “/home/username/.gitignore”

    but that is being treated as a literal, as explained. String literals and paths are mutually exclusive when they reference the same thing.

  10. IdleWorx says:

    Your post was really useful.
    I was trying to figure out some things about ignoring CVS files when committing GIT projects on windows and found a few good pieces of information here, particularly about the excludesfile property (which is a little hard to understand from the docs).

    I recently wrote a similar post regarding ignore files on Windows, with some inspiration from yours. Let me know what you think.

    http://blog.idleworx.com/2010/06/setting-up-gitignore-for-java-web.html