.gitignore
A .gitignore
file specifies intentionally untracked files that Git should ignore.
For example, a gradle project uses a folder called .gradle
to store all the files associated to the dependencies that you add to your project. (The folder may be hidden in your computer.) These files are system dependent and should not be added to a Git repository.
As another example, IntelliJ by default creates a build
folder to store all the compiled Java (.class
) files. These files should not be added to the Git repository.
Generally speaking, you should not add these four types of files into your Git repository:
- Files that don't belong to the project like
.DS_Store
(for Mac OS),Thumds.db
(for Windows). - Files that are automatically generated like those in
build
folder. - Libraries (depends on the situation) like those in
.gradle
folder. - Credentials and secretes (we'll explore this soon)
You can manually create a .gitignore
file (notice the leading dot) in the root of your Git repository (which is the class-search-app
folder for this example) and list the files and folders to be ignored.
For example, I am going to add the following content to my .gitignore
file:
.gradle
build
.DS_Store
__MACOSX
My computer is a MacBook; the macOS generates system files .DS_Store
and __MACOSX
which are typically hidden (so you will not see them by default but Git will see and track them unless you tell it not to).
You can use this online tool to generate
.gitignore
for different operating systems, project environments, etc.
Lucky us, someone has created a plugin for IntelliJ that uses the API for the online tool noted above. The plugin is called .ignore.
IntelliJ Plugins extend the functionality of the IDE. The general process to manage and install plugins are describe here.
After installing the plugins, restart the IDE. Then open IntelliJ again. Right click on the root of the project and select New $\rightarrow$ .ignore File $\rightarrow$ .gitignore File (Git).
From the prompt, elect Gradle, JetBrains, Java, and macOS (or Windows or Linux, if you are using those operating systems).
Now an elaborate .gitignore
file has been added to the root of the Git repository. (If IntelliJ asked you "Add file to Git", say "Yes".)