Close

Git - Using Git in Intellij IDE

[Last Updated: Jan 3, 2019]

Intellij (Ultimate and Community versions) have built-in support for Git. This tutorial will show how to configure and use Git for a Java project.

Creating a Project

Create a new Java project:

The project with a Java class is created:

Configuring Git

Since Intellij does not come with Git distribution, we need to install it externally (check out our getting started tutorial for Git installation).

Open Settings>Version Control>Git (Ctrl+Alt+S) and specify git external path as shown (this is just a one time configuration):

Click on the Test button:

Creating Git repository:

Go to VCS>Enable Version Control Integration.., it will ask us to select the version control system and then the project where .git folder will be created:

Selecting our project:

Now the file colors in the project changed to red (red is used for untracked files):

Version Control view:

Open View>Tool Windows>Version Control(Alt+9):

As seen above 'Local Changes' tab shows all untracked files in red color. Let's ignore Intellij specific files.

Ignoring Files

Intellij has a setting to ignore files:

Above view does not create .gitignore file, but it maintains an internal Intellij file for ignoring artifacts. We will not use this view and will create .gitignore file manually at the project's root:

As seen in 'Local Changes' tab, all Intellij specific files disappeared and their color in the 'Project' tree view also turned to normal color.

A plugin called .ignore, can be used which has a various useful functionality for creating/editing .gitignore files.

File status

In Intellij, each file has its own status marked with a specific color, check out this for color-to-status listing. The above color (red) shows that Main.java file is unversioned (untracked). Let's confirm that from git-bash:

Joe@jpc MINGW64 /d/git-with-intellij-example (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/

nothing added to commit but untracked files present (use "git add" to track)

Joe@jpc MINGW64 /d/git-with-intellij-example (master)
$ git status src/
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        src/Main.java

nothing added to commit but untracked files present (use "git add" to track)

Adding files to staging

Right click on the selected files to add them to staging as shown:

Or we can also use right click>Git>Add as shown:

After adding, the file color has changed to green (green is for newly added staged file).

Committing files

Right click on the selected files to commit:

Enter commit message. Also if you don't want to 'perform code analysis' and 'check TODO', uncheck those options (they are checked by default):

Click on 'commit'. Now Files from 'Default changelist' will disappear. The committed files' color will change to the default color:

Making changes to versioned files

Let's make some changes to our previously committed Java file. That will turn its color to blue:

Let's add the file to stagging and then commit. In intellij we can also commit files directly without adding them to staging first. The direct commit will perform both actions for us.

Git logs

To see git logs click on 'Log' tab in Version Control view:

Toggle Select the "Preview Diff", that will show the change difference as well:

Right Click Git menu

Various Git operations can also be performed by invoking right click menu on the selected files:

See Also