- Git overview
- Git code hosting service
- Git common commands
- Using Git in IDEA
1 Introduction
1.1 Git first experience
Git can be integrated in the IDEA development tool (Git installation and integration process will be explained later):
After integration, you can see Git related icons in IDEA:
The use of Git can be demonstrated by simulating two developers by launching two IDEA windows:
Other version control tools:
- SVN
- CVS
- VSS
1.2 What can be done with Git
- Code backtracking: Git will record logs in the process of managing files to facilitate rollback to historical versions
- Version switching: Git has the concept of branches, and a project can have multiple branches (versions), which can be switched arbitrarily
- Multi-person collaboration: Git supports multi-person collaboration, that is, a team develops a project together, and each team member is responsible for a part of the code, which can be managed and coordinated through Git
- Remote backup: Git manages files through warehouses. There are remote warehouses in Git. If local files are lost, they can also be obtained from remote warehouses.
2. Git overview
2.1 Introduction to Git
Git is a distributed version control tool, usually used to manage source code files in the software development process. These files are stored and managed through Git warehouses, which are divided into two types:
- Local warehouse: the Git warehouse on the developer's own computer
- Remote repositories: Git repositories on remote servers
explain:
commit: Submit, save local files and version information to local warehouse
push: push, upload the local warehouse file and version information to the remote warehouse
pull: pull, download the remote warehouse file and version information to the local warehouse
2.2 Git download and installation
Download address: https://git-scm.com/download
Just double-click to complete the installation. After the installation is complete, you can click the right mouse button in any directory. If you can see the following menu, the installation is successful:
Git GUI Here: Open the Git GUI
Git Bash Here: Open the Git command line
The Git installation directory structure is as follows:
3. Git code hosting service
3.1 Commonly used Git code hosting services
There are two types of repositories in Git, local repositories and remote repositories . So how do we build a Git remote warehouse ?
We can use some code hosting services provided on the Internet to achieve this, among which GitHub, Code Cloud, GitLab, etc. are more commonly used.
name | url | illustrate |
---|---|---|
gitHub | https://github.com/ | A hosting platform for open source and private software projects, because it only supports Git as the only repository format for hosting, hence the name gitHub |
code cloud | https://gitee.com/ | A code hosting platform in China, because the server is in China, compared to GitHub, Code Cloud will be faster |
GitLab | https://about.gitlab.com/ | An open source project for a warehouse management system, using Git as a code management tool, and a web service built on this basis. |
BitBucket | https://bitbucket.org/ | A source code hosting site that uses Mercurial and Git as distributed version control systems, and offers business plans and free accounts |
3.2 Code cloud code hosting service
Code cloud URL: https://gitee.com/
The operation process of using code cloud is as follows:
- Registration code cloud account
- login code cloud
- Create a remote repository
- Invite other users to become repository members
3.2.1 Registration code cloud account
Registration URL: https://gitee.com/signup
3.2.2 Login code cloud
After the registration is complete, you can use the email you just registered to log in (address: https://gitee.com/login )
3.2.3 Create a remote warehouse
After successful login, you can create a remote warehouse. The operation method is as follows:
The page jumps to the new warehouse page:
explain:
Warehouse name: required, each warehouse needs to have a name, and the names of warehouses under the same code cloud account cannot be repeated
Path: It will be used when accessing the remote warehouse. Generally, there is no need to specify it manually, and it will be automatically consistent with the warehouse name
Open Source: Anyone can view this repository
Private: only visible to members of this repository, not to others
After the creation is complete, you can view the warehouse information:
Note : Each Git remote warehouse will correspond to a network address. Click the [Clone/Download] button, and click the [Copy] button in the pop-up window to copy the network address. The address is as follows:
https://gitee.com/ChuanZhiBoKe/myGitRepo.git
3.2.4 Invite other users to become warehouse members
I have created my own remote warehouse on Code Cloud, and currently the only member of the warehouse is myself (I am an administrator). In the actual development of an enterprise, a project is often jointly developed by multiple people. In order for multiple participants to have permission to operate the remote warehouse, it is necessary to invite other project participants to become members of the current warehouse.
Click the management button to enter the warehouse management page, and you can see [warehouse member management] in the left menu:
Click the [Developer] menu to jump to the following page:
Click the [Invite User] menu under the [Add Warehouse Member] menu to jump to the following page:
You can see that there are many ways to invite users: link invitation, direct addition, and invite members through the warehouse
Note : The invited user must be a registered user of Code Cloud, otherwise he cannot become a member of the warehouse
4. Git common commands
4.1 Git global settings
start a net name
The first thing to do when installing Git is to set up your username and email address. This is very important, because every Git commit will use this user information. Execute the following command on the Git command line:
Set user information
git config --global user.name “vsunks”
git config --global user.email “[email protected]”
View configuration information
git config --list
Note: The user.name and user.email set above are not the username and email address we used when registering the code cloud account, and can be set arbitrarily here.
4.2 Get the Git repository
To use Git to manage our code, we first need to obtain a Git repository.
There are usually two ways to obtain a Git repository:
- Initialize the Git repository locally (not commonly used)
- Clone from a remote repository (commonly used)
4.2.1 Initialize the Git warehouse locally
The operation steps are as follows :
- Create an empty directory (such as repo1) in any directory as our local Git repository
- Enter this directory, right-click to open the Git bash window
- Execute the command git init
If you see the .git folder in the current directory (this folder is a hidden folder), it means that the Git warehouse is created successfully
4.2.2 Cloning from a remote warehouse
You can clone from the remote warehouse through the commands provided by Git, and clone the remote warehouse to the local
Command format : git clone remote warehouse address
4.3 Workspace, temporary storage area, repository
In order to learn Git better, we need to understand some concepts related to Git, which will be mentioned frequently in later studies.
Repository : The .git hidden folder seen earlier is the repository, which stores a lot of configuration information, log information, and file version information, etc.
Workspace : The directory containing the .git folder is the workspace, also known as the working directory, which is mainly used to store the developed code
Temporary storage area : There are many files in the .git folder, and one of the index files is the temporary storage area, which can also be called stage. The staging area is a place to temporarily save modified files
4.4 Status of files in a Git workspace
Files in a Git workspace exist in two states:
-
untracked untracked (not included in version control)
-
tracked Tracked (put into version control)
1) Unmodified unmodified state
2) Modified Modified state
3) Staged has been temporarily stored
Note : The status of the file will change as we execute Git commands
4.5 Local Warehouse Operations
Common commands for local warehouses are as follows:
- git status View file status
- git add adds the modification of the file to the temporary storage area
- git reset will unstage the files in the temporary storage area or switch to the specified version
- git commit Submit the file modification in the temporary storage area to the repository
- git log view log
4.5.1 git status
The git status command is used to view the file status
Note: Due to the different status of files in the workspace, the output after executing the git status command will also be different
4.5.2 git add
The function of the git add command is to add the modification of the file to the temporary storage area , but not to the local warehouse. The command format: git add fileName
Execute the git status command after joining the temporary storage area, and you can find that the status of the file has changed.
4.5.3 git reset
The function of the git reset command is to unstage the files in the temporary storage area or switch to the specified version
Cancel temporary storage command format: git reset file name
Switch to the specified version command format: git reset --hard version number (use with caution)
This command will clear the existing content in the temporary storage area and work area, and switch to the specified version. If you have modified the code, but have not submitted it (stored in the local warehouse), use this command, and your unsubmitted content will be lost! ! !
Be sure to use it with caution. If the fee is not valid, commit
submit it first and then use this command. Avoid code loss! !
Note: Every Git submission will generate a new version number, and you can go back to the historical version through the version number
4.5.4 git commit
The function of the git commit command is to submit the modification of the file in the temporary storage area to the version library. The command format is: git commit -m msg file name
explain:
-m: stands for message, which needs to be set every time you submit, and will be recorded in the log
Wildcards * can be used to submit multiple files at once
4.5.5 git log
The role of the git log command is to view the commit log
git log --oneline
Display the version log on one line, showing brief information.
Check the log through the git log command, you can find that each submission will generate a version number, and the message, submitter, email, submission time and other information set at the time of submission will be recorded in the log
4.6 Remote Warehouse Operation
The previous command operations are all for the local warehouse. In this section, we will learn some operations about the remote warehouse, including:
- git remote View remote warehouse
- git remote add add remote warehouse
- git clone clones from a remote warehouse
- git pull pulls from a remote warehouse
- git push Push to remote warehouse
4.6.1 git remote
If you want to view the configured remote warehouse servers, you can execute the git remote command, which will list the abbreviation of each remote server.
If you've cloned the remote repository, you should at least see origin , which is the default name of the repository server Git clones from.
explain:
You can view more detailed information about the remote warehouse through the -v parameter
The remote warehouse configured in the local warehouse needs an abbreviation, which will be used later when interacting with the remote warehouse
4.6.2 git remote add
Add remote warehouse command format: git remote add referred to as remote warehouse address
Note: A local warehouse can be associated with multiple remote warehouses
4.6.3 git clone
If you want to get a copy of an existing Git remote repository, then use the git clone command. Git clones almost all data (including log information, history, etc.) on the Git warehouse server.
Command format for cloning warehouse: git clone remote warehouse address
4.6.4 git push
Push the contents of the local warehouse to the remote warehouse, command format: git push remote warehouse abbreviated as branch name
When using the git push command to push local files to the code cloud remote warehouse, if it is the first operation, identity authentication is required, and the push can only be done after the authentication is passed, as follows:
Note: The user name and password above correspond to the user name and password we registered on Code Cloud. After the authentication is passed, the user name and password will be saved in the windows system (as shown in the figure below), and there is no need to repeatedly enter the user name and password for subsequent pushes.
After the push is complete, you can go to the remote warehouse to view the file changes.
explain:
A warehouse can have multiple branches. By default, a master branch is automatically created after the warehouse is created.
Branch-related operations will be explained later
4.6.5 git pull
The function of the git pull command is to obtain the latest version from the remote warehouse and merge it into the local warehouse
Command format: git pull remote warehouse abbreviated branch name
Note : If the current local warehouse is not cloned from the remote warehouse, but a locally created warehouse, and there are files in the warehouse, an error will be reported when pulling files from the remote warehouse (fatal: refusing to merge unrelated histories )
To solve this problem, you can add the parameter --allow-unrelated-histories after the git pull command
4.7 Branch operation
Branching is a very important concept in the use of Git. In actual combat, new requirements will be developed in an independent branch. After the development and testing pass, it will be merged into the main branch for release.
There are branches in both the local warehouse and the remote warehouse. The same warehouse can have multiple branches, and each branch is independent of each other and does not interfere with each other.
When creating a local warehouse through the git init command, a master branch is created by default.
In this section, we will learn related commands about branches. The specific commands are as follows:
-
git branch View local branches
- git branch -r View remote warehouse branches
- git branch -a View all remote and local branches
- git branch -vv View the relationship between remote warehouse branches and local branches
-
git branch [branchName] Create a new branch based on the current branch code
-
git checkout [branchName] switch branch
-
git push [repoShortName] [branchName] Push to the remote warehouse branch
-
git merge [brandName] merges the specified branch into the current branch
4.7.1 View branch
View branch command: git branch
git branch lists all local branches
git branch -r lists all remote branches
git branch -a lists all local and remote branches
4.7.2 Create a branch
Create branch command format: git branch branch name
4.7.3 Switch branches
There can be multiple branches in a warehouse, switch branch command format: git checkout branch name
Note: The current branch will be displayed on the command line, as shown in the figure above.
4.7.4 Push to remote warehouse branch
Push to remote warehouse branch command format: git push remote warehouse referred to as branch command
After the push is complete, you can view the remote warehouse:
4.7.5 Merging branches
Merging branches is to merge the files of two branches, command format: git merge branch command
Note: When merging branches, pay attention to the direction of merging. As shown in the figure above, if you execute the operation on the Master branch, the result is to merge the b3 branch into the Master branch.
4.8 Label operation
A label in Git refers to the state of a branch at a specific point in time. Through the label, it is very convenient to switch to the state at the time of marking.
Typically people will use this feature to tag release nodes (v1.0, v1.2, etc.). The following is the label of mybatis-plus:
In this section, we will learn the following commands related to tags:
- git tag view tags
- git tag [name] creates a tag
- git push [shortName] [name] will push the label to the remote warehouse
- git checkout -b [branch] [name] checkout tag
4.8.1 View tags
View tag command: git tag
4.8.2 Create label
Create tag command: git tag tag name
4.8.3 Push tags to remote warehouse
Push the label to the remote warehouse command: git push remote warehouse abbreviated label name
After the push is complete, you can view the tags in the remote warehouse.
4.8.4 Detect tags
When checking out a label, you need to create a new branch to point to a certain label. The command format for checking out a label is: git checkout -b branch name label name
5. Using Git in IDEA
Git-related operations can be completed through Git commands. In order to simplify the operation process, we can configure Git in IEDA. After configuration, we can operate Git graphically in IDEA.
5.1 Configure Git in IDEA
Using Git in IDEA is essentially using locally installed Git software, so you need to install Git in advance and configure Git in IDEA.
Git installation directory:
explain:
git.exe: the executable file in the Git installation directory, the git command executed earlier is actually the executed file
Configuration in IDEA:
Note: If Git is installed in the default directory (C:\Program Files\Git), IDEA can be used directly without manual configuration.
Configure git
the shortcut button
The effect is as follows:
5.2 Get the Git repository
There are two ways to get the Git repository in IDEA:
- The essence of locally initializing the warehouse is to execute the git init command
- The essence of cloning from a remote warehouse is to execute the git clone command
5.2.1 Initialize warehouse locally
In IDEA, a local warehouse can be initialized locally through the following operations. In fact, the bottom layer is the executed git init command. The operation process is as follows:
1) Select the menu【VCS】-【Import into Version Control/check out from Version Control】-【Create Git Repository】
2) In the pop-up [Create Git Repository] dialog box, select the current project root directory, and click the [OK] button:
After the operation is complete, you can see that the .git hidden directory appears in the root directory of the current project:
After the operation is completed, you can see the related operation icons of Git in the toolbar of IDEA:
5.2.2 Cloning from a remote repository
The essence of cloning from a remote warehouse in IDEA is to execute the git clone command. The specific operation process is as follows:
1) Click [Get from Version Control] in the IDEA start window
2) In the pop-up [Get from Version Control] window, enter the URL address of the remote warehouse and the corresponding local warehouse storage directory, and click the [Clone] button to clone the warehouse
5.3 Git ignores files
There is a special file .gitignore in the Git workspace, through which you can specify which files in the workspace do not need Git management. When we create a Git remote warehouse on Code Cloud, we can specify to generate this file, as follows:
After the creation is complete, the effect is as follows:
explain:
1) When we use Git to manage project code, not all files need to be managed by Git, such as .class files compiled in Java projects, configuration files that come with development tools, etc. There is no need to commit to the Git repository
2) Note that the name of the ignore file is fixed and cannot be modified
3) Files added to the ignore list will be ignored by subsequent Git tools
A reference .gitignore file reads as follows:
.git
logs
rebel.xml
target/
!.mvn/wrapper/maven-wrapper.jar
log.path_IS_UNDEFINED
.DS_Store
offline_user.md
*.class
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
5.4 Local Warehouse Operations
Local warehouse operations:
- The essence of adding files to the temporary storage area is to execute the git add command
- The essence of submitting the files in the temporary storage area to the repository is to execute the git commit command
- View the log, the essence is to execute the git log command
5.4.1 Add files to the temporary storage area
After adding a new file or modifying an existing file in the Git workspace, you need to add the file modification to the temporary storage area. The specific operation is as follows:
5.4.2 Submit the files in the temporary storage area to the repository
Submit the file in the temporary storage area to the repository, you can select a file to submit, or you can select the entire project to submit multiple files. The operation of file submission is simplified in IEDA, that is, if the file is modified, it can be submitted directly without adding to the temporary storage area.
1) Submit a file:
As you can see, if you select a file to submit, the menu name is [Commit File…]
2) Submit multiple files:
It can be seen that if multiple files are submitted, the menu name is [Commit Directory...]
Since the submission operation is a high-frequency operation, in order to further facilitate the operation, a shortcut button for the submission operation is provided in the toolbar of IDEA:
5.4.3 View logs
View the log, you can view the commit log of the entire warehouse, or view the commit log of a file.
1) View the commit log of the entire project:
2) View the commit log of a file
5.5 Remote Warehouse Operation
Remote warehouse operations:
- The essence of viewing a remote warehouse is to execute the git remote command
- Adding a remote warehouse is essentially executing the git remote add command
- The essence of pushing to a remote warehouse is to execute the git push command
- Pulling from a remote warehouse is essentially executing the git pull command
5.5.1 View remote warehouse
The operation process is as follows:
In the pop-up [Git Remotes] window, you can see the configured remote warehouse:
5.5.2 Add remote warehouse
A local warehouse can be configured with multiple remote warehouses, click [+] in the [Git Remotes] window to add a new remote warehouse:
5.5.3 Push to remote warehouse
The local warehouse file can be pushed to the remote warehouse by the following operations:
In the pop-up [Push Commits] window, you can see the files pushed this time, click the [Push] button to push to the remote warehouse:
Since the operation of pushing to the remote warehouse is a high-frequency operation, the submission and push can be completed at the same time through the submit shortcut button in the IDEA toolbar:
Click the [Commit and Push…] button to complete the commit and push operations at the same time
5.5.4 Pull from remote warehouse
You can pull from a remote warehouse by doing the following:
Since pulling files from remote warehouses is a high-frequency operation, corresponding shortcut buttons are provided in IDEA's toolbar:
Click [OK] in the pop-up [Update Project] window:
5.6 Branch operation
Branch operation:
- The essence of viewing a branch is to execute the git branch command
- To create a branch, the essence is to execute the git branch branch name command
- The essence of switching branches is to execute the git checkout command
- Pushing the branch to the remote warehouse is essentially executing the git push command
- The essence of merging branches is to execute the git merge command
5.6.1 View branch
You can view branches by doing the following:
In the pop-up window, you can see the local branch and remote branch:
Since branch operations are high-frequency operations, shortcut buttons for branch operations are provided in IDEA's status bar:
Click the [master] shortcut button to pop up the [Git Branches] branch window:
5.6.2 Create a branch
Click [New Branch] in the [Git Branches] branch window, and the following window will pop up:
Enter the name of the new branch in the pop-up [Create New Branch] window, and click the [Create] button to complete the branch creation
5.6.3 Switch branches
You can switch branches by doing the following:
5.6.4 Push the branch to the remote warehouse
Branches can be pushed to remote warehouses by doing the following:
5.6.5 Merging branches
Branches can be merged by doing the following:
Is to execute the git branch branch name command
- The essence of switching branches is to execute the git checkout command
- Pushing the branch to the remote warehouse is essentially executing the git push command
- The essence of merging branches is to execute the git merge command
5.6.1 View branch
You can view branches by doing the following:
In the pop-up window, you can see the local branch and remote branch:
Since branch operations are high-frequency operations, shortcut buttons for branch operations are provided in IDEA's status bar:
Click the [master] shortcut button to pop up the [Git Branches] branch window:
5.6.2 Create a branch
Click [New Branch] in the [Git Branches] branch window, and the following window will pop up:
Enter the name of the new branch in the pop-up [Create New Branch] window, and click the [Create] button to complete the branch creation
5.6.3 Switch branches
You can switch branches by doing the following:
5.6.4 Push the branch to the remote warehouse
Branches can be pushed to remote warehouses by doing the following:
5.6.5 Merging branches
Branches can be merged by doing the following: