Using Git in IntelliJ IDEA
1. Uploading a Project Created in IDEA to a Remote Repository
1.1 Initialize a Local Repository
Create a Maven project in IDEA and manage it with a local Git repository:
- Create a Maven project in IDEA.
- Go to the top menu: VCS (Version Control) -> Create Git Repository.
- Select your project directory. The directory will become a repository, and Git icons will appear at the top of the IDEA interface.
1.2 Associate the Local Repository with a Remote Repository on Gitee
- Right-click the project name -> Git -> Manage Remotes.
- In the Git Remotes dialog, click the
+
button. - Enter the remote name (e.g.,
origin
) and URL (e.g.,https://gitee.com/kxzhu93/git_test.git
) to associate the local repository with the remote one.
1.3 Add, Commit, and Push Changes from IDEA to the Remote Repository
Use the Git icons to add, commit (select Unversioned Files), and push changes from your local repository to the remote repository.
2. Cloning a Remote Repository and Pushing Changes Back
2.1 Clone a Remote Repository
Clone a repository from Gitee to view, modify, and push changes locally:
- Top menu: VCS (Version Control) -> Get from Version Control.
- Enter the repository URL (e.g., HTTPS), and log in with your Gitee credentials.
- Alternatively:
- On IDEA’s startup page, choose Get from Version Control.
- Or, go to Git -> Clone.
2.2 Push Changes Back to the Remote Repository
Add, commit (select Unversioned Files), and push changes from IDEA using the Git icons.
3. Pulling Updates from a Remote Repository
If the remote repository has updates and you need to pull them locally:
- Click the Git icon -> Update Project -> OK.
Configuration
After installing Git, configure IDEA:
- Go to Settings -> Version Control -> Git.
- IDEA should automatically detect the Git executable path. Click Test to verify. If the version number appears, the configuration is correct.
Working with Git Repositories
1. Initialize a Local Repository
To manage a Maven project in IDEA using Git:
- Top menu -> VCS (Version Control) -> Create Git Repository.
- Select the project directory. Git icons will appear in the interface.
2. Clone a Remote Repository
Clone a repository from Gitee to view, modify, and push changes:
- Top menu: VCS (Version Control) -> Get from Version Control.
- Enter the repository URL, log in with Gitee credentials, and clone.
- Alternatively:
- On the startup page, choose Get from Version Control.
- Or, go to Git -> Clone.
The .gitignore
File
Purpose: Defines which files in the repository should be ignored by Git. Examples include .git
, target
folders, .iml
files, etc.
How to Create:
- When creating a remote repository on Gitee:
- Choose Initialize Repository.
- Select Java as the language.
- Add a
.gitignore
file for Java. The file will appear in the root directory of the repository.
Local Repository Operations
1. Add Files to the Staging Area
Method 1: When creating a new class, click Add in the dialog box to stage the file (equivalent to
git add
).Method 2: Right-click the file -> Git -> Add.
Red: Files not staged.
Green: Files staged.
Black: Files under Git management.
2. Commit Files from Staging Area to Local Repository
Equivalent to git commit -m "message"
.
- Method 1: Right-click the file -> Git -> Commit File. Select the files to commit and add a commit message.
- Method 2: Use the green checkmark in the Git toolbar to stage and commit files in one step.
3. View Logs
Equivalent to git log
.
- Use the clock icon in the Git toolbar (Show History) to view:
- Local Changes.
- Logs of all project changes. Click a version to see modifications.
Remote Repository Operations
1. View Associated Remote Repositories
Right-click the project name -> Git -> Manage Remotes.
2. Add a Remote Repository
Associate a local repository with a remote repository on Gitee:
- Right-click the project name -> Git -> Manage Remotes.
- In the Git Remotes dialog, click
+
. - Enter the remote name (e.g.,
origin
) and URL (e.g.,https://gitee.com/kxzhu93/git_test.git
).
Note: A local repository can be associated with multiple remote repositories.
3. Push to a Remote Repository
Right-click the project name -> Git -> Push.
master -> origin:master
: Pushes the localmaster
branch to the remoteorigin
‘smaster
branch.
4. Pull from a Remote Repository
If the remote repository has updates:
- Use Git -> Pull.
- Or, click the Git icon -> Update Project.
You can check the remote repository and branch under Remote.
Branch Operations
1. View Branches
- Use Git -> Branches.
- Or, check the bottom-right corner of IDEA for branch information.
2. Create a Branch
- Click the branch menu at the bottom-right corner of IDEA -> New Branch.
- Select Checkout Branch to switch to the new branch upon creation.
3. Switch Branches
- Click the branch menu at the bottom-right corner of IDEA -> Select the branch under Local Branches -> Checkout.
4. Push a Branch to a Remote Repository
Push a locally created branch to a remote repository:
- Click the branch menu at the bottom-right corner of IDEA -> Select the branch -> Push.
5. Merge Branches
To merge branches in IDEA:
- Switch to the target branch (e.g.,
main
). - Click the branch to merge (e.g.,
b1
) -> Merge into Current.
Example: Merge branch b1
into main
.