分支管理#180804
2018-08-04 11:44
134 查看
查看当前仓库分支
[root@localhost ~]# cd /data/gitroot/ [root@localhost gitroot]# git branch * master
*为当前所在的分支的位置
创建新分支
[root@localhost gitroot]# git branch main [root@localhost gitroot]# git branch main * master [root@localhost gitroot]# ls 1.txt
切换分支
[root@localhost gitroot]# git checkout main Switched to branch 'main' [root@localhost gitroot]# git branch * main master [root@localhost gitroot]# ls 1.txt
[root@localhost gitroot]# echo "222" > 2.txt [root@localhost gitroot]# git add . [root@localhost gitroot]# git commit -m "add 2.txt" [main 9b0965c] add 2.txt Committer: root <root@localhost.localdomain> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+) create mode 100644 2.txt
[root@localhost gitroot]# ls 1.txt 2.txt [root@localhost gitroot]# git checkout master Switched to branch 'master' [root@localhost gitroot]# ls 1.txt
分支合并
合并之前,先切换到目标分支[root@localhost gitroot]# git checkout master Already on 'master' [root@localhost gitroot]# git branch main * master [root@localhost gitroot]# git merge main Updating c246d69..9b0965c Fast-forward 2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 2.txt [root@localhost gitroot]# ls 1.txt 2.txt
合并冲突
[root@localhost gitroot]# echo "222" >> 2.txt [root@localhost gitroot]# git add 2.txt [root@localhost gitroot]# git commit -m "add 2.txt" [master 8658308] add 2.txt Committer: root <root@localhost.localdomain> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+)
[root@localhost gitroot]# git checkout main Switched to branch 'main' [root@localhost gitroot]# echo "333" >> 2.txt [root@localhost gitroot]# git add 2.txt [root@localhost gitroot]# git commit -m "add 2.txt" [main 7f8ccff] add 2.txt Committer: root <root@localhost.localdomain> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 1 insertion(+)
[root@localhost gitroot]# git checkout master Switched to branch 'master' [root@localhost gitroot]# git merge main Auto-merging 2.txt #自动合并 2.txt CONFLICT (content): Merge conflict in 2.txt #冲突(内容):合并冲突于2.txt Automatic merge failed; fix conflicts and then commit the result. #自动合并失败,修正冲突然后提交修正的结果
解决冲突的方法就是修改内容为一致,将当前内容修改为与main的2.txt一致
若main的内容为保留内容,可使用倒着合并,即先切换到main下,在进行合并
[root@localhost gitroot]# cat 2.txt 222 <<<<<<< HEAD 222 ======= 333 >>>>>>> main
冲突部分,上面为master,下面main
[root@localhost gitroot]# git checkout main 2.txt: needs merge error: you need to resolve your current index first #您需要先解决当前索引的冲突 [root@localhost gitroot]# git checkout main Switched to branch 'main'
当发生冲突,不能切换分支
查看提示信息
[root@localhost gitroot]# git commit -a Merge branch 'main' Conflicts: 2.txt # # It looks like you may be committing a merge. # If this is not correct, please remove the file # .git/MERGE_HEAD # and try again. # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Committer: root <root@localhost.localdomain> # # On branch master # All conflicts fixed but you are still merging. # (use "git commit" to conclude merge) # # Changes to be committed: # # modified: 2.txt #
解决冲突方法,修改内容为一致,或将冲突文件进行重命名,再次合并
删除分支
[root@localhost gitroot]# git branch -d main error: Cannot delete the branch 'main' which you are currently on. #无法删除当前所在的分支 [root@localhost gitroot]# git checkout master Switched to branch 'master' [root@localhost gitroot]# git branch -d main Deleted branch main (was 7f8ccff). #已删除分支
若分支还没有合并,删除前则会提示
强制删除
[root@localhost gitroot]# git branch -D main
若不合并,则会强制删除
相关文章推荐
- 内存管理之11:物理内存页面的分配
- Shell脚本学习笔记
- nginx 服务器报错
- SDIBT_2018SummerVacationTraining_5
- 想提高爬虫效率?aiohttp 了解下
- 人工智能 —— 博客服务器
- entity和domain包名区别
- python aiohttp asyncio 异步请求
- 浅析对人工智能,机器学习和深度学习的理解
- email Python using SMTP
- AI 语音对话技术
- 要发展AI,离不开“三算”
- 研究人员开发出可描绘大脑神经元的人工智能
- async 和 await
- 分离链接法(Separate Chaining)
- 巡风源码阅读与分析---Aider.py
- Windows下 import torchvision :ImportError: DLL load failed: The specified module could not be found.
- #C程序语言设计练习1-16#对用于打印最长行的程序的主程序main进行修改,使之可以打印任意长度的输入行的长度,以及文本中尽可能多的字符。
- Large-Scale Terrain Rendering forv Outdoor Games
- background-size值为cover、100%和contain的区别