Preface
Generally speaking, version control is generally inseparable during project development, and the current mainstream version control tools are nothing more than Git and SVN, and now the usage rate of Git can be said to have far exceeded SVN. This article will mainly look at how Use IDEA + Git to control the code version and make team development more smooth!
Git installation and configuration
Since you want to use Git, the first thing to do is to install Git. The specific installation process will not be mentioned, and the next step will be the next step.
Next, let's talk about what happened when we installed Git. After installing Git, open IDEA's setting interface and enter File -> Settings -> Version Control -> Git
(you can also Ctrl + Alt + S
), and then set Path to Git executable
to the path where you installed Git, for example, mine is D:\Program Files\Git\bin\git.exe
.
After the setting is complete, click Test
on the right. If the Git installation is successful and the path is correct, the following prompt message will pop up, indicating that our configuration is also successful.
‘
Github settings
Similarly, we can find that Version Control
is also a Github tab in 061d502b7f3954, where we can log in with our own Github account, that is, Log In via Github
, and then we can directly pull down the code on our Github.
After the login is successful, the relevant information of our Github account will appear, as shown in the figure below.
Pull remote Git warehouse code
Here may be more to pull the code in the company's Git library to local for collaborative development, but there is no way to demonstrate here, so I use IDEA to pull code from Github as an example to demonstrate how to pull from a remote Git repository Code.
There are mainly two ways here. The first is to pull the code locally through the Git tool in advance, and then open it through IDEA; the other is to pull the code directly through IDEA. The two are demonstrated below. Kind of operation.
Pull first, then open
First find the Github repository we want, and then copy the Github repository path.
Then open the terminal and use the following command to clone to the local.
git clone Githu 仓库地址
git clone https://github.com/cunyu1943/java-programming-instance.git
After the pull is successful, we can open the project through IDEA, enter File -> Open
one by one, and then find the path of the code cloned in the previous step to open the project.
Pull directly to open
In addition to the above methods, we can also directly pull the code through IDEA, enter File -> New -> Project from Version Control
in turn, and then there are two ways to pull.
- URL pull
URL
with the warehouse library address url we copied in the previous part, and then Version control
selects Git
, Directory
is the local path where we want to store the code, and the code pulled down later will be placed in this path.
- account pull
In the Github settings section, we have logged in to our Github account. At this time, we can also use the Github option, and then find the warehouse where we want to pull the code. Here, we can search and quickly locate it through the search box at the top, and then click Clone
below to pull it. Take, Directory
same as the above pull method, and it is also the path where we pull the code and store it.
In addition, there are Githu Enterprise
and Space
, but these two methods are more inclined to companies or organizations. Generally, individuals do not need to use this part. It is good to know here. The method of use is similar to the above two methods. .
Update code
Generally, when submitting the local code to the remote warehouse, it is best to update the code of the remote warehouse to the local first to avoid unnecessary conflicts.
The update method is also very simple, you can use the shortcut key Ctrl + T
to pull, or you can use the button in the toolbar.
There are two ways to update, one is Merge incoming changes into the current brance
, and the other is Rebase the current branch on top of incoming changes
.
Merge
is to find the ancestor commit
two branches, and then merge the latest version of the public branch into its own branch to form a new commit
submission, which is shown as follows.
Rebase
is based on a branch again commit
, that is, the current branch from the ancestor commit
submitted after the commit
are revoked, put them in a cache, and then based on a branch, the cached commit
in order Add to the back of the branch, as shown in the figure below, put the commit
submitted by 4 and 5 in the cache, and then add them to the back of 6 in order.
The basic principles of using Rebase
and Merge
can be summarized as follows:
-
Rebase
when updating the upstream branch content -
Merge
when the upstream branch merges the content of the downstream branch -
--rebase
parameter when updating the content of the current branch
For more information about Rebase
and Merge
, you can refer to this article:
https://zhuanlan.zhihu.com/p/34197548
In addition to the way to open the shortcut keys and click the button in the toolbar to update, you can also click the right mouse button on the project, and then enter Git -> Pull
to update.
Submit code
After the local development work, we need to submit our code to the remote warehouse. At this time, it can also be done in the following two ways.
- shortcut key
Open the code submission workspace with the shortcut key Ctrl + K
, and you can see that the files are marked in different colors, generally red, green, and blue. The meaning of the three is as follows:
- Red : Indicates that the file has not been added to version control, such as the
target
folder in the Java project. Here we can.gitignore
file. - Green : Indicates that the file is newly added to version control, such as a newly created file in our project, but it has not yet been submitted to the remote code repository at this time.
- blue : indicates that the file is a modified file, and the warehouse already exists in our remote code warehouse, but we have modified it and has not yet submitted it.
In addition, there is a Amend
area, which is mainly used to fill in our submission information.
After checking the code files we need to submit and filling in the submission information, we can click the commit
button to complete the code submission.
- Toolbar button
Similarly, we can also submit through the button in the toolbar. After clicking the button, the interface that appears should be the same as the interface through the shortcut key submission method.
Push code
After submitting the code, only the code is submitted to the local buffer. To actually submit it to the remote code warehouse, we also need to push. There are also many ways to push.
- shortcut key
Through the shortcut key Ctrl + Shift + K
, we can open the following interface, at this time we only need to select the record we want to submit, and then click the Push
button at the bottom right.
- Toolbar button
Through the toolbar buttons, we also carry out the push work. After clicking the buttons in the figure below, the interface that appears should be the same as the interface pushed through the shortcut keys.
- Right mouse button
By clicking the right mouse button on the project, and then selecting Git -> Push
, we can also perform our push work.
- simultaneously submitted push
Perhaps when submitting, you will find that in addition to the Commit
button in the lower left corner, there is also a Commit and Push
button. By clicking this button, we can pop up the push decoding while submitting, then click the record that needs to be pushed, and click the Push
button at the bottom right.
Branch management
Generally speaking, our project must have multiple branches. Suppose we want to switch branches in IDEA at this time, what should we do?
At this point, you need to pay attention to the status bar at the bottom of IDEA. Here we can not only switch between different branches, but also perform a series of operations such as Rebase
, Merge
, Pull
Summarize
Well, the above is all the content of this article, mainly from Git installation and configuration, Github settings, pull remote code, update, submit, push code, branch management and other aspects to introduce you how to use Git in IntelliJ IDEA, hopefully this can provide beneficial help to everyone.
At the same time, if you have any questions, you can also talk freely in the comment area, and I will reply as soon as I see it!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。