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.
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:
*.iml
.idea/
WEB-INF/
You can also manually edit your .gitignore file, of course, and add comments if you'd like:
.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.,
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 :)
In your example, I add a .gitignore in your project and then in it put.
# except
!WEB-INF/web.xml
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 :).