1.7分支结构
在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。
那在哪干活呢?干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。所以团队的合作分支看起来会像下图那样。
1.7.1分支创建与切换
[root@gitlab git_data]#git branch #查看当前所在分支
[root@gitlab git_data]#git branchlinux#创建分支 'linxu'
*master
[root@gitlab git_data]#git checkout linux#切换到分支'linux'
[root@gitlab git_data]#git branch
*linux
master
在linux分支进行修改
[root@gitlab git_data]#echo "2017年11月30日" >> README
[root@gitlab git_data]#git commit -a -m "2017年11月30日09点10分"[linux 5a6c037] 2017年11月30日09点10分
1 file changed, 1 insertion(+)
[root@gitlab git_data]#git status
#位于分支 linux
无文件要提交,干净的工作区
在回到master分支查看这个文件是否有内容(没有就对了,因为是在linux分支创建的)
[root@gitlab git_data]#git checkout master
切换到分支'master'
[root@gitlab git_data]#cat README
[root@gitlab git_data]#git log -1commit 7015bc7b316cc95e2dfe6c53e06e3900b2edf427
Date: Wed Nov29 19:30:57 2017 +0800
123
合并代码
注意:只能下级分支合并到上级分支(先切换到上级分支)
[root@gitlab git_data]#git merge linux#git merge 分支名称
Fast-forward
README| 1 +
1 file changed, 1 insertion(+)
[root@gitlab git_data]#git status
#位于分支 master无文件要提交,干净的工作区
[root@gitlab git_data]#cat README
2017年11月30日
1.7.2合并失败解决
模拟冲突,在文件的同一行做不同修改
在master分支进行修改
[root@gitlab git_data]#cat README2017年11月30日
[root@gitlab git_data]#echo "kai in master">> README
[root@gitlab git_data]#git commit -a -m "kai 2017年11月30日 09点20分 "[master 7ab71d4] clsn 2017年11月30日 09点20分
1 file changed, 1 insertion(+)
切换到linux分支进行修改
[root@gitlab git_data]#git checkout linux
切换到分支'linux'
[root@gitlab git_data]#cat README2017年11月30日
[root@gitlab git_data]#echo "kai in linux" >> README
[root@gitlab git_data]#git commit -a -m "2017年11月30日 03"
[linux 20f1a13] 2017年11月30日 03
1 file changed, 1 insertion(+)
回到master分支,常识进行合并,出现冲突
[root@gitlab git_data]#git checkout master
切换到分支'master'
[root@gitlab git_data]#git merge linux自动合并 README
冲突(内容):合并冲突于 README
自动合并失败,修正冲突然后提交修正的结果。
人工介入,解决冲突
[root@gitlab git_data]#vim README2017年11月30日
kaiinmaster
kaiinlinux
再次提交
[root@gitlab git_data]#git commit -a -m "2017年11月30日 03"
[master b6a097f] 2017年11月30日 03
1.7.3删除分支
因为之前已经合并了linux分支,所以现在看到它在列表中。在这个列表中分支名字前没有*号的分支通常可以使用git branch -d删除掉;你已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。
查看所有包含未合并工作的分支,可以运行git branch --no-merged:
git branch --no-merged
testing
这里显示了其他分支。因为它包含了还未合并的工作,尝试使用git branch -d命令删除它时会失败:
git branch -d testing #相当于删除一个软连接
error: The branch'testing'isnotfully merged.
If you are sure you want to delete it, run'git branch -D testing'.
如果真的想要删除分支并丢掉那些工作,如同帮助信息里所指出的,可以使用-D选项强制删除它。
1.8 windwos上Git的使用
windows上git软件网站https://git-for-windows.github.io
软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git-2.15.1.2-64-bit.exe
软件安装默认即可。
1.8.1软件使用
创建新的仓库
定义仓库的路径
方法一:添加用户信息(在git bash中)
default@Hzs-Desktop MINGW64 /i/git_data (master)
default@Hzs-Desktop MINGW64 /i/git_data (master)
$ git config--globaluser.name"kai"
方法二:在git Gui中添加用户信息,添加一次就可
在页面中将数据配置好即可使用
查看历史数据
1.9gitlab的使用
前面我们已经知道Git人人都是中心,那他们怎么交互数据呢?
•使用GitHub或者码云等公共代码仓库(加密要收费)
•使用GitLab私有仓库
1.9.1安装配置gitlab私有仓库
官方安装文档https://about.gitlab.com/installation/
国内软件镜像站https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
安装
准备软件:gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
[root@gitlab ~]#yum localinstall gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
初始化(只执行一次就够了,出问题可以执行多次)
[root@gitlab ~]#gitlab-ctl reconfigure #会自动启动服务
启动gitlab-ctl命令: gitlab-ctl start
状态
[root@gitlab ~]#gitlab-ctl status
run: gitaly: (pid 4491) 48s; run: log: (pid 4087) 279s
run: gitlab-monitor: (pid 4539) 44s; run: log: (pid 4251) 207s
run: gitlab-workhorse: (pid 4501) 47s; run: log: (pid 4099) 273s
run: logrotate: (pid4125) 265s; run: log: (pid 4124) 265s
run: nginx: (pid4112) 271s; run: log: (pid 4111) 271s
run: node-exporter: (pid 4175) 243s; run: log: (pid 4174) 243s
run: postgres-exporter: (pid 4528) 45s; run: log: (pid 4223) 219s
run: postgresql: (pid3933) 343s; run: log: (pid 3932) 343s
run: prometheus: (pid4514) 46s; run: log: (pid 4156) 259s
run: redis: (pid3876) 355s; run: log: (pid 3875) 355s
run: redis-exporter: (pid 4186) 237s; run: log: (pid 4185) 237s
run: sidekiq: (pid4078) 281s; run: log: (pid 4077) 281s
run: unicorn: (pid4047) 287s; run: log: (pid 4046) 287s
检查端口
[root@gitlab ~]#netstat -lntup|grep 80
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 4073/unicorn master
tcp 0 00.0.0.0:80 0.0.0.0:* LISTEN 4112/nginx: master
tcp 0 00.0.0.0:8060 0.0.0.0:* LISTEN 4112/nginx: master
1.9.2使用浏览器访问,进行web界面操作
第一次访问,创建密码
创建一个新的项目
定义项目的名称
创建完成后会提示没有添加ssh密钥
在服务器上创建ssh密钥使用ssh-ketgen命令
[root@gitlab ~]#ssh-keygen
Generating public/private rsa key pair.
Enter fileinwhich to save the key (/root/.ssh/id_rsa):
Created directory'/root/.ssh'.
Enter passphrase (emptyforno passphrase):
Enter same passphrase again:
Your identification has been savedin/root/.ssh/id_rsa.
Your public key has been savedin/root/.ssh/id_rsa.pub.
The key fingerprintis:
SHA256:n/V2kCiwwm2UfBsnQLm17eXUCBiBByyPbefmz5oQvfU root@gitlab
The key's randomart image is:
+---[RSA 2048]----+
| o++o+ |
| ..+o+ . |
| ==++o.. o |
| ..o==o=..+..|
| o.So+.++o |
| o oo*.o.. |
| .o+ E .|
| ..o . . |
| ooo |
+----[SHA256]-----+
[root@gitlab .ssh]#cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSVdBypha/ALMmvIiZGXxYGz7FJ5TC+hYWo7QGBJ+J6JVinp9yH851fwxln5TWGBrtEousoVHXTTJfFRy8LV+Ho7OfaksYt+5TPxEjf5XX53Z3ZX70PYH3DQFmgzl0QpWw1PYIjrD7kBeLhUg+R/ZePS+HzPvbRCb6gOlkdx46vX4Olr7YbAO5lzAarhaZcE2Q702kPXGeuZbR7KcwVhtoiueyHwyj94bccMfKq7qSskXGbpWuCwcaKQ6uqGap1rP5Viqqv0xeO7Vq0dIZ/YnPL2vPDUvNa36nHosiZGkn4thpPh63KjXaFIfKOuPemLzvDZY0A+88P8gwmAYiPoxp root@gitlab
将密钥添加到web界面的用户中
gitlab自带的命令集
Command line instructions
Gitglobalsetup
git config--globaluser.name"Administrator"
Create a new repository
git clone git@10.0.0.63:root/kai.git
cd kai
touch README.md
git add README.md
git commit-m"add README"
git push-u origin master
Existing folder
cd existing_folder
git init
git add .
git commit-m"Initial commit"
git push-u origin master
Existing Git repository
cd existing_repo
git remote rename origin old-origin
git push-u origin --all
git push-u origin --tags
创建行的git仓库
[root@gitlab ~]#git clone git@gitlab:root/Test1.git
正克隆到'Test1'...
The authenticity of host'gitlab (10.0.0.63)'can't be established.
ECDSA key fingerprintisSHA256:yOrzs0W+R//s8VDEN9nko6r6wW+8gwJl3Ut7ac0i5SY.
ECDSA key fingerprintisMD5:21:33:dd:4d:01:00:eb:71:a4:4e:2d:2b:bf:37:48:ed.
Are you sure you want tocontinueconnecting (yes/no)? yes
Warning: Permanently added'gitlab'(ECDSA) to the list of known hosts.
warning: 您似乎克隆了一个空版本库。
创建文件并推到远端git仓库
[root@gitlab Test1]#echo "clsn" >> clsn
[root@gitlab Test1]#git push -u origin master分支 master 设置为跟踪来自 origin 的远程分支 master。
Everything up-to-date
推送完成后能够在web界面中查看
# md语法的使用方法
至此gitlab的使用结束了