[A brief summary of common knowledge about Git tools "Easy to Understand"]

Basic knowledge of Git

  1. Basic concepts of GIT: free and open source distributed version control system. It is an open source version control software developed by Linus Torvalds to help manage Linux kernel development.

  2. As a beginner, you only need to know the following two things.

    - The essence of Git: It is a tool. You can simply understand it as a housekeeper. His job is to help you take care of various things in your house.

    - The purpose of learning Git: We are to learn the instructions to the housekeeper. Only if your instructions are clear and the housekeeper understands them, will they be able to take care of your mansion in an orderly manner.

Customize your own butler (GIT)

Background: You came to the capital with great ambitions, and the prosperous scene in front of you opened your eyes. Fortunately, you are from a noble family and can still take such a big scene indifferently. When you first arrived, what is your top priority? To obtain fame? Going into business? No, no, no, our young master from the rich family has been riding hard all the way to the capital. No matter what he does next, he must enjoy it first. Looking at the carriage filled with luggage behind him, you fell into deep thought. The most urgent thing is to find a local to help you get familiar with it. Familiarize yourself with the environment. Then you follow the crowd and arrive at a magnificent mansion. An old butler next to the red door is telling people about the apprentices he brought out. You feel happy and think to yourself: It's just what I want.

1. The download of Git is also very simple. The Git link is also provided here, so please feel free to use it.
Git’s official website

2. After git is installed successfully, you only need to enter git -v on the terminal command line and the corresponding version number will be output.
window (win+R -> cmd -> git -v)
mac (command+space-> Terminal -> git -v)< /span>

3. First you need to customize your own housekeeper (only needs to be executed once)

//git初始化设置
git config --global user.name "Your Name" //配置您的名字

git config --global user.email  "[email protected]"//配置您的邮箱

git config --global credential store //(选配)配置启用凭证存储,第一次从远程存储库中拉取或推送时,系统会询问您用户名和密码。

Parameter description
Omitted (Local): local configuration, only valid for local warehouses
–global: global configuration, valid for all warehouses♥
–system: system configuration, effective for all users

Configuration completed, view git configuration information

git config --global --list // 查看配置信息

Customize your residence (Repository)

Background: After some operations, your exclusive butler has been put in place. At this moment, the two of you, master and servant, are looking at each other in confusion on the streets of the capital. Your butler asks for instructions on which mansion you need him to help you take care of. You just realized that you didn’t have a mansion when you first arrived! ! ! Good guy, this is not a joke. Your butler seems to have noticed your problem. He scratched his head and offered you two plans for your reference.

1. Find a suitable place and build a mansion on the spot.

git init <project-name>  //创建一个新的本地仓库 ( 省略project-name则在当前目录创建。 )

2. Find a middleman to buy a house of your choice.

git clone <url> //克隆一个远程仓库,<url>代表远程仓库SSH链接

The great future of GIT steward (four regions and four states)

Background: After listening to the advice given to you by your dedicated butler, you are no longer at a loss. You are thinking that you have to build a house by yourself all the way, which is time-consuming and labor-intensive. With a huge wealth, you choose the latter without hesitation. The middleman takes a look. Big business comes to your door and hurriedly confirms your needs. After a while, according to your needs, the intermediary recommends a dream mansion that meets your standards. With a wave of your hand, this famous mansion is already in your name. . You take the housekeeper and walk into this large mansion. The facade is very grand. When you step into the mansion, you will feel the breeze blowing on your face, flowers blooming, and you can see pavilions, jade pavilions, strange mountains and rocks in the distance. Looking closely at the small bridge and flowing water, the stream is gurgling and the birds are singing and the flowers are fragrant. It's like a paradise. Immediately, you officially move into the mansion in Beijing and leave everything in your home to the butler. The dedicated butler lives up to expectations and has your mansion in order within a few days. It is divided into four major areas and three states according to your needs. …

Four areas for file storage

1.Working Directory: Local working directory: the directory where we operate.
2.Staging Area/Index: : Intermediate area, used to temporarily store items to be submitted to Git The modification content of the warehouse.
3.Local Repository: The repository created by the git init command contains the entire project history and Metadata, the main location where git stores code and version information.
4.Remote Repository: : A warehouse hosted on a remote server. Commonly used ones include GitHub, GitLab, and Gitee.

Four statuses of files (git status)

1.Untracked (Untrack): The status of newly created files that have not been managed by git.
2.Unmodified (Unmodified): Files that have been managed by git and have not been modified.
3.Modified: The file has been modified, but has not been added to the temporary storage area. .
4.Staged (Staged) : The status after the modification is completed.

A picture helps you clearly understand the relationship between various states.
Insert image description here

Some special files that come with Git after creating Git (simply understand it as the skills that come with the housekeeper, just understand it)
Insert image description here

Common command series of Git Butler

Background: Time flies and time flies. Of course you are not satisfied with the status quo because of your extraordinary talent. Under your management, you opened a ticket trading (bank) and set up your own branches in all nine states across the country. You yourself sit in the center of stability in the capital. Your financial empire is like a big tree rooted in the sky. Each semicolon is like a branch on the big tree. It completes the tasks assigned by you in its own semicolon. In order to facilitate you to manage this The huge financial empire will first transport the deposit and receipt account books of various semicolons to your warehouse in the capital. Your exclusive butler GIT has formulated a set of rules for the interaction between each semicolon and the general ledger information...

1.Git routine process (new file -> remote warehouse)

1. Store the file in the temporary storage area (git add)

git add <file> //添加目标文件到暂存区

git add *.txt //将某一类文件一同添加进暂存区例所有的.txt文件

git add . //将本地所有已跟踪的文件加入到暂存区

2. Store in the local warehouse from the staging area (git commit)

git commit -m [message] //将暂存区文件提交进本地仓库,message可以是一些备注信息
git log --oneline  //查看简洁版提交记录

3. The steps of adding and submitting files can be summarized into one step

git commit -am "message" //将提交所有已修改的文件到本地仓库。

4. View modifications and synchronize remote warehouse and local warehouse information

git show //查看本地仓库做的修改

git pull //提交前的同步操作

5. Submit to the remote warehouse

git push //提交到远程仓库

2. Four situations of version rollback

  • File rollback in workspace
git checkout -- filename  //在工作区的文件可用此方法回退。
  • Files are rolled back in the staging area
git restore --staged <file> //撤销暂存区的文件, 重新放回工作区 ( git add的反向操作)
  • Files are rolled back in the local warehouse
git log --oneline  //查看简洁版提交记录<commit_id>
git reset --hard <commit_id>  //回退到相应的版本
  • The file is already in the remote repository
//同第三步但需要执行强推操作
git log --oneline  //查看简洁版提交记录<commit_id>

git reset --hard <commit_id>  //回退到相应的版本

git pull  //git pull将旧版本重新上传到远程仓库,如果报错说版本低,需要强推上去 git pull -f

3. Compare the differences between regions

  • Comparison before version upload (comparison before git commit -m)
git diff HEAD  //比较工作区和版本库之间的差异,HEAD指的是分支最新提交节点

git diff --cached //比较暂存区和版本库之间的差异
  • Comparison after generating version number after uploading to local warehouse
git log --oneline //查看已提交到本地仓库的版本

git diff <版本号1> <版本号2>

//最经常用到的是比较当前版本和上一个版本之间的差异
git diff HEAD~ HEAD  //(git diff HEAD~2 HEAD)代表提交之前的二个版本以此类推。

git diff HEAD^ HEAD  //与~一个意思
  • Difference comparison of files
git diff + <文件名称>  //用于查看某一文件的差异

4. Delete files from the repository

Option 1:
①First use rm <file1.txt> to delete the target file in the local workspace
②Check the warehouse status git status (You will be prompted to update the files in the temporary storage area)
③git ls-files View the files in the temporary storage area (normally you will see that the contents in the temporary storage area have not been deleted)
④git add . (update the files in the temporary storage area)
Submit git commit -m "submit format"

Option 2: (The rm command will delete files in the local workspace and temporary storage area)
① git rm file2.txt
② git status View the status of the warehouse (the file has also been deleted)
③ git ls-files View the files in the temporary storage area
Submit git commit -m "Submit Format"

Summary of knowledge related to Git branches

Background: The book continues from the above. It is said that warehouses in various places are piled with various materials. However, you are bohemian by nature and do not want to spend too much time and experience managing this huge commercial and financial empire. The management is all handed over to your exclusive butler. As time goes by, your sales are getting bigger and bigger. Your exclusive butler alone is no longer enough to manage this huge amount of property for you. It didn’t take long for you to be smart and think of something. A good idea is to set up different branches in Jiuzhou, and each branch has a dedicated steward. Each steward manages the largest industrial warehouse here, which is called a local warehouse. They can set up different branches or merge branches according to their own needs. The helm or cancel the sub-rudder is used to enrich the materials in their local warehouses. When they handle related matters, they need to go to the sub-rudder where the problem is located. They obey your orders. With your order, these local warehouses can transfer all the materials to In the remote warehouse in the capital, everything happens very quickly. With such a good method, you have more and more industries under your control. You can't stand it anymore and decide to travel around the world...

Branch in git

1. View the branches currently in your warehouse

git branch //查看当前您所在的仓库中所有分支

2. Create a new branch

git branch <branch_name>  //创建一个新的分支(分舵)

git checkout -b <branch-name> //切换到指定分支, 并切换到新分支

3. Two ways to switch branches

//两种切换方法,区别在于避免checkout有歧义,故专门使用该指令切换分支
git checkout <branch_name> //切换到目标分支

git switch <branch_name> //切换到目标分支

4. Two methods of merging branches

//将不同分支合并到当前分支中,merge后面的分支名是将要被合并的分支。
//例如我们需要把dev合并到main分支上时,首先先切入main分支后再执行git merge dev操作达到目的。

git merge <branch_name>  //将<branch_name>分支合并到您当前所在的分支上

git rebase <branch-name> //在任意分支上执行变基操作将两分支合并到一起

git log --graph --oneline --decorate --all  //查看合并分支图

Knowledge supplement:

//查看图形化页面的提交记录原本命令
git log --graph --oneline --decorate --all

//命令别名化   
alias graph="git log --oneline --graph --decorate --all"

//之后两者就是等价状态
graph
4.1 Differences and problems encountered between the two merging methods
  • merge
    ①After the merge is successful, the original branch still exists. In order to avoid too many redundant branches, the corresponding branch can be deleted
git branch -d <branch_name> //  如果合并后一个分支不需要了,就可以使用该命令删除它

git branch -D <branch_name> //如果一个分支还是没有合并就不要了,就需要用-D强制删除

② This method of merging may cause merge conflicts and requires manual merging or terminating the merge.
Error reporting style

//手动合并合并完成再进行提交到本地仓库
git diff / git status //进行对比操作查看冲突文件,进行手动修改

git commit -am "name" //name为提交备注

//终止合并
git merge --about  //终止合并
  • rebase (rebase) merge
    Explanation of rebase merge: As shown below, the initial merge is in an intermediate state. If you perform a rebase operation on the dev branch, first find the common ancestor main3 of the dev branch and the main branch, find the latest commit record main5 on the main branch, graft the entire dev branch to the back of the main branch, and merge it into one branch.

Memory skills: On which branch the rebase operation is performed, the branch will be above the main branch after rebase.
Insert image description here

4.2 Analysis of advantages and disadvantages of merge and rebase
  • merge
    Advantages: It will not destroy the submission history of the original branch, making it easy to trace back and view.
    Disadvantages: Additional submission nodes will be generated, and the branch graph is more complicated.
  • Rebase (do not perform rebase operations on public branches)
    Advantages: No additional commit records will be added, forming a linear history, which is relatively intuitive and clean.
    Disadvantages: It will change the submission history and change the node of the current branch branch out, so avoid using it on the shared branch.

5.git stash

Usage scenario: Developing a new feature in a branch, which has not yet been completed. Halfway through, there is feedback about an urgent bug that needs to be dealt with, but the new feature is halfway developed and does not want to be submitted.

The Stash operation can "store" the current work site and continue working when the site is restored later.

1. When there are changes to the branch, you cannot commit or switch branches without committing them
Can't switch branches
0. Store the status at this moment

git stash save "message"  //massage是您存储写的信息,方便二次打开
//-u 参数表示把所有未跟踪的文件也一并存储;
//-a 参数表示把所有未跟踪的文件和忽略的文件也一并存储;
//save参数表示存储的信息, 可以不写。

Solution:
① Check the local storage list to see how many items there are, and restore them as needed

 git stash list  //查看所有stash
 git stash pop //恢复最近的一次stash

②Look at the list to see if there is a version that needs to be deleted.

git stash drop n //n指代是您删除{
    
    n}的序列号
git stash clear //删除所有的stash

③Restore to the specified version as needed

git stash pop stash@{
    
    n} //n为指定的版本,stash@{
    
    2}表示第三个stash, stash@{
    
    0}表示最近的stash。
//推荐apply
git stash apply n //n指代是您删除{
    
    n}的序列号

//pop和apply的区别是, pop会把stash内容删除, 而apply不会。
可以使用git stash drop 来删除stash。

Branch management and workflow model (understand)

GitFlow model

GitFlow model: branches are divided into five types

①main: mainline branch/baseline branch: contains the code of the latest stable version of the project, ensuring that the mainline branch code is releasable.
②hotfix: online version bug hot fix branch: its main function is to solve online problems. After the repair is completed, it will be merged back to the main branch. Generally, the corresponding version will be deleted after the repair is completed.
③develop: Development branch (core): It is separated from the main line branch and contains the code of the latest development version of the project for development and testing.
④Function branch (example: feature-login-page): used to develop new functions,
⑤release: Pre-release branch: further execute the version release process according to the ③release plan.

Main branch and auxiliary branch
Main branch (long-term existence): main branch + development branch
Auxiliary branch (delete after completing their respective functions) ): hotfit+feature branch+release

git tag to specify the version number

git tag <tag-name> //给当前的提交打上标签, 通常用于版本发布。

version number

GitHubFlow model (commonly used in systematic enterprises)

①Team members develop and test by separating their own branches from the main branch.
②Team members can submit code add/commits in the local branch.
③After the development is completed, the pull request operation (PR merge request) can be performed. Team members can Review it (discuss and review commits) -> deploy
④Branch naming specifications, branch creation specifications, and branch merging specifications
good habits

Summary of knowledge related to remote warehouses

Background: Have you ever remembered that the big mansion you bought with a lot of money when you first came to the capital has become the helmsman of your commercial and financial empire in a few years? It covers an area of ​​thousands of hectares, with pavilions in the hall. There are countless pavilions. After being taken care of by your dedicated butler, your mansion has been frequently expanded over the past few years. With the establishment of the Tianxia Jiuzhou branch, your mansion has become the general helm. With the efforts of each branch, you have already For the sub-rudder warehouses in various places, your mansion warehouse is more luxurious than them. It is a well-known remote warehouse with all the materials in it. The sub-rudder owners in various locations will naturally have to transfer the supplies from their local warehouses after a period of time. Materials are sent as tribute to you. At this time, you are already rich, but you are a famous big shot in the capital. However, you are busy with affairs and you are free by nature. In order to get rid of this tedious chore and the trouble of tribute from Kyushu, you have given them more power. They You can add, delete, and modify the remote warehouse of your residence according to your own needs. At the same time, they can also push corresponding materials to your residence regularly. There is nothing more glorious than this in life...

Interaction between local warehouse and remote warehouse

① Add a remote warehouse

git remote add <remote-name> <remote-url> //添加远程仓库

②View remote warehouse

git remote -v //查看远程仓库

③Delete remote warehouse

git remote rm <remote-name>  //删除远程仓库

④Rename the remote warehouse

git remote rename <old-name> <new-name> //重命名远程仓库。

⑤ Pull the code from the remote warehouse

git pull //默认拉去代码
git pull <remote-name> <branch-name> //从远程仓库拉取代码。 默认拉取远程仓库名origin的master或者main分支。

⑥Push the code to the remote warehouse

git push //推送代码进入远程仓库

⑦Get all remote branches

git fetch <remote-name> //获取所有的远程分支

⑧View all remote branches

git branch -r  //查看远程分支

Article summary:

Background: Just when you are thinking about how to further develop your business empire and how to further expand your business with your huge wealth, a thunder explodes in the sky and you wake up and sit up. What about beautiful buildings, small bridges and flowing water, birds singing and fragrant flowers, and a huge wealth? It's just a dream...

Origin of the article

This article is a summary of Xiaosheng’s own study and some excellent courses he watched on the Internet after entering the society and working. If you have enough time, you can go to GIT courses Carry out corresponding study, this course is very detailed, I highly recommend it!

Original intention of creation

Thank you all for reading. I also had a sudden impulse and wanted to share with you what I have gained after learning git. On the one hand, I made a note myself to deepen my memory. On the other hand, CSDN has given me a lot of help in my study and life. I hope everyone will like my article. By the way, if you have anything to add or modify after reading it, please tell me in the comment area. I hope you will give me some advice. Make progress together!

Guess you like

Origin blog.csdn.net/weixin_48495360/article/details/134681589