Home  >  Article  >  Development Tools  >  Detailed graphic explanation of Git branches

Detailed graphic explanation of Git branches

WBOY
WBOYforward
2022-03-15 18:27:525945browse

This article brings you relevant knowledge about Git, which mainly introduces related issues about branches, including the role of branches, master main branch, functional branches, branch operations, etc. I hope everyone has to help.

Detailed graphic explanation of Git branches

Recommended study: "Git Tutorial"

1. The role of Git branch

in When carrying out multi-person collaborative development, in order to prevent mutual interference and improve the experience of collaborative development, it is recommended that each developer develop project functions based on branches, for example:

2: master branch

When initializes the local Git warehouse, Git has been created for us by default A branch named master. Usually we call this master branch the main branch.

In actual work, the role of the master branch is: It is used to save and record the completed functional code of the entire project.
Therefore, programmers are not allowed to modify the code directly on the master branch , because the risk of doing so is too high and can easily cause the entire project to collapse, Therefore we need to be responsible for the development ourselves Develop on the branch

3. Function branch

Since programmers cannot directly develop functions on the master branch, so With the concept of function branches.
The function branch refers to the branch specially used to develop new functions. It is temporarily forked from the master branch. When the new functions are developed and tested, ,Finally needs to be merged into the master branch, as shown in the figure:

4. Local branch operation

1. View the branch list

Use the following command to view a list of all branches in the current Git repository:

git branch

Note: * in front of the branch indicates where is currently located Branch

2. Create a new branch

Use the following command to

create a new branch based on the current branch. At this time, The code in the new branch is exactly the same as the current branch: (So we need to create a new branch on the main branch during development)

git branch 分支名称

3. Switch branches

Use the following command to

switch to the specified branch for development:

git checkout 分支名称

4. Quick creation and switching of branches

Use the following command to

create a new branch with the specified name and switch to the new branch immediatelyUp:

1 #-b表示创建一个新分支
2 # checkout表示切换到刚才新建的分支上
3 git checkout -b 分支名称

5. Merge molecule

After the code development and testing of the functional branch is completed, you can use the following command to complete the Merge the code into the master branch:

1 切换到master分支
2 git checkout master
3在 master 分支上运行 git merge 命令,将要合并分支的代码合并到 master分支
4 git merge 分支名称

6. Delete the branch

After merging the code of the function branch into the master branch, You can use the following command to delete the corresponding function branch:

git branch -d 分支名称

7. Branch merging when encountering conflicts

If there are two

If different modifications are made to the same file in different branches, Git cannot merge them cleanly. At this point, we need to open these files containing conflicts and resolve the conflicts manually.

1#假设:在把reg分支合并到 master分支期间,代码发生了冲突
2 git checkout master
3 git merge reg

Conflict:

打开冲突的文件手动解决(也可以使用vs code的辅助解决(红色圈里面)

 解决后重新提交和合并

#打开包含冲突的文件,手动解决冲突之后,再执行如下的命令
 git add .
 git commit -m“解决了分支合并冲突的问题"
 git merge 分支名称

 8.将本地分支推送到远程仓库

如果是第一次将本地分支推送到远程仓库,需要运行如下的命令:

1#-u表示把本地分支和远程分支进行关联,只在第一次推送的时候需要带-u参数
2 git push -u 远程仓库的别名 本地分支名称:远程分支名称
3
4#实际案例:
5 git push -u origin payment: pay
6
7#如果希望远程分支的名称和本地分支名称保持一致,可以对命令进行简化:
8 git push -u origin payment

 如果不是第一次将本地分支推送到远程仓库,需要运行如下的命令:

则切换到要推送的分支后直接git push 就可以将本地分支推送到远程仓库 

9.查看远程仓库的所有分支列表 

git remote show 远程仓库名称

 10.跟踪分支

跟踪分支指的是:从远程仓库中,把远程分支下载到本地仓库中。需要运行的命令如下:

11.拉取远程分支的最新代码 

可以使用如下的命令,把远程分支最新的代码下载到本地对应的分支中:

1#从远程仓库,拉取当前分支最新的代码,保持当前分支的代码和远程分支代码一致
2 git pull

 12.删除远程分支

可以使用如下的命令,删除远程仓库中指定的分支:

1 #删除远程仓库中,指定名称的远程分支
2 git push 远程仓库名称 --delete 远程分支名称
3 #示例:
4 git push origin --delete pay

 五.总结

1.能够掌握Git中基本命令的使用
git init
git add .
git commit -m"提交消息"

git status和git status -s


2.能够使用Github 创建和维护远程仓库
能够配置Github 的SSH 访问

能够将本地仓库上传到Github

3.能够掌握Git分支的基本使用
git checkout -b 新分支名称

git push -u origin 新分支名称

git checkout 分支名称
git branch

推荐学习:《Git学习教程

The above is the detailed content of Detailed graphic explanation of Git branches. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete