Article directory
Do you also want a piece of green? let's look down
1. What is 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
- commit: Submit, save the local code and version information to the 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
Two, Git download and installation
Download address: https://git-scm.com/download
After the download is complete, you will get the installation file:
click, all the way to next, how to verify whether your installation is successful? Click the right mouse button in any directory, if you can see the following menu, it means the installation is complete:
- Git GUI Here: Open the Git GUI
- Git Bash Here: Open the Git command line
3. Git code hosting service
Two types of repositories exist in Git, namelylocal warehouseandremote warehouse, How to build a Git remote warehouse?
The more commonly used ones are GitHub, Code Cloud, GitLab, etc. We choose Gitee here, because the server is in China, which is faster than Github
Use code cloud code hosting service
1. Registration code cloud account
registration website: https://gitee.com/signup
2. Login code cloud
3. Create a remote warehouse
Fill in some basic information of the warehouse:
After the creation is complete, you can view some information of the warehouse:
Note: each Git The remote warehouse will correspond to a network address, click [Clone/Download], and click the [Copy] button in the pop-up window to obtain the network address
4. Invite other users to become members of the warehouse
. You have created your own remote warehouse on Code Cloud. Currently, you are the only member of the warehouse (as an administrator). In the actual development of an enterprise, a project is often jointly developed by multiple people. In order to allow 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
Four, Git common commands
Git global settings
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:
// 设置用户信息
git config --global user.name “itcast”
git config --global user.email “[email protected]”
// 查看配置信息
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.
Get the Git repository
To use Git to version control our code, we first need to obtain a Git repository.
There are usually two ways to obtain a Git repository:
- Initialize a Git repository locally (not commonly used)
- Clone from a remote repository (commonly used)
The execution steps are as follows:
1. Create an empty directory in any directory as our local Git warehouse
2. Enter this directory to open the Git Bash window
3. Execute the git init command
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
You can clone from the remote warehouse through the commands provided by Git, and clone the remote warehouse to the local
// 克隆仓库
git clone
Workspace, staging area, repository
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
Git workspace file status
There are two types of states in Git workspace files:
1. untracked (not included in version control)
2. tracked (included in version control)
- Unmodified Unmodified state
- Modified Modified status
- Staged staged state
Note: The status of these files will change as we execute Git commands
Local Warehouse Operations
Common commands for local warehouses are as follows:
git status // 查看文件状态
git add // 将文件修改加入暂存区
git reset // 将暂存区文件取消暂存或提交到指定版本
git commit // 将暂存区文件提交到版本库
git long // 查看日志
git status:
git add:
git reset:
git commit:
git commit -m "提交信息"
git log:
Here we can see that Author contains the global information username and mail we just configured
There is a commit followed by a string of version information in the log, and we reset the specified version based on this information
git reset --hard
We can find that user.xml is missing, because there was no user.xml in that version at that time
Remote Warehouse Operations
The previous command operations are all for the local warehouse, now let’s learn some operations of the remote warehouse
git remote // 查看远程仓库
git remote add // 添加远程仓库
git clone // 从远程仓库克隆
git pull // 从远程仓库拉取
git push // 推送到远程仓库
View remote warehouse:
If you want to view the configured remote warehouse server, you can run the git remote command, which will list the abbreviation of each remote server.
Because the warehouse is manually git init, not cloned from the remote warehouse, we found that the local warehouse has no remote warehouse associated with it
Add a remote warehouse:
Add a remote warehouse, run git remote add <shortname> <url> to add a new remote Git warehouse, and specify a shorthand that can be referenced
Clone the remote warehouse to the local:
If you want to get a copy of the existing Git remote warehouse, then use the git clone command. Git clones almost all data (including log information, history, etc.) on the Git warehouse server, not just the files needed for copying work.
The command format for cloning a warehouse is: git clone [url]
Push to remote warehouse:
To push the content of the local warehouse to the remote warehouse, you can use the command: git push [remote-name] [branch-name]
When using the git push command to push local files to the code cloud remote warehouse, authentication is required , the authentication can be pushed, as follows:
Pull from the remote warehouse:
The function of the git pull command is to obtain the latest version from the remote warehouse and merge it into the local warehouse. The command format is: git pull [short-name] [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. At this time, when pulling files from the remote warehouse, an error will be reported (fatal: refusing to merge unrelated histories)
To solve this problem, you can add the parameter --allow-unrelated-histories after the git pull command
branch operation
Branching is a very important concept in the use of Git. Using branches means that you can separate your work from the main line of development so that it does not affect the main line of development.
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.
git branch // 查看分支
git branch [name] // 创建分支
git checkout [name] // 切换分支
git push [shortName] [name] // 推送至远程仓库分支
git merge [name] // 合并分支
View branch:
- git branch lists all local branches
- git branch -r lists all remote branches
- git branch -a lists all local and remote branches
Create branch:
create branch command format: git branch [name]
switch branch:
switch branch command format: git checkout [name]
push to remote warehouse branch:
push to remote warehouse branch command format: git push [shortName] [name]
merge branch :
Merge branches Command format: git merge [name]
merge conflict
In Git, a merge conflict means that when performing operations such as git merge or git pull, if it is found that the same part of the code of the same file has been modified at the same time, Git cannot automatically decide which modification should be kept, resulting in the failure of the merge. Resolving merge conflicts requires manually editing the file, selecting which changes to keep, and committing the version that resolves the conflict.
Here are the general steps to resolve merge conflicts:
1. Use the git status command to see which files have merge conflicts.
2. Open the file containing the conflict, you can see a mark similar to the following:
3. Manually edit the file to resolve conflicts as needed. You can choose to keep the code of the current branch, merge the code of the branch, or make custom modifications according to specific needs.
4. After resolving conflicts, save the file and remove the conflict markers in the file.
5. Use the git add command to mark resolved conflict files as staged.
6. Finally, execute the git commit command to submit the version that resolves the conflict. Usually, Git will automatically generate a merge commit message, which you can edit as needed.
7. If the conflict is resolved during the pull operation, you may also need to execute the git pull command to confirm that the merge has been successfully resolved.
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:
git tag 列出已有的标签
git tag [name] 创建标签
git push [shortName] [name] 将标签推送至远程仓库
git checkout -b [branch] [name] 检出标签
List existing tags:
list existing tags can use the command: git tag
create tags:
create tags can use the command: git tag [name]
push tags to remote warehouses:
command format for pushing tags to remote warehouses: git push [shortName] [name]
Checkout label:
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: git checkout -b [branch] [name]