git how to: ignoring files in git

Just as we moved from cvs to subversion to git, our ignore files changed from .cvsignore to .svnignore to .gitignore. However, git offers more flexibility in managing our ignore files depending on situation you are in. Here are a few options, I've found:

Directory-specific ignores (.gitignore):

This is the most common scenario for ignoring files in specific directory. This file is also almost always checked into version-control to share with other developers.

Repository-wide ignores (.git/info/exclude):

This resides inside .git/ directory and is not shared with other developers. Such a file would be useful if I want to keep a few files local to my development environment. For example, this allows me to keep my nbproject/ folder (netbeans folder) out of repository which I don't want to share with other developers.

Global ignores (~/.gitignore):

This doesn't work by default. To enable this, you would need to set following global git config:

git config --global core.excludesfile ~/.gitignore

Once the above global config is set, any ignore patterns added to this file will be ignored for all your git projects. I've found that most stuff that I used to add to repository-specific ignore files could go here, once and for all :-)

I am left with one question though: How do I ignore a file that already exist in codebase?

Why do I need that?, you ask. I need it for files like config/database.yml which are already in database but I need a slightly modified version of it for myself. I know, I know, we can agree as a team to have config/database.yml.template only in database. But, what if I didn't have that option?

Topics: ,

Comments: 5 so far

  1. I feel you pain. I’ve forked a number of libraries where compiled binaries have snuck into the repository. (Bundled Merb apps prior to 1.06 like merb-book are the worst offenders.) The developer who owns these file has no reason to remove them they don’t bother him, but I clobber them every time I redeploy. I then have to meticulously keep them out of my commits because binary changes almost always create merge conflicts. If you find a solution let me know!

    Comment by Antares Trader, Friday, January 9, 2009 @ 8:06 pm

  2. Thanks for this round-up; I only knew about the first two options.

    Has anyone found .gitignore to be intermittent? I often find I need to remove a file from the repo, delete the entry in .gitignore, return the file to the repo, then add the entry back in to .gitignore - otherwise it just won’t include the file in the list of ignored files. Is this just me or has anyone else seen this?

    Comment by Neil, Saturday, January 10, 2009 @ 2:36 am

  3. You solve it by not having production configs in the repository and having Capistrano or whatever deployment system/script you use symlink database.yml to an immutable copy on the server.

    That is the convention at least.

    Thanks for your article. Git rocks and so does github.

    Comment by Erik Petersen, Saturday, January 10, 2009 @ 9:55 am

  4. You’ll need to do git rm –cached to keep the file in your tree and then ignore it.
    From Here : http://www.gitready.com/beginner/2009/01/19/ignoring-files.html

    Comment by greb, Sunday, April 26, 2009 @ 12:35 am

  5. http://GitReady.com/ is a great resource: Thanks for stopping by and pointing out.

    Comment by Sharad Jain, Tuesday, April 28, 2009 @ 10:53 am

Leave a comment

Powered by WP Hashcash

About Pathfinder

Follow the Blog

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email

      

    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com