IntelliJ idea git operation guide

Time:2021-12-3

Write in front

For a long time, I have been using git command line to manage code. When there is only one person, this is no problem at all, but when I participate in team cooperation, the command line seems to be out of my mind. This does not mean that I can’t do it after the command, but the command needs to remember too many commands, the learning cost is too high, and in solving conflicts, if I only rely on the command, That feeling you have experienced once and never want to experience again. After spending a day or two studying the GIT tool of idea, I decided to completely give up the command line and embrace the GUI. Therefore, the research results of these two days are recorded here for your learning and reference.

Create warehouse

Create remote warehouse

The first step is togithubperhapsgiteeThe code management platform creates a good warehouse. Here, take GitHub as an example

IntelliJ idea git operation guide

  • Enter warehouse name
  • Select the warehouse type. Public means that everyone can see it, and private means that only authorized can have permission

After the warehouse is created, it is an empty warehouse by default. At this time, the interface will prompt how to push the code to the warehouse

IntelliJ idea git operation guide

Notice that there is a warehouse address on the interface. The addresses are SSH and HTTPS. What’s the difference between the two

  • HTTPS: the user name and password are required for code management through HTTP protocol
  • SSH: code management through SSH protocol, using a key (you can understand it as a credential)

The operation of pushing warehouse will be described in detail later, and will not be introduced here.

Associated local warehouse

If it is a local existing project, you need to associate the newly created remote warehouse. The specific steps are as follows:

  • Initialize project

Select in the menu barVCS -> Import into Version Control -> Create Git Repository, a folder selection window will pop up after selection. Select the current folder

IntelliJ idea git operation guide

After selecting a folder, idea will create one in the current directory.gitThe folder indicates that the project is already a project using git version control. If your terminal is a friendly terminal (such asOh My ZSH), the current branch will be displayed. The default is the master branch

➜  git-learning git:(master) ✗
  • Associated remote warehouse

The remote warehouse is called in GITRemote, a project can be associated with multiple reomtes, that is, your project can be pushed to multiple remote warehouses at the same time. This is very useful. For example, our code should be pushed not only to the customer warehouse for integration, but also to the company’s warehouse for code review. At this time, you can use multiple remote to push. ClickVCS -> Git -> RemotesAdd a remote warehouse. If it is the first warehouse, the name defaults to origin. It is recommended not to modify the name

IntelliJ idea git operation guide

In this way, we have completed the association between the local warehouse and the remote warehouse, and then we can submit the code and create branches.

Clone warehouse

If you need to synchronize the warehouse created and submitted by others to the local, you need to clone the warehouse and selectFile -> New -> Project from Version Control, enter the remote warehouse code address

IntelliJ idea git operation guide

gitignore

In the project, we do not want to submit some project files or program runtime files to the warehouse, so we need to prepare a file named.gitignoreWrite all the files you don’t want to submit to this file, and git won’t submit and manage these files.

A. Gitignore sample

/out/
/classes/
.mvn
/node_modules/
.idea
*.iws
*.iml
*.ipr
.DS_Store
/target/

You can right-click a file or folder to add it to gitignore

IntelliJ idea git operation guide

Submit code

The modified code needs to be submitted to the local warehouse. This process is calledCommit, after the code is submitted to the local warehouse, you can submit the commit to the remote warehouse. This process is calledpush, the process of updating code from a remote warehouse is calledpull, these three git operations are the three most commonly used operations. There are buttons corresponding to these three operations in the upper right corner of the idea

IntelliJ idea git operation guide

  • ① Update code, pull operation
  • ② Submit to the local warehouse and commit
  • ③ Submit to remote warehouse and push

Click the commit icon to submit the code. The code submission interface is as follows

IntelliJ idea git operation guide

Note that ① there is a check box in front of the file. If it is a newly added file and there is no special configuration, it will not be checked automatically. The newly added file needs to be checked manually to submit. You can directly select all the top-level folders. After committing, you need to push to synchronize to the remote warehouse. Click the push button

IntelliJ idea git operation guide

After the push is successful, you can log in to the remote warehouse to view the latest code

IntelliJ idea git operation guide

Notice that the system prompts us to add a readme file. Readme is a file namedREADME.mdThe readme file is located in the root directory of the project. A good readme file helps others quickly understand the project. For how to write a readme file, please refer to the following documents

After modifying the code, the local submission will be compared with the last submission in the submission interface to facilitate us to view the modified content

IntelliJ idea git operation guide

Rollback code

Git rolls back code in two ways

  • reset
  • revert

What’s the difference between the two? Suppose we commit three times

commit1 -> commit2 -> commit3

If I want to roll to commit2, if I use reset, all modifications after commit2 will be discarded, that is, all the commits after commit2 will be cut off. Revert is a bit like undo. It will reverse the operations done by commit2, that is, undo, and generate a new commit, which becomes

commit1 -> commit2 -> commit3 ->4

What is the specific application scenario? For example, when debugging, we may add some debugging statements, such as system.out. When debugging is completed, we hope to remove these debugging statements and submit a commit before debugging. After debugging, reset to the commit. Of course, you can also implement it through branches, After debugging, delete the branch.

Our system is constantly iterating. Suddenly one day, the product manager says that a function that has been online before is no longer needed, so you can find the commit corresponding to the function and reverse it, so that the corresponding function can be removed and subsequent new functions can be retained.

reset

On the idea toolbar, clickGitToolbars, switching toLogOn the tab, you can see the local commit log and remote commit log. Right click on the specified commit to selectReset Current Branch to HearYou can roll back to the commit in the way of reset

IntelliJ idea git operation guide

There are four rollback modes

<img style=”zoom:60%;” />

choiceHardMode, the local version will be cleared and forced to roll back to the specified commit state. However, the push operation cannot be performed through reset because the local version is lower than the remote version. At this time, the local version can be forced to push to the remote branch

IntelliJ idea git operation guide

By default, the idea prohibits forced push on the master branch. If the master branch needs forced push, you can use the command line on the terminalgit push --force

Force push is a very bad habit. Unless you are clearly aware of the consequences of Force push, do not implement force push

revert

Revert can undo all the modifications made by the specified commit without affecting the modifications of other commits. Suppose we have three files, file1.txt, file2.txt and file3.txt. Now submit three commits, and the three commits are

  • Commit1: add content to file1.txt
  • Commit2: add content to file2.txt
  • Commit3: add content to file3.txt

IntelliJ idea git operation guide

Now we areCommit2: file 2 modificationRight click on the commit to selectRevert CommitAt this time, a submission window will pop up. Idea will automatically submit a new commit. After the commit, we find that the content of file 2 is gone, that is, the modified content of commit2 is revoked. After the commit, we can push it to the remote warehouse by executing the push operation. Because a new commit is generated, we can submit it to the remote warehouse without force push

Code conflicts often occur during revert operation. In this case, you need to manually solve the conflicts. The solutions will also be described in detail below

Branch Management

Branching is a very important concept in GIT. If there is no git, our system is online and runs stably online. At this time, you need to develop subsequent functions, but you want to keep the current online version of the code, because there may be emergency bugs online that need to be repaired and you need to use the current version for modification, so you may copy a copy of the code for backup, After the new function is developed, you need to integrate the code modified on the backup code during this period. If this process is carried out manually, it is very error prone, so git provides a branch to solve this problem. The code running online can be on the main branch, such as the master branch. When developing new functions, you can cut a branch from the master branch, such as the dev branch, For bug repair, you can also cut a branch from the master branch, such as the fix branch. When the bug is repaired, merge the fix branch into the master branch. When the new function is developed, integrate the dev branch into the master branch to complete all the above operations.

New branch

Displays the current branch in the lower right corner of the idea

IntelliJ idea git operation guide

Click open to create a new branch under the current branch

IntelliJ idea git operation guide

You can also cut a branch from a commit by right clicking on the commit logNew Branchthat will do

Switch branch

If you want to switch to another branch, click the branch you want to switch and selectCheckout

IntelliJ idea git operation guide

Before switching branches, you need to submit or temporarily store the modifications of the current branch, which will be mentioned later

Branch merge

For example, if I want to merge the fix branch into the dev branch, switch the current branch to dev and select the fix branchMerge into Current

IntelliJ idea git operation guide

There may be conflicts during the consolidation process. The consolidation can only be completed after the conflicts are resolved. Conflict resolution will also be mentioned below

After merging the branches, it is recommended to delete the fix branch to avoid mistakenly submitting the branch to the remote warehouse, resulting in branch confusion.

rebase

Note that there is another one on merge in the above menuRebase Current onto SelectedThere are two ways to merge. One is merge and the other is rebase. What’s the difference between the two? For example, at 1 pm, rebase is merged, and 2 and 3 are merged

IntelliJ idea git operation guide

  • Merge keeps branch commit information
  • Merge will create the commit information of the merge branch once
  • Compared with merge, rebase will integrate the commit on the branch into the current branch, making the whole code cycle clearer
  • Compared with merge, rebase does not generate redundant commit information

Rebase and merge have their own advantages, depending on the specification requirements of the project.

conflict resolution

No matter rebase or merge, conflicts will inevitably occur during merging. The so-called conflict refers to the process in which the codes on two branches modify the same place. The system cannot judge which code to keep, and manual intervention is needed to solve the conflict. If there are conflicts, all conflicting codes will be listed when merging

IntelliJ idea git operation guide

There are three options to choose from

  • Accept your: keep the current branch code and discard the merged branch code
  • Accept them: keep the merged branch code and lose the current branch code
  • Merge: merge manually

Select merge to enter the manual merge code interface

IntelliJ idea git operation guide

  • ① Current branch code
  • ② Merged code
  • ③ Merged branch code

Idea will highlight different on both sides, and you can merge them according to the actual situation

IntelliJ idea git operation guide

A confirmation box will pop up after merging. If all conflicts have been resolved, click OKApply Change and Mark Resolved, indicating that the conflict has been resolved

Label management

create label

Generally, when we release a new version, we will put a tag on the project to indicate its importance. On gihub, we often see that the project will be labeled when releasing a new version

IntelliJ idea git operation guide

Right click commit on the commit log to create a label for the commit. The label name is usually the version number, such asv1.0 v1.1etc.

IntelliJ idea git operation guide

When pushing the code, you can choose to submit the tag to the remote warehouse together

IntelliJ idea git operation guide

In this way, we can see the tag in the remote warehouse, and the system will automatically package and archive the corresponding commit code at that time

IntelliJ idea git operation guide

Pull the code according to the label

For example, we need to repair the v1.0 code. We need to pull the v1.0 code and merge it into the current branch after the repair

  • Pull code according to tag

IntelliJ idea git operation guide

You can pull the code by entering the label name, but the branch name at this time is the commitment number corresponding to the branch. In other words, it is a commitment. We need to create another branch on the branch to modify the code. The creation method is the same as above, that is, click the branch name,New BranchAfter modification, submit and merge according to normal branches.

patch

patch

You can create a patch for one or more commits. Idea will create a. Patch file, which records the changes of the file

IntelliJ idea git operation guide

Patches can be imported on the branch,VCS -> Apply Patch

IntelliJ idea git operation guide

Patching is also a way of merging. It also needs to solve the problem of code conflict

cherry-pick

Similar to patching, cherry pick can directly apply one or more commits of other branches to the current branch without exporting the patch file

IntelliJ idea git operation guide

Recommended Today

Win10 system comes with screenshot tool

How to use the screenshot function of win10 system The screenshot function is often used in the process of daily computer use. In fact, win10 system itself has the screenshot function. The following describes how to use the screenshot function of win10 system. Tools / raw materials  Computer system: Windows 10 professional edition Method / […]