96

What should be the content of the .gitignore file for a java project in netbeans?

Humphrey Bogart
  • 7,423
  • 14
  • 52
  • 59
thuaso
  • 1,085
  • 2
  • 9
  • 7
  • 1
    Depends on the OS you're developing on too, most Mac developers include `.DS_Store` as this is generated by OS X, in most directories, and is superfluous to your application. – Greg K Apr 06 '10 at 15:36
  • I an developing in windows. In question I mean the some certain files like .DS_Store. For example should i include build folder in .gitignore? – thuaso Apr 06 '10 at 15:39
  • 3
    This is, more or less, a duplicate of http://stackoverflow.com/questions/1267403/which-netbeans-projects-files-should-go-into-source-control – David J. Sep 23 '11 at 03:10

3 Answers3

84
# NetBeans specific #
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml

# Class Files #
*.class

# Package Files #
*.jar
*.war
*.ear
mono68
  • 2,080
  • 19
  • 27
  • 1
    Just a note that this closely aligns with the NetBeans .gitignore hosted on GitHub: https://github.com/github/gitignore/blob/master/Global/NetBeans.gitignore – Steven Mar 21 '14 at 16:56
  • 1
    GitHub's NetBeans .gitignore allows .jar files to be included in the repo. I doubt that's best practice, but it does make my life easier. – Neal Ehardt Feb 18 '15 at 00:03
  • ...and, like the GitHub `.gitignore` for NetBeans, it isn't quite correct. See http://stackoverflow.com/q/24139478/421049 . – Garret Wilson Aug 11 '15 at 21:36
  • 3
    Add leading slashes to the names of directories to be ignored. For example: "/build/" instead of just "build/". Otherwise if you have a directory called "build" somewhere down in the directory structure, it will be ignored, which is probably not what you want. – Duncan Dec 04 '15 at 02:19
66

There are a fair number of files that you probably do not need to commit into git, since they are built, are generated by NB or contain environment specific information.

If you create a project that uses Ant as the build mechanism, you usually end up with a directory tree that looks like this...

project-root-directory/
+ nbproject/
  build-impl.xml
  + private/
  + project.properties
  + project.xml
+ src/
+ test/
+ build.xml

After you do a build.. there will be a couple additional directories

project-root-directory/
+ build/
+ dist/
+ nbproject/
  build-impl.xml
  + private/
  + project.properties
  + project.xml
+ src/
+ test/
+ build.xml

You should probably put the build, dist and nbproject/private directories (and their children) into your .gitignore.

If you want to be very aggressive about excluding files, you may want to consider excluding all the files that appear in nbproject EXCEPT project.properties and project.xml. The other files in the nbproject directory are regenerated by NetBeans when the project is opened.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • 4
    [A very similar question was asked a year earlier](http://stackoverflow.com/questions/1267403/which-netbeans-projects-files-should-go-into-source-control/1267580#1267580) and the best answer pointed to a [NetBeans Knowledge Base Article](http://netbeans.org/kb/docs/java/import-eclipse.html#versioning) that gives a similar answer as you did. – David J. Sep 23 '11 at 03:43
  • 2
    @DavidJames -- broken link now – Jason S Dec 23 '16 at 18:40
  • @JasonS My first link is still valid; I can't edit the second link. More context at [Is there a way to fix broken links in comments? Can the mods edit comments?](http://meta.stackexchange.com/questions/135647/is-there-a-way-to-fix-broken-links-in-comments-can-the-mods-edit-comments) – David J. Dec 24 '16 at 00:39
  • Here it is a [link to that NetBeans Knowledge Base Article](https://web.archive.org/web/20121018213758/http://netbeans.org/kb/docs/java/import-eclipse.html) from the Internet Archive. The relevant section seems to be "Version Control Considerations" – Rafael Atías Jun 02 '22 at 08:30
1

There should be no NetBeans-specific files in your .gitignore. The .gitignore file is project-specific but shared between developers, IOW there should only be things in there that are common for all developers working with the code (including ones that use OSX, Linux instead of Windows and Eclipse, IntelliJ or Notepad as editors) and that are specific to the project.

If there are some files that you would like to ignore based on your specific environment (like e.g. Windows Thumbs.db and desktop files or NeBeans nbproject directories) you should do that in your global ignore list, not in the project-specific .gitignore – if only because then you don't need to add them to every single of your projects individually.

If the files you want to ignore are both specific to your environment and specific to the project, put them into that repository's .git/info/exclude.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 2
    Why use .git/info/exclude rather than the projects .gitignore? What is the benifit / downside if put in .gitignore? – Andrew Burns Oct 29 '10 at 19:33
  • 2
    I disagree; it doesn't hurt an IntelliJ user if an Eclipse user checks in appropriate (e.g. not auto-generated) project files, or vice versa. It actually can help when one member of the project team needs to work with that other IDE. (This might happen during troubleshooting or pair programming.) – David J. Sep 23 '11 at 03:41
  • @Andrew Burns The difference between .gitignore and .git/info/exclude is that the former will be versioned (git tracks changes), while the latter does not. I use .gitignore because I sometimes change what is ignored and want that associated to version. I liked t [sample .gitignore files here](http://sujee.net/tech/articles/gitignore/). – Ivin Aug 07 '11 at 07:05
  • 1
    nb-configuration.xml should not be ignored. It is intended to be shared among developers. – DejanLekic Nov 24 '14 at 16:51