IntelliJ IDEA Tutorial Series | GIT Operations EP5 | How to Perform Local Project Repository Commits – COMMIT

Hello, everyone. In our daily development process, whether it’s the end of a development phase or the end of a day’s work, we need to perform local project repository commits for our code. This operation not only saves the code versions but also helps with future tracking work. It is a necessary habit to develop. In this article, we will learn how to commit the completed code versions to the local project repository for storage and recording.

For this operation, I am using IntelliJ IDEA version 2022.2.3 (Community Edition).

Add a new code file to the index of the local project repository.

1.Move the mouse over the project folder, right-click, and then choose “New” -> “File” from the menu that pops up to create a new file. (Note: If you are working on a Java project development, please choose “Java Class”)

IntelliJ IDEA提交操作_1.create_new_file

2.Enter the new file name and press the “Enter” key on the keyboard.

IntelliJ IDEA提交操作_2.new_file_name

3.In the popped-up dialog, it asks if we want to add the new file to the local project index. Here, we click on “Add”. (Note: If you want to skip this prompt in the future and automatically add new files to the index, you can check the “Don’t ask again” checkbox at the bottom left.)

IntelliJ IDEA提交操作_3.add_file_to_git

4.Now you can see that the newly created file has turned green, indicating that this file has been added to the index of the local project repository. (Note: In IntelliJ IDEA, a green file indicates that the file is in a state ready to be committed.)

IntelliJ IDEA提交操作_4.add_file_finish

To commit the completed code file version to the local project repository

1.Add some text to the newly created file.

IntelliJ IDEA提交操作_5.my_first_code

2.Click on the ✔️ icon in the top-right Git shortcut bar or press 【ctrl+k】 on the keyboard.

IntelliJ IDEA提交操作_6.commit

3.At this point, you will see that the left-side project view has switched to the Commit tab. At the top of the window, it displays the affected files in this commit. In the middle, it indicates how many files have been added, modified, or deleted. At the bottom, you can input your commit message. Here, I want to remind everyone to leave a clear and informative message for each commit, describing the purpose of the changes made. This not only helps others understand the changes but also prevents yourself from forgetting the reason behind the code changes in the future, thus enhancing development efficiency. After writing the commit message, click on the 【Commit】 button.

IntelliJ IDEA提交操作_7.commit_window_click_commit

4.After the commit is completed, you will see a clear commit notification at the bottom left corner.

IntelliJ IDEA提交操作_8.commit_finish

5.Finally, let’s click on the “Project” tab or press “Alt+1” on the keyboard to return to the project view.

IntelliJ IDEA提交操作_9.come_back_project_window

Supplemental Note: Regarding automatically pushing to the remote repository after version commits

Observant users may have noticed that on the file commit page, there is a “Commit and Push” button located to the right of the “Commit” button. When clicking this button, IntelliJ IDEA will automatically perform the commit and push the committed changes to the remote repository. While this feature is very convenient, it is essential to exercise caution when using it frequently, as it can lead to unnecessary risks in the remote repository.

In the next article, I will provide several principles as prerequisites for using the “Commit and Push” button. I hope these principles can help you avoid unnecessary troubles and make more informed decisions when utilizing this feature.

Git Commit Operation Demonstration (Using version 2023.1.1)