Git Global Ignores, Can Be Overridden

First off, I'm loving Git (fast/awesome/free/open-source distributed version control system). If you haven't already, check it out!

I'd like to briefly mention how to ignore certain files from version control, both by project and globally. I'd also like to confirm that it's very easy to add files to version control, which are normally globally ignored.

Git makes it simple to ignore certain files/directories/patterns from version control by creating a .gitignore file in your project root, and optionally in any sub-directory. Check out the gitignore docs page for more.

That said, I quickly found myself ignoring the same patterns for every repository. Fortunately, Git also makes it easy to globally ignore patterns. You just set a global config property of core.excludesfile to tell Git where to look for a global gitignore file. The following two commands take care of setting the property and creating a file with a few popular ignore patterns.

git config --global core.excludesfile ~/.gitignore
printf ".project\n*.iml\n.idea/\nWEB-INF/\n" >
> ~/.gitignore

The above commands will work on both Linux and Mac. Windows users will likely want to locate a global .gitignore file in some common user settings directory. You can then create/edit the text file any way you'd like.

For me, on Ubuntu Linux, I end up with a /home/jkrug/.gitignore file that looks like this:

.project
*.iml
.idea/
WEB-INF/

You can also manually edit your .gitignore file, of course, and add comments if you'd like:

# Eclipse project files
.project

# IntelliJ IDEA project files
*.iml
.idea/

# Java WAR directories
WEB-INF/

Now here's the bonus: if you have one repository in which you'd like to add a file that is globally ignored to version control, you can do so. Simply use a standard add command with a -f flag, e.g.,

git add -f WEB-INF/web.xml

The global ignores are especially useful to me when I fork a project that may not have an ignore for my primary IDE's (IntelliJ IDEA) configuration files. And when I have a custom project in which I'd like to add one of those global ignores--no problem!

Happy versioned coding :)

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Mike Henke's Gravatar Interesting. I did not know about the the f option for git add. I wonder if that added or changed your .gitignore the specific folder.

In your example, I add a .gitignore in your project and then in it put.

# except
!WEB-INF/web.xml
# Posted By Mike Henke | 5/27/10 12:02 PM
Jamie Krug's Gravatar Hi Mike,

Ya, I thought about posting information on using negation of rules in project-specific .gitignore files, but I got strange results in a couple quick tests I performed. The "git add -f" trick seems very simple and straightforward.

It really should be an edge case anyway. I'll only add global ignore patterns that really are ignored almost *always*. The only danger is if you have a global ignore pattern that ignores a large number of files in the project, and you may not think to add them (but then again, you probably don't want to, which is why you've globally ignored it :).
# Posted By Jamie Krug | 5/27/10 2:03 PM
BlogCFC was created by Raymond Camden.