一、分支概念
每次提交, Git 都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支。HEAD 严格来说不是指向提交,而是指向 master ,而 master 才是指向提交的,所以, HEAD 指向的就是当前分支。开始的时候, master 分支是一条线, Git 用 master 指向最新的提交,再用HEAD 指向 master ,就能确定当前分支,以及当前分支的提交点。每次提交, master 分支都会向前移动一步,这样,随着你不断提交, master 分支的线也越来越长。
二、创建分支过程
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
也可以使用如下命令直接创建并切换到 "feature-branch" 分支, -b 选项相当于执行两条命令, git checkout 和 git branch 。
[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
这将把你的 "feature-branch" 分支的更改合并到主分支上。如果存在冲突,需要手动解决冲突。 Git 会在冲突的文件中标记出冲突的部分,你需要打开这些文件并手动编辑以解决冲突。解决冲突后,你需要再次提交更改。
7.
最后,如果你想删除不再需要的分支,可以使用以下命令删除分支:
[root@YH1 ~]# git branch -d feature-branch
这将删除名为 "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
(1)git仓库的建立
1)找到一个空目录 mkdir /test
2)进入到这个目录 cd /test
3)执行git指令 git init
4)发现一个隐藏目录 .git
5)test目录就是工作区域,.git就是仓库
(2)常用的指令
1)在test目录中修改文件
2)git add .|文件名 将修改的文件提交的暂存区域
3)git commit -m "说明" 将暂存区的数据保存到仓库
4)git log 查看提交日志
(3)分支
1)让一个项目有多重可能性2)分支的创建,删除合并a.查看分支 git branch 有几行,就有几个分支, * 标注当前所在的分支b.切换分支的指令 git checkout 分支名c.新增分支git branch 新分支名在跳转分支的时候创建 git checkout -b 新分支名
(4)删除分支,要求是分支已经合并1)git branch -d 分支名2)git branch -D 分支名
(5)分支冲突将两个分支合并git checkout abranchgit merge bbranch 在 a 分支合并 b 分支a 分支和 b 分支都对同一个文件进行了修改,并且 commit ,合并的时候,就是保分支冲突,需要手动选择 再提交就可以了
四、Git 拉取
再启动一台主机, YH2
[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
当前网站: http://192.168.33.11首次登录只设置密码即可,用户名为 root
![](https://i-blog.csdnimg.cn/direct/496e4e51713a46ffa99acc1a3728b6fd.jpeg)
5、创建项目
点击创建项目
![](https://i-blog.csdnimg.cn/direct/e1aae6d275f640dba16167df5de01957.jpeg)
创建一个任意项目,按照图示操作即可
6、新建主分支
跳转到新页面后,点图中所示的灰色加号,再点新建文件
![](https://i-blog.csdnimg.cn/direct/45b95db375734330b37e6205aeecacbe.jpeg)
写清文件名,写好内容,点下面的提交更改
![](https://i-blog.csdnimg.cn/direct/7dd70e0bfc9f43fea2463496d0c7b91c.jpeg)
7、新建其他分支
点击左侧列表的分支,即可看到刚才新建的分支文件,再点击新分支,来创
建其他分支
![](https://i-blog.csdnimg.cn/direct/8f33c2655809449594168768a6501737.jpeg)
输入新分支名称(因为翻译缘故,这里叫做分行名称),再点击创建分支
待页面跳转后,再次点击左侧分支项,点合并请求
输入标题
跳转后的页面,点创建文件
写点内容,再点下方的提交更改
点击 解决 WIP 状态
点 合并
8、客户端克隆分支
使用 YH2 克隆 YH1 建立好的分支
[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