Mastering Git and GitHub Integration in IntelliJ IDEA : A Complete Guide to Version Control Commands
IntelliJ IDEA offers seamless integration with Git, providing a range of version control operations within the IDE itself. This guide covers everything from setting up Git in IntelliJ IDEA to using all the essential Git commands within the interface.
1. Setting Up Git in IntelliJ IDEA
1.1 Installing Git:
- Step 1: Download and install Git from the official website: Git Downloads.
- Step 2: After installation, open IntelliJ IDEA, go to File > Settings > Version Control > Git (for Mac: IntelliJ IDEA > Preferences > Version Control > Git).
- Step 3: In the Git Path, specify the path to the Git executable. IntelliJ will automatically detect it if installed properly.
1.2 Setting Up GitHub:
- Step 1: Go to File > Settings > Version Control > GitHub (for Mac: IntelliJ IDEA > Preferences > Version Control > GitHub).
- Step 2: Click Add Account, log in with your GitHub credentials or token.
2. Creating or Cloning a Repository
2.1 Creating a New Repository:
- Step 1: Open IntelliJ IDEA and your project.
- Step 2: Navigate to VCS > Import into Version Control > Create Git Repository.
- Step 3: Select the root directory of your project and click OK. This initializes a Git repository in your project directory.
2.2 Cloning a GitHub Repository:
- Step 1: Navigate to VCS > Git > Clone.
- Step 2: Enter the URL of the GitHub repository in the “URL” field.
- Step 3: Select a directory to clone the repository into and click Clone
3. Basic Git Operations
3.1 Checking the Status:
- Navigate to VCS > Git > Show Changes (Alt + 9) to check the current status of your repository (modified, untracked files, etc.).
3.2 Adding Files:
- Git Add: Right-click the file in the Project view and select Git > Add or simply use the keyboard shortcut Ctrl + Alt + A.
3.3 Committing Changes:
- Step 1: Go to VCS > Git > Commit (Ctrl + K).
- Step 2: In the Commit Changes window, select the files you want to commit, add a commit message, and press Commit.
3.4 Pushing Changes:
- After committing, navigate to VCS > Git > Push (Ctrl + Shift + K) to push changes to a remote repository.
3.5 Pulling Changes:
- To sync your local repository with the latest changes from the remote, navigate to VCS > Git > Pull.
4. Branching and Merging
4.1 Creating a New Branch:
- Navigate to VCS > Git > Branches, then select New Branch and name your branch. This will create a new branch based on your current one.
4.2 Switching Between Branches:
- Go to VCS > Git > Branches and select the branch you want to switch to.
4.3 Merging Branches:
- Navigate to VCS > Git > Branches, select the target branch, and click Merge into Current to merge another branch into your current branch.
4.4 Resolving Merge Conflicts:
- If there are conflicts during a merge, IntelliJ will display a conflict window. You can choose to accept changes from your current branch, the target branch, or manually edit and resolve conflicts.
5. Advanced Git Operations
5.1 Stashing Changes:
- If you want to temporarily store changes without committing, navigate to VCS > Git > Stash Changes. To apply the stash later, go to VCS > Git > Unstash Changes.
5.2 Rebasing:
- Navigate to VCS > Git > Rebase to rebase your current branch onto another branch. This will allow you to replay your commits on top of the target branch.
5.3 Cherry-Picking:
- If you want to apply a specific commit from one branch to another, go to VCS > Git > Show History, right-click the commit and select Cherry-Pick.
5.4 Reverting Commits:
- To undo a commit, go to VCS > Git > Show History, right-click the commit, and select Revert. This will create a new commit that undoes the changes made by the selected commit.
5.5 Resetting:
- Go to VCS > Git > Reset HEAD, select the reset type (Soft, Mixed, or Hard), and apply it to undo commits or changes.
6. GitHub-Specific Operations
6.1 Creating a GitHub Repository from IntelliJ:
- Navigate to VCS > Import into Version Control > Share Project on GitHub. Enter the repository name, description, and privacy settings.
6.2 Creating a Pull Request:
- After pushing changes, navigate to VCS > Git > GitHub > Create Pull Request to create a pull request directly from IntelliJ.
6.3 Viewing GitHub Issues:
- Go to VCS > Git > GitHub > View Issues to check the issues associated with your repository.
7. Version Control Tab and Logs
7.1 Local Changes:
- Navigate to the Version Control tab (Alt + 9) to see your local changes. You can view modified files, perform Git actions (add, commit, etc.), and more from this tab.
7.2 Log Tab:
- The Log tab provides a history of commits, branches, merges, and more. You can inspect individual commits, view diffs, and navigate through your project’s Git history.
8. Customizing Git in IntelliJ
8.1 Ignoring Files (.gitignore):
- You can manually create a
.gitignorefile in the root of your project, or use VCS > Git > Gitignore to add files to be ignored from Git tracking.
8.2 Configuring Git Settings:
- Go to File > Settings > Version Control > Git (or Preferences > Version Control > Git for Mac) to customize your Git settings, including SSH keys, remotes, and update methods.
9. Shortcuts for Efficient Git Usage
- Commit:
Ctrl + K - Push:
Ctrl + Shift + K - Pull:
Ctrl + T - Add to VCS:
Ctrl + Alt + A - Show History:
Alt + 9 - Branching:
Ctrl + Shift + Alt + N - Show Git Log:
Ctrl + 9 - View Differences:
Ctrl + D
10. Tips for Better Git Workflow in IntelliJ
- Auto-fetch: Enable auto-fetching in Settings > Version Control > Git to automatically sync with the remote repository.
- Interactive Rebase: Use interactive rebase to clean up commit history. Go to VCS > Git > Rebase, and enable interactive mode to edit, reorder, or squash commits.
- Hooks: Use Git hooks to trigger scripts during specific Git actions (e.g., pre-commit hooks for linting or testing). These need to be set up manually in your
.git/hooksdirectory.
This comprehensive guide should provide you with all the tools you need to effectively manage Git and GitHub repositories using IntelliJ IDEA. Whether you’re working on solo projects or collaborating on large codebases, IntelliJ’s built-in Git integration makes version control straightforward and efficient.
👏 If you found my articles useful, please consider giving it claps and sharing it with your friends and colleagues.
To read other topics
- @Formula Annotation in Spring Boot
- @AssociationOverride, @AttributeOverrides, @Embeddable, @Embedded Annotation.
- Using @Temporal Annotation in Hibernate using Spring boot
- Mastering Transaction Propagation and Isolation in Spring Boot
- One To One mapping in Spring Boot JPA
- One To Many mapping in Spring Boot JPA
- Pessimistic Locking in JPA with Spring Boot
- Optimistic Locking in JPA with Spring Boot
- Optional in Java 8






