Article directory
0 Preliminary Tutorials
1 Introduction
Regarding Git, since I really have no special needs, I have never used this very powerful tool. But as the frequency of using GitHub increases, my interest in git becomes stronger, so I want to combine the GitHub website and VS Code, a commonly used editor, to summarize a git usage tutorial.
The instruction practice part refers to a practical website in the previous tutorial.
2 Basic use
2.1 configuration
Before using it for the first time, it generally needs to be initialized first.
- Set basic configuration
git config --global user.name "Zoey"
git config --global user.email 123456@qq.com
Note that the username here should be based on the URL of the GitHub website, and the email address should be the registered email address.
- View all configurations
git config --list/-l #查看所有设置项
git config --list --show-origin #查看所有设置项及其对应的文件
git config usr.name #查看某个设置
- delete a configuration
git config --global --unset user.name
2.2 Get help
When we don't know how to use a command, the best way is to get the corresponding parameters through the built-in help command.
- Get comprehensive help documentation and manuals
git help <verb> #<verb>为需要查询的指令
- Get brief help information
git <verb> -h #获取某个指令的帮助信息
3 GitHub repository and git
Git is essentially a version management system, and GitHub is equivalent to a shared network disk, which is used to store code uploaded by developers. Therefore, using git generally has to be used in conjunction with GitHub.
3.1 Create a new GitHub repository
Log in to your GitHub account, click New in repositories, you can create a new warehouse, and then set some parameters, which is very simple.
The newly created project is generally empty, and there will be at most one readme file, and the key point is to look at the prompts it gives, as shown in the figure below.
3.2 Delete a warehouse (repository)
Found on the warehouse homepage Settings
:
Then scroll all the way down to find Danger Zone
:
to delete a repository.
3.3 Upload project code
Generally speaking, uploading the project code to a warehouse established by oneself requires the following steps:
# 1 在项目文件夹中初始化git,生成一个.git文件夹
git init
# 2 关联远程仓库
git remote add origin <link.git> #link即为仓库的网址
# 3 添加被追踪的文件 / 删除被追踪的文件
git add
git rm
# 4 提交到本地仓库
git commit -m "<message>"
# 5 远程推送
git push origin <branch_name>
There seems to be a problem here? That is, we only set up the username and mailbox, and it seems that the password has not been set? Regarding this, you will need to log in when you associate with the remote warehouse, so that this device is trusted. This process only needs to be performed once.
4 git common commands
4.1 Create a branch
git checkout <branch> #切换分支
git checkout -b <branch> #创建分支同时切换到那个分支上
# 新版git也可以使用
git switch
4.2 Merging branches
If we have two branches, and each has an independent submission, for example, two people develop different functions at the same time, and then modify some codes, so that different modifications can be integrated together by merging branches.
git merge bugFix
The effect achieved is as follows
Another operation for merging branchesrebase
git rebase main
That is, a merged branch is generated after the main branch
The original C3 branch still exists, but
this makes the submission record more linear, and then you can switch to the main branch and execute it again git rebase bugFix
, the result is as follows
4.3 Move on git commit record
It is possible to move using a hash, but that would be inconvenient, so relative references are often used.
Use ^
to move up 1 commit record
Use ~<num>
to move up multiple commit records, as in~3
git checkout main^
HEAD moves to the previous parent node.
At the same time, HEAD can also be used as a relative reference,
git checkout HEAD^
, that is, HEAD automatically moves to the parent node of the next level.
Detached HEAD state
git checkout HEAD~4
HEAD moves up four times
git branch -f main HEAD~3
Forcibly switch the main branch to a certain location
4.4 Undo changes
git reset HEAD~1
The changes made by the original C2 are still there, but they are not added to the temporary storage area.
Another way to undo changes: git revert
git reset
it is very simple for local warehouse operations, but it is invalid for remote warehouse branches. If you want to undo changes and share them with other collaborators, you can use the git revert
command
git revert HEAD #撤销上一次的提交
This is essentially another submission, and the changes it submits are actually a submission that restores the original changes, thus achieving remote synchronization.
4.5 Organize submission records
git cherry-pick
After execution git cherry-pick C2 C4
, it becomes the following state:
Note that only the hash value can be used here, that is, the corresponding C1 C2 in the circle
In addition, you can also use interactive rebase to tidy up the commit record, the code is
git rebase -i HEAD~4 #整理当前分支的前4个提交记录
Executing this command will pop up a UI interface, which can be manually adjusted by dragging with the mouse.
5 Using git on VS Code
5.1 Add to the staging area
5.2 When committing, add a message in the input box
5.3 Remote push (synchronization)
6 Common error messages
6.1 git Recv failure: Connection was reset
Remove the old origin: git remote remove origin
re-create a new origin: git remote add origin http://github.com/×××
re-push
6.2 Error in the HTTP2 framing layer
Turn off the VPN and reset the https proxy at the same time.
git config --global --unset http.proxy
git config --global --unset https.proxy