Original address: https://blog.dpf114.top/posts/git-often-used/
1. What version control system
Separately speaking about version control, we may not know what it is, but for a small example in our lives, we can easily understand what it is.
For example, we have revised the thesis countless times after graduation:
毕业论文最终版
毕业论文最最终版
毕业论文最最最终版
毕业论文最最最最终版
毕业论文最终不改版
毕业论文最终真不改版
毕业论文最终真真不改版
毕业论文最终打死不改版
毕业论文最终打死不改版 2
...
There are many versions, and the version control system is the system that controls the different versions of the content we write.
Commonly used version control systems are SVN
and Git
. We use Git a lot in actual development.
2. Installation and configuration
2.1. Installation
Windows
For windows, just download the installation package and install it directly.
Linux
Use apt-get tool to download and install
apt-get update
sudo apt-get install git
Mac
Use brew tool to download and install
brew install git
2.2. Configuration after installation
Regardless of whether it is a Windows installation or a Linux installation, after the installation is complete, we will first make a basic configuration through the following two lines of commands. The purpose is to distinguish the identities of different developers, that is, who submitted each submission. The configuration method is as follows:
git config --global user.name 'username'
git config --global user.email '[email protected]'
After the configuration is completed, the configuration information is stored in the ~/.gitconfig
file
3. Basic use
Before the basic operation, we need to understand the partitions used by Git
- Work area: Work area is where we usually write code
- Temporary storage area:
git add
a place to temporarily store code after submission - Local library: through
git commit
the place where the code is stored after submission, the local library saves historical version related information. - Remote libraries: remote libraries are also remote code repositories, such as Gitee, GitHub, and gitlab.
3.1. Initialize the warehouse
There are two ways to initialize the warehouse, one is to clone from a remote warehouse, and the other is to initialize directly from the current directory. The command is as follows:
git init
After the completion of the implementation will be more of a current directory .git
hidden folder, all git
the required data and resources are stored in the directory.
3.2. View warehouse status
git status
You can view the status of the warehouse through commands. The newly initialized warehouse view status is as follows:
After modifying the workspace, check the status as shown in the figure below:
After submitting the temporary storage area, check the status as shown in the figure below:
After submitting to the local warehouse, check the status as shown below:
3.3. Submit to the staging area
Through git add
the content of the work area can be submitted to the temporary storage area, there are the following use cases:
git add 具体文件
git add .
git add *
.
*
Both and both represent all files submitted to the workspace.
3.4. Submit to the local library
By git commit
submitting the contents of the temporary storage area to the local warehouse, the specific usage is as follows:
git commit -m '提交描述内容'
Note: The description content cannot be empty. It is recommended to write clearly the content of each submission, which can be used for version rollback.
3.5. Submit to remote warehouse
If the warehouse is initialized by cloning, after submitting to the local warehouse, you can directly git push
submit to the remote warehouse directly. If it is through git init
initialization, you need to create the corresponding warehouse remotely, and associate the local warehouse with the remote warehouse and use it later. git push
submit.
Associate remote warehouse
Use git remote
commands to associate remote warehouses, the format is git remote add 别名 远程仓库url
as follows
git remote add origin https://github.com/xiaoxiaoshou/testgit.git
Url where you may both types, respectively, https://github.com/username/reponame.git
and [email protected]:username/reponame.github.io.git
, git represent two different connections.
Other related remote warehouse related commands:
# 查看关联了哪些远程分支
git remote -v
# 删除关联分支
git remote remove 别名
Submit to remote warehouse
The format of the submission command is git push 别名 分支名
as follows:
git push origin master
3.6. Pull code from remote
There are two commonly used methods:
1. Use git clone
Pull from the remote master branch by default:
git clone 远程url地址
Designated branch pull:
git clone -b 分支名称 远程url地址
Use git pull
Use format git pull <远程主机名> <远程分支名>:<本地分支名>
.
For example, we want to pull the master branch of the remote origin and merge it with the local master branch
git pull origin master:master
We can also simplify to
git pull origin master
git pull origin
git pull
git pull = git fetch+git merge
, The role of fetch is to pull code remotely, and the role of merge is to merge code (the branch management will be analyzed in detail later)
4. Code revocation and version rollback
4.1. View submission records
Use git log
to view the submitted information, each submission ( git commit
) to the local warehouse detailed information, each submission information is roughly as follows:
commit cdc2ac8c3b6d9f8bde05140aca484aa4482f8895
Author: xiaoxiaoshou <[email protected]>
Date: Tue Dec 1 15:41:13 2020 +0800
提交描述内容
The number after commit is an index for each submission (we can call it the version number), which is used to advance or rewind the version
Use git log
the content displayed too much, we generally use the following two relatively simple way to view:
# 推荐使用
git reflog
git log --online
4.2. Work area code cancellation
May one day you're writing code, writing for a long time found a mistake, and would like to return to its initial state, delete a stupid way is to just write the code line by line, but this way is too costly, you can git checkout -- <file>
command Undo the code modification in the workspace. As shown below:
4.3. Undo code added to the staging area
Want to cancel the code submitted to the staging area in the following two steps:
-
1. Undo the temporary storage area code to the work area
git reset HEAD
-
2. Cancel the work area code (the same as the work area code revocation)
git checkout -- file
4.4. Submit to the local warehouse code version rollback
Rollback based on index version (recommended)
1. View submission history
2. Use git reset --hard [局部索引值]
back (forward and back)
Use the ^ symbol
git reset --hard^
One
^
step back
Use the ~ symbol
git reset --hard~n
n represents the number of steps back
Comparison of three parameters in git reset
git reset --soft
git reset --mixed
git reset --hard
- –soft
- Only move the HEAD pointer in the native library
- –mixed
- Move the HEAD pointer in the local warehouse
- Reset staging area
- –hard
- Move the HEAD pointer in the local library
- Reset staging area
- Reset workspace
4.5. Submit to the remote warehouse version rollback
Since the contents of the remote warehouse are consistent with the local warehouse, the remote warehouse version is required to be rolled back, and only the local version is rolled back and then submitted to the remote warehouse.
5. Branch management
5.1. What is a branch
When we complete a project, it is impossible to develop it in a "single-threaded" way. Many times the tasks are parallel. Let me give you a chestnut: The 2.0 version of the project is online, and now we are going to develop version 3.0. At the same time, there may be some bugs in version 2.0 that need to be fixed. After these bugs are fixed, we may release 2.1, 2.2, and 2.3 versions. After all the bugs are repaired, we will develop version 3.0. Fixing the bugs of 2.0 and developing new features of 3.0 are two parallel tasks. At this time, the development of our 3.0 functions directly on the master branch is definitely not appropriate. We must ensure that there is a stable , The branch that can be released at any time exists (in general, this role is played by the master branch), at this time we can flexibly use the branch management function in Git:
- Create a long-term branch to develop 3.0 features. Assuming the name of this branch is v3, we add new features to v3 and continue to test it. When v3 is stable, we merge v3 into the master branch.
- Create a feature branch to fix the 2.0 bug. Once the bug is successfully fixed, the branch will be merged into the master. Once a new bug is found, the branch will be created immediately to fix it, and then merged after the repair is successful.
5.2. View branch
We can use the command git branch
to see which branches of the current warehouse and the branch we are currently in
*
The branch in front is our current branch.
5.3. Create and switch branches
the first method
- First use to
git branch 分支名
create a branch - Then use
git checkout 分支名
switch branch
The second method
Use to git checkout -b 分支名
create and switch branches in one step.
5.4. Integration branch
There are two forms of branch integration, one is merge
, the other isrebase
merge
Use commands to git merge [有新内容的分支名]
merge branches.
The above figure explains: the main branch found a bug at m1, so cut out a branch bugFix, after a period of time, the main branch was developed to m3, the bugFix branch (submitted as m2), the bug has been resolved, and the bugFix branch needs to be merged to The main branch is to use the command git merge bugFix
.
rebase
Rebase is also called rebase, and you git rebase [有新内容的分支]
can use commands to rebase .
Analysis of the above figure: The above figure is to use commands git rebase bugFix
to integrate branches in the main branch . The integration process is to record the submission of bugFix in the main branch and submit it again from bottom to top.
The difference between merge and rebase
-
Merge is to merge the current branch and the branch to be merged together, and make a commit submission
-
Rebase is to resubmit the historical submission of the branch to be rebase in the current branch in order, and the position of HEAD remains unchanged after rebase (that is, * in the figure)
-
Conflicts may occur during the rebase process, continue to submit after the conflict is resolved
- Conflict resolution
- Commit to buffer
git add .
- Continue to complete the merger
git rebase --continue
The following is part of the conflict
<<<<<<< HEAD 需要合并分支的内容 ======= 当前分支内容 >>>>>>>
6. Label management
We can tag a certain submission to show that it is important. More representative tags use it to release the corresponding application version such as v1.0, v2.0. Git supports two types of tags: lightweight and annotated.
We can use the following command to view all tags in the warehouse:
git tag
6.1. Lightweight tags
A lightweight tag is a lot like a branch that doesn't change-it's just a reference to a specific commit. The lightweight tag essentially stores the submitted checksum in a file—no other information is saved.
Lightweight labeling format git tag 标签名称
, for example:
git tag v0.0
Lightweight tags default to the latest ones commit
.
We can also commit
label and format the designation git tag 标签名称 commi对应索引
. Examples are as follows
# 查看commit索引
git reflog
# 打标签
git tag v0.01 bcde256
We can also delete tags and formats on the local warehouse git tag -d 标签名称
, for example:
git tag -d v0.01
6.2. Note label
A note label is a complete object stored in the warehouse. It has its own checksum information, which contains the labeler’s name, email, date and time, and a label information. It can be GNU Privacy Guard (GPG)
signed and verified.
The format of the note label is git tag -a 标签名称 -m '标签信息'
as follows:
git tag -a v1.0 -m 'my version 1.0'
6.3. Push tags to remote warehouse
By default, the git push
command does not send tags to the remote warehouse. We need to manually push the label to the remote warehouse, the push format git push 远程分支别名 标签名
, for example:
git push origin v1.0
Reference materials:
https://git-scm.com/book/zh/v2
https://www.runoob.com/git/git-tutorial.html
https://segmentfault.com/a/1190000037465780
Learn while playing games: https://learngitbranching.js.org/?locale=zh_CN