【git笔记】分支管理
2021/11/17 23:13:53
本文主要是介绍【git笔记】分支管理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
git笔记 分支管理
- git笔记 分支管理
- 说明
- 命令
- 创建分支
- 查看分支情况
- 合并分支
- 切换分支
- 删除分支
- 推送分支
- 创建远程仓库的分支到本地
- 查看分支合并情况
- 命令使用示例
- 创建分支示例
- 切换分支示例
- 删除分支示例
- 合并分支示例
- 推送分支示例
- 获取远程仓库的分支
- 解决本地分支冲突示例
- [参考资料]
git笔记 分支管理
说明
介绍git分支相关命令。环境:Ubuntu/Debian。
命令
创建分支
创建分支
git branch <branchname>
该命令执行后,创建一个分支。
创建并切换到分支
git checkout -b <branchname>
或者是
git branch <branchname> git checkout <branchname>
或者是Git version2.23新增的命令
git switch -c <branchname>
该命令执行后,创建一个分支并切换到这个分支下。
查看分支情况
git branch
该命令执行后,会列出所有分支,并显示当前分支。
合并分支
git merge <branchname>
该命令执行后,会合并指定分支到当前分支。
切换分支
git checkout <branchname>
或者是Git version2.23新增的命令
git switch <branchname>
该命令执行后,会切换到指定分支。
删除分支
git branch -d <branchname>
该命令执行后,会删除到指定分支。
推送分支
git push <remote> <branch>
该命令执行后,会推送<branch>
分支到远程仓库<remote>
。
远程仓库默认名称是origin
git push origin <branchname>
该命令执行后,会推送<branch>
分支到远程仓库origin
。
创建远程仓库的分支到本地
git checkout -b <branchname> <remote>/<branchname>
该命令执行后,会创建远程仓库<remote>
的<branchname>
分支到本地分支<branchname>
。
远程仓库默认名称是origin
git checkout -b <branchname> origin/<branchname>
该命令执行后,会创建远程仓库origin
的分支到本地。
查看分支合并情况
git log --graph
或者
git log --graph --pretty=oneline --abbrev-commit
该命令执行后,可以看出分支的合并情况。
命令使用示例
创建分支示例
创建分支示例
使用命令git branch testbranch
创建testbranch
分支。
然后使用命令git branch
查看分支情况
* master testbranch
可以看到新建了一个testbranch
分支。
创建并切换到分支
使用命令git checkout -b testbranch
创建并切换到testbranch
分支。
Switched to a new branch 'testbranch'
然后使用命令git branch
查看分支情况
master * testbranch
可以看到新建了一个分支testbranch
,并且当前已经切换到了testbranch
分支。
切换分支示例
当前有两个分支,分别是master
和testbranch
分支。
使用命令git branch
查看分支情况。
* master testbranch
然后执行git checkout testbranch
,切换到testbranch
分支。
Switched to branch 'testbranch'
使用命令git branch
查看分支情况。
master * testbranch
可以看到该命令执行后,会切换到指定的testbranch
分支。
删除分支示例
当前有两个分支,分别是master
和testbranch
分支。
使用命令git branch
查看分支情况。
* master testbranch
然后执行git branch -d testbranch
,删除testbranch
分支。
Deleted branch testbranch (was 632d1fe).
使用命令git branch
查看分支情况。
* master
可以看到该命令执行后,testbranch
分支会被删除。
合并分支示例
当前有两个分支,分别是master
和testbranch
分支,现在在testbranch
分支。
目前仓库中有一个文件README.md。
现在对里面的内容做出了修改。
执行git add README.md
和git commit -m "branch test"
提交到本地仓库(local repository)。
[testbranch 20e9301] branch test 1 file changed, 1 insertion(+)
这个时候完成testbranch
分支的开发工作。
执行git checkout master
切换回master
分支。
现在执行git merge testbranch
,将分支的更新内容合并到master
分支。
Updating 632d1fe..20e9301 Fast-forward README.md | 1 + 1 file changed, 1 insertion(+)
这个时候查看master
分支的内容已经都和testbranch
分支内容相同了。
推送分支示例
当前有两个分支,分别是master
和testbranch
分支,现在在testbranch
分支。
执行命令git push origin testbranch
推送分支到远程仓库(remote repository)
remote: remote: To create a merge request for testbranch, visit: remote: https://github.com/test=testbranch remote: To https://github.com/test/test.git * [new branch] testbranch -> testbranch
获取远程仓库的分支
执行命令git clone https://github.com/test/gittest.git
将项目克隆到本地
Cloning into 'gittest'... remote: Enumerating objects: 23, done. remote: Counting objects: 100% (23/23), done. remote: Compressing objects: 100% (15/15), done. remote: Total 23 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (23/23), done.
使用命令git branch
查看分支情况
* master
发现只有master
分支,没有其他分支。
执行命令git checkout -b testbranch origin/testbranch
创建本地testbranch
分支
Branch testbranch set up to track remote branch testbranch from origin. Switched to a new branch 'testbranch'
这个时候再使用命令git branch
查看分支情况
master * testbranch
就已经有testbranch
分支,可以在该分支上进行操作了。
解决本地分支冲突示例
当前有两个分支,分别是master
和testbranch
分支,现在在testbranch
分支。
目前仓库中有一个文件NEW.md。
现在对里面的内容做出了修改,改成以下内容。
test branch
执行git add README.md
和git commit -m "branch test"
提交到本地仓库(local repository)。
[testbranch 5421aa7] branch test 1 file changed, 1 insertion(+)
这个时候完成testbranch
分支的开发工作。
执行git checkout master
切换回master
分支。
现在对NEW.md文件里面的内容做出修改,改成以下内容。
test master
执行git add README.md
和git commit -m "master test"
提交到本地仓库(local repository)。
[master 6086e59] master test 1 file changed, 1 insertion(+)
这个时候执行git merge testbranch
命令来合并testbranch
分支
Auto-merging NEW.md CONFLICT (content): Merge conflict in NEW.md Automatic merge failed; fix conflicts and then commit the result.
提示文件冲突,需要手动解决冲突才能提交。
使用git status
命令查看
# On branch master # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: NEW.md # no changes added to commit (use "git add" and/or "git commit -a")
看到NEW.md被共同修改了。
使用cat NEW.md
文件查看
<<<<<<< HEAD test master ======= test branch >>>>>>> testbranch
可以看到标记出了不同分支的冲突内容。
手动修改NEW.md文件,解决冲突。
修改为
test merge
执行git add NEW.md
和git commit -m "merge test"
重新提交。
提交完成后,可以使用命令git log --graph
或者git log --graph --pretty=oneline --abbrev-commit
查看分支的合并情况。
使用命令git log --graph
查看
# git log --graph * commit 09d41b448dfa3abb4526aa5406ce5d49d6724e59 |\ Merge: 6086e59 5421aa7 | | Author: root <root@Linux-host.(none)> | | Date: Sun Jan 24 23:22:09 2021 +0800 | | | | merge test | | | * commit 5421aa77f5abcc81174d358f4dde50d615524e59 | | Author: root <root@Linux-host.(none)> | | Date: Sun Jan 24 23:17:08 2021 +0800 | | | | branch test | | * | commit 6086e59208cb8545c40b80c6563ff5094864bafa |/ Author: root <root@Linux-host.(none)> | Date: Sun Jan 24 23:18:29 2021 +0800 | | master test | * commit 632d1fee973a4fee3256d3fb05f3cdf2e9324e59 | Author: root <root@Linux-host.(none)> | Date: Sun Jan 24 17:38:31 2021 +0800 | | version |
或者使用命令git log --graph --pretty=oneline --abbrev-commit
查看
# git log --graph --pretty=oneline --abbrev-commit * 09d41b4 merge test |\ | * 5421aa7 branch test * | 6086e59 master test |/ * 632d1fe version
[参考资料]
git Documentation
git-branch
多人协作
解决冲突
本文链接:https://blog.csdn.net/u012028275/article/details/121389537
这篇关于【git笔记】分支管理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现