labs / tiddlers / content / labs / lab02 / _Labs_02_Add the .gitignore

We added a task to a build script for creating the .gitignore file in last week's lab, but we didn't really discuss the purpose of the file.

Many of the problems that we see students having with Git is caused by the student committing some build output or other rubbish such as Gradle caches to their repository. Build output and caches change very frequently which makes it easy for merge conflicts to occur if these files are part of your repository. Another common source of problems is the DS_Store files that Macs like to leave all over the place --- again because these files change frequently which can cause merge conflicts.

The best way to prevent these problems is to keep the rubbish out of the repository in the first place. The way to do this in Git is via the .gitignore file. This file generally resides in the root of the repository or project folder (or both --- you can have many .gitignore files in a single repository).

It can be a little bit tricky to create this file since pretty much all platforms hide 'dot' files (files that start with a '.') by default since they are generally considered to be system configuration files that users don't need to see most of the time. Windows Explorer outright refuses to create dot files, so you usually need to use the terminal. Macs have no way of creating files from the finder at all.

This is why we added a Gradle task to the build script in last week's lab --- having a convenient way to create a .gitignore saves a lot of hassle.

  1. Run the task now by selecting the root of the project in the Projects pane and then running the <

    createGitIgnore">> task via the Navigator pane.

  2. If you ever need to exclude anything else from your repository you can open the .gitignore file via the Files pane of NetBeans. Open the file now to take a quick look at it.