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.
Recommended study: "Git Tutorial"
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:
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
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:
git branch
Note: * in front of the branch indicates where is currently located Branch
2. Create a new branchUse the following command tocreate 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 branchesUse 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 分支名称
1 切换到master分支 2 git checkout master 3在 master 分支上运行 git merge 命令,将要合并分支的代码合并到 master分支 4 git merge 分支名称
git branch -d 分支名称
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 分支名称
如果是第一次将本地分支推送到远程仓库,需要运行如下的命令:
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 就可以将本地分支推送到远程仓库
git remote show 远程仓库名称
跟踪分支指的是:从远程仓库中,把远程分支下载到本地仓库中。需要运行的命令如下:
可以使用如下的命令,把远程分支最新的代码下载到本地对应的分支中:
1#从远程仓库,拉取当前分支最新的代码,保持当前分支的代码和远程分支代码一致 2 git pull
可以使用如下的命令,删除远程仓库中指定的分支:
1 #删除远程仓库中,指定名称的远程分支 2 git push 远程仓库名称 --delete 远程分支名称 3 #示例: 4 git push origin --delete pay
推荐学习:《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!