一、分支概念
二、创建分支过程
1. 首先,确保你当前在主分支(通常是 master 或 main 分支)上工作。你
可以使用以下命令切换到主分支:
[root@YH1 ~]# cd /test //先切换到之前创建master主分支
[root@YH1 ~]# git checkout master //确保当前操作位置是在主分支
2. 接下来,使用以下命令创建一个新的分支:
[root@YH1 ~]# git branch feature-branch
这将创建一个名为 "feature-branch" 的新分支。
3. 现在,你需要切换到新创建的分支上。使用以下命令切换分支:
[root@YH1 ~]# git checkout feature-branch
[root@YH1 test]# git branch //查看当前分支
* feature-branch //当前所在哪个分支,哪个分支前面有*号master
[root@YH1 ~]# git checkout -b feature-branch
[root@YH1 test]# git branch //跟上面的操作效果一样
* feature-branch
master
这两个操作都将切换到新创建的 "feature-branch" 分支。
4. 在新分支上进行编写,提交更改。你可以使用以下命令添加更改并将更
改提交到本地仓库:
[root@YH1 test]# vim test.c //添加新内容
hello
test
[root@YH1 ~]# git add . //提交到缓存区,这里也可以写master分支的目录名,比如test
[root@YH1 ~]# git commit -m "提交消息" //提交消息写清注释信息即可
5. 当代码任务完成后,需要将更改合并回主分支。首先,切换回主分支:
[root@YH1 ~]# git checkout master
[root@YH1 test]# git branch
feature-branch
* master
[root@YH1 test]# cat test.c //刚才写入的内容没有了,是因为之前是在其他分支上操作的,并非在master上;所以切换回master后,显示的内容只会是master分支上的内容
6. 然后,使用以下命令将你的分支合并到主分支:
[root@YH1 ~]# git merge feature-branch
[root@YH1 test]# cat test.c //合并完成,其他分支的内容被合并到了master上
hello
test
7. 最后,如果你想删除不再需要的分支,可以使用以下命令删除分支:
[root@YH1 ~]# git branch -d feature-branch
[root@YH1 ~]# git branch -D feature-branch
三、解决分支冲突
[root@YH1 test]# git checkout -b y123 // 创建并切换到新 分支 切换到一个新分支 'y123'
[root@YH1 test]# vim test.c //写入点内容
hello
test
yyyy
[root@YH1 test]# git add test.c //将新分支写入的内容提交到缓存
[root@YH1 test]# git commit -m "yy" //提交新分支并注释为“yy”
[y123 32e5a5a] yy
1 file changed, 1 insertion(+)
[root@YH1 test]# git checkout master //切换到主分支切换到分支 'master'
[root@YH1 test]# cat test.c //此时还未合并,所以只显示当前master分支的内容
hello
test
[root@YH1 test]# vim test.c //向master分支内写入些内容
hello
test
hhhh
[root@YH1 test]# git add test.c //提交master分支
[root@YH1 test]# git commit -m "hh" //正式提交,注释信息为“hh”
[master 4a51362] hh
1 file changed, 1 insertion(+)
[root@YH1 test]# git merge y123 //合并新分支
自动合并 test.c
冲突(内容):合并冲突于 test.c //提示冲突了
自动合并失败,修正冲突然后提交修正的结果。
[root@YH1 test]# git status //git状态信息也提示两个分支冲突了
位于分支 master
您有尚未合并的路径。
(解决冲突并运行 "git commit")
(使用 "git merge --abort" 终止合并)
未合并的路径:
(使用 "git add <文件>..." 标记解决方案)
双方修改: test.c
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@YH1 test]# cat test.c //git使用记号帮我们标记除了冲突位置和新分支的内容
hello
test
<<<<<<< HEAD
hhhh
=======
yyyy
>>>>>>> y123
[root@YH1 test]# vim test.c //手动修改冲突文件
hello
test
hhhh
yyyy
[root@YH1 test]# git add test.c //提交修改后的分支
[root@YH1 test]# git commit -m "zuizhong" //正式提交,注释信息为“zuizhong”
[master f13e70e] zuizhong
[root@YH1 test]# git log --graph --pretty=oneline -- abbrev-commit //查看日志分析分支合并情况
* f13e70e (HEAD -> master) zuizhong //当前分支所处位置为zuizhong
|\
| * 32e5a5a (y123) yy //y123分支冲突时所处的分支位置为yy
* | 4a51362 hh //master分支冲突时所处的分支位置为hh
|/
* 173a360 test
* 9e89cac add new file test.c
[root@YH1 test]# git branch -d y123 //删除y123分支
已删除分支 y123(曾为 32e5a5a)。
[root@YH1 test]# cat test.c //查看master分支上的最终版本
hello
test
hhhh
yyyy
四、Git 拉取
[root@YH2 ~]# ssh-keygen
[root@YH2 ~]# ssh-copy-id root@192.168.33.11
[root@YH2 ~]# yum -y install git
[root@YH2 ~]# mkdir /yh2 //新建git仓库
[root@YH2 ~]# cd /yh2/
[root@YH2 yh2]# git init //仓库初始化,当前目录作为master
[root@YH2 yh2]# git clone 192.168.33.11:/test/.git //将YH1的test分支克隆到本机
[root@YH2 yh2]# ls
test
[root@YH2 yh2]# ls test/
test.c
五、部署 Gitlab 服务器
1、搭建环境
系统 | IP地址 | 主机名 | 所需软件 |
Rokcy8.7 | 192.168.33.11 | YH1 |
gitlab-ce-12.10.14- ce.0.el8.x86_64git-2.39.3- 1.el8_8.x86_64 |
Rokcy8.7 | 192.168.33.22 | YH2 |
git-2.39.3-1.el8_8.x86_64 |
2、安装 gitlab
[root@YH1 ~]# yum -y install git //安装git
[root@YH1 ~]# yum -y install curl openssh-server postfix cronie // 安装依赖环境
[root@YH1 ~]# wget --content-disposition
[root@YH1 ~]# ls
公共 图片 音乐 gitlab-ce-12.10.14-
ce.0.el8.x86_64.rpm mysql.sh
模板 文档 桌面 initial-setup-ks.cfg
replication_mysql.yml
视频 下载 anaconda-ks.cfg ip.sh
[root@YH1 ~]# rpm -ivh gitlab-ce-12.10.14-
ce.0.el8.x86_64.rpm
[root@YH1 ~]# rpm -q gitlab-ce
gitlab-ce-12.10.14-ce.0.el8.x86_64
[root@YH2 ~]# yum -y install git //YH2安装git
3、加载配置并启动 gitlab
[root@YH1 ~]# gitlab-ctl reconfigure
[root@YH1 ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE
NAME
nginx 1726 root 7u IPv4 36866 0t0 TCP
*:http (LISTEN)
nginx 1800 gitlab-www 7u IPv4 36866 0t0 TCP
*:http (LISTEN)
nginx 1801 gitlab-www 7u IPv4 36866 0t0 TCP
*:http (LISTEN)
4、浏览器访问 gitlab
5、创建项目
点击创建项目
创建一个任意项目,按照图示操作即可
6、新建主分支
跳转到新页面后,点图中所示的灰色加号,再点新建文件
写清文件名,写好内容,点下面的提交更改
7、新建其他分支
点击左侧列表的分支,即可看到刚才新建的分支文件,再点击新分支,来创
建其他分支
输入新分支名称(因为翻译缘故,这里叫做分行名称),再点击创建分支
待页面跳转后,再次点击左侧分支项,点合并请求
输入标题
跳转后的页面,点创建文件
写点内容,再点下方的提交更改
点击 解决 WIP 状态
点 合并
8、客户端克隆分支
[root@YH2 ~]# git clone http://192.168.33.11/root/test.git
[root@YH2 ~]# cd test/
[root@YH2 test]# ls
123456 666 README.md
[root@YH2 test]# cat 666
yunjisuan
[root@YH2 test]# cat 123456
I am yourfather