June 16, 2010

Setting up .gitignore for java web appplications on windows

Just like CVS has .cvsignore and SVN has .svnignore files, git has a way to ignore certain resources by using a .gitignore file. This post will show you how to use git's ignore features to avoid committing certain files.

If you are not somewhat familiar with code version control systems like CVS or SVN, or with GIT this post is probably not for you. In particular if you are not familiar with GIT and how to set it up on windows read some of these tutorials:

Ignore that Git

There are several ways you can make git ignore certain types of files:

File to edit What to put here
Method 1
projectfolder/.gitignore Files that all developers will want to ignore go here (eg. bin or build directories)
Method 2
$GIT_DIR/info/exclude Auxiliary files that live inside the repository but are specific to one user's workflow.

(eg. if you use eclipse these files should go here: .classpath, .project )

$GIT_DIR is usually your project's '.git' folder
Method 3

The git documentation refers to this as a file specified by core.excludesfile in the user's ~/.gitconfig.

Patterns which a user wants git to ignore in all situations

(eg. backup or temporary files generated by the user's editor of choice)

The funky Method 3

You have two choices if you want to use Method 3:

First though you have to do the following:

  1. Add the file: c:/.gitexcludes
  2. Add the exclusion rules to this file

Now here's the choices I mentioned:

  • Choice 1: Edit the file yourproject/.git/config

    Add the following line under the [core] section of this file:
    excludesfile = "c:/.gitexcludes"


  • Choice 2: Run the command: git config --global core.excludesfile c:/.gitexcludes [my preffered method]

    This will add the same entry from the command line:
    excludesfile = "c:/.gitexcludes"

    You can test this by running: git config -l You should see the entry there: core.excludesfile=c:/.gitexcludes

    NOTE: I haven't yet figured out though what file is modifed by git when you run the git config --global command. If someone knows please let me know. I thought it would be either C:\Program Files\Git\etc\gitconfig or C:\Documents and Settings\\.gitconfig but I don't think that's the case.

Warning: If you are using Egit with Eclipse make sure all the files and folders you want to ignore are in the .gitignore file
When I was testing, for some reason Method 3 above didn't work, adding .gitexcludes a file (if anyone knows how to get that working please let me know)

Example (Method 1)

[project folder structure image comming soon...]

Add .gitignore to your project's folder. Here is how it should look:

 #ignore everything under the 'bin' folder
 #ignore everything under any CVS folders
 #ignore the eclipse configuration files

Now run:
git add .
git status -s

If you made a mistake in your .gitignore file and you still see file patterns that shouldn't be there after you run git status -s, DON'T modify the .gitignore file.

First run this command: git rm . --cached -r then modify your file.
If you get an error and run it with the -f flag, it will apparently delete your files on disk, but this command won't.

I hope this helps someone when dealing with git on windows.