zoukankan      html  css  js  c++  java
  • Git本地远程仓库

    Git本地远程仓库

    1. GitLab服务器简介

    1.GitLab分类

    • 使用GitHub或者码云等公共代码仓库
    • 使用GitLab私有仓库

    2.GitLab是什么?

    是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。基础功能免费,高级功能收费。

    3.为什么要使用GitLab?

    • 基础功能开源,可自行搭建
    • 可以进行权限控制,使得代码对部分人可见
    • gitlab使用方便

    2. GitLab安装

    主机名 IP 备注
    git01 192.168.200.31 GitLab服务器
    git02 192.168.200.32 Git客户端一
    git03 192.168.200.33 Git客户端二

    2.1 安装GitLab

    官方安装文档:https://about.gitlab.com/installation/#centos-7

    GitLab国内源下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

    #根据需要下载GitLab安装包
    [root@git01 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
    
    [root@git01 ~]# yum -y localinstall gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm 
    
    重要说明:
    yum localinstall 利用yum安装本地指定的rpm包,好处是自动解决依赖问题
    

    2.2 初始化GitLab

    [root@git01 ~]# gitlab-ctl reconfigure
    #以下省略若干。。。
    
    #查看gitlab启动状态
    [root@git01 ~]# gitlab-ctl status
    run: alertmanager: (pid 3561) 49s; run: log: (pid 3267) 104s
    run: gitaly: (pid 3407) 53s; run: log: (pid 2685) 209s
    run: gitlab-exporter: (pid 3454) 51s; run: log: (pid 3192) 122s
    run: gitlab-workhorse: (pid 3430) 53s; run: log: (pid 3017) 160s
    run: grafana: (pid 3578) 48s; run: log: (pid 3386) 65s
    run: logrotate: (pid 3058) 146s; run: log: (pid 3068) 142s
    run: nginx: (pid 3031) 157s; run: log: (pid 3042) 154s
    run: node-exporter: (pid 3444) 52s; run: log: (pid 3131) 126s
    run: postgres-exporter: (pid 3570) 48s; run: log: (pid 3296) 98s
    run: postgresql: (pid 2796) 206s; run: log: (pid 2853) 203s
    run: prometheus: (pid 3464) 50s; run: log: (pid 3240) 110s
    run: redis: (pid 2642) 214s; run: log: (pid 2652) 213s
    run: redis-exporter: (pid 3457) 51s; run: log: (pid 3219) 114s
    run: sidekiq: (pid 2981) 169s; run: log: (pid 2995) 166s
    run: unicorn: (pid 2952) 175s; run: log: (pid 2973) 174s
    

    2.3 GitLab中文社区版补丁包安装

    GitLab中文社区:https://gitlab.com/xhang/gitlab/tree/11-2-stable-zh

    #解压GitLab中文版补丁包
    [root@git01 ~]# tar xf gitlab-12-3-stable-zh.tar.gz
    
    #查看系统已经安装的GitLab版本号
    [root@git01 ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
    12.3.5
    
    #查看解压后的补丁包版本号
    [root@git01 ~]# cat gitlab-12-3-stable-zh/VERSION 
    12.3.5
    
    说明:
    补丁包版本号和安装的GitLab版本号需要一致
    
    #备份英文版GitLab
    [root@git01 ~]# cp -r /opt/gitlab/embedded/service/gitlab-rails{,.bak}
    
    #将中文补丁包的内容覆盖英文版
    [root@git01 ~]# /bin/cp -rf gitlab-12-3-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/
    

    2.4 修改GitLab配置文件/etc/gitlab/gitlab.rb

    我们需要修改GitLab的默认访问域名(因为我们是在内网搭建的GitLab)
    GitLab默认的监听端口为80,但是在企业中,这个端口经常被别的服务占用,所以我们还需要更换端口号

    [root@git01 ~]# cp /etc/gitlab/gitlab.rb{,.bak}
    
    [root@git01 ~]# vim /etc/gitlab/gitlab.rb
    [root@git01 ~]# cat /etc/gitlab/gitlab.rb | sed -n "13p;1105p"
    external_url 'http://192.168.200.31:88'
    nginx['listen_port'] = 88
    
    #重新配置GitLab
    [root@git01 ~]# gitlab-ctl reconfigure
    #以下省略若干。。。
    
    #重启动GitLab
    [root@git01 ~]# gitlab-ctl restart
    ok: run: alertmanager: (pid 4721) 0s
    ok: run: gitaly: (pid 4733) 0s
    ok: run: gitlab-exporter: (pid 4751) 0s
    ok: run: gitlab-workhorse: (pid 4759) 1s
    ok: run: grafana: (pid 4766) 1s
    ok: run: logrotate: (pid 4778) 0s
    ok: run: nginx: (pid 4786) 1s
    ok: run: node-exporter: (pid 4791) 0s
    ok: run: postgres-exporter: (pid 4799) 1s
    ok: run: postgresql: (pid 4809) 0s
    ok: run: prometheus: (pid 4821) 1s
    ok: run: redis: (pid 4846) 0s
    ok: run: redis-exporter: (pid 4852) 1s
    ok: run: sidekiq: (pid 4866) 0s
    timeout: run: unicorn: (pid 2951) 460s
    

    2.5 在宿主机输入http://192.168.200.31:88地址就可以访问了

    image_1f0omc90tddm1usv1rc51u6cdre9.png-272.8kB

    image_1f0omeaip1aba7uk1ktje0pansm.png-247.3kB

    2.6 创建一个项目

    image_1f0os98ki1vp83ut1198sfr1g3r13.png-309.5kB

    image_1f0otihlp1t21jis1ld02fj1spf1g.png-496.7kB

    image_1f0ovg6523av1mrj1s51lc5pd59.png-445.1kB

    2.7 尝试将本地仓库代码推送到私有GitLab

    1.提交到本地仓库

    #创建git仓库并初始化
    [root@git02 ~]# mkdir /mycode
    [root@git02 ~]# cd /mycode/
    [root@git02 mycode]# git init
    初始化空的 Git 版本库于 /mycode/.git/
    
    [root@git02 mycode]# echo "远程提交测试" > test.txt
    [root@git02 mycode]# cat test.txt 
    远程提交测试
    
    #Git全局配置
    [root@git02 mycode]# git config --global user.name "Mr.yang"
    [root@git02 mycode]# git config --global user.email "1773464408@qq.com"
    
    #提交到本地仓库
    [root@git02 mycode]# git add test.txt 
    [root@git02 mycode]# git commit -m "第一次远程提交测试"
    [master(根提交) 06cd049] 第一次远程提交测试
     1 file changed, 1 insertion(+)
     create mode 100644 test.txt
    

    2.推送本地仓库master分支到远程仓库GitLab上

    [root@git02 mycode]# git branch
    * master    #当前在master分支
    
    #添加远程GitLab仓库管理
    [root@git02 mycode]# git remote add origin http://192.168.200.31:88/root/ceshi.git
    [root@git02 mycode]# git remote -v
    origin	http://192.168.200.31:88/root/ceshi.git (fetch)
    origin	http://192.168.200.31:88/root/ceshi.git (push)
    
    #推送本地仓库master分支到远程仓库GitLab上
    [root@git02 mycode]# git push origin master
    Username for 'http://192.168.200.31:88': root
    Password for 'http://root@192.168.200.31:88': ywb971108
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To http://192.168.200.31:88/root/ceshi.git
     * [new branch]      master -> master
    

    image_1f0p23hc211n81l852q3or4ljv9.png-421.3kB

    image_1f0p2h6t0100is0lk5g1qjl675m.png-309.3kB

    3.推送本地仓库dev分支到远程仓库GitLab上

    #创建并接环到dev分支
    [root@git02 mycode]# git branch dev
    [root@git02 mycode]# git checkout dev
    切换到分支 'dev'
    [root@git02 mycode]# git branch
    * dev
      master
    
    [root@git02 mycode]# echo "dev远程提分支交测试" >> test.txt 
    [root@git02 mycode]# cat test.txt 
    远程提交测试
    dev远程提分支交测试
    
    [root@git02 mycode]# git add test.txt 
    [root@git02 mycode]# git commit -m "dev远程提分支交测试"
    [dev ab5e6c0] dev远程提分支交测试
     1 file changed, 1 insertion(+)
    
    #推送本地仓库dev分支到远程仓库GitLab上
    [root@Git01 mycode]# git push origin dev
    Username for 'http://192.168.200.53:8888': root
    Password for 'http://root@192.168.200.53:8888': ywb971108
    对象计数中: 3, 完成.
    写入对象中: 100% (3/3), 306 bytes | 0 bytes/s, 完成.
    Total 3 (delta 0), reused 0 (delta 0)
    remote: 
    remote: To create a merge request for dev, visit:
    remote:   http://192.168.200.53:8888/root/yunjisuan/merge_requests/new?merge_request%5Bsource_branch%5D=dev
    remote: 
    To http://192.168.200.53:8888/root/yunjisuan.git
     * [new branch]      dev -> dev
    

    image_1f0p32nrr11k71l0itpctpv1ni813.png-325kB

    image_1f0p5h403gtkuat1r8348r1v6e34.png-153.9kB

    4.推送本地仓库标签V1.0到远程仓库GitLab上

    [root@git02 mycode]# echo "打个标签" >> test.txt
    [root@git02 mycode]# cat test.txt 
    远程提交测试
    dev远程提分支交测试
    打个标签
    
    [root@git02 mycode]# git add test.txt 
    [root@git02 mycode]# git commit -m "标签测试"
    [dev 21ccadf] 标签测试
     1 file changed, 1 insertion(+)
    
    #创建标签
    [root@git02 mycode]# git tag v1.0
    [root@git02 mycode]# git tag
    v1.0
    
    #推送本地仓库标签V1.0到远程仓库GitLab上
    [root@git02 mycode]# git push origin v1.0
    Username for 'http://192.168.200.31:88': root
    Password for 'http://root@192.168.200.31:88': 
    Counting objects: 5, done.
    Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To http://192.168.200.31:88/root/ceshi.git
     * [new tag]         v1.0 -> v1.0
    

    image_1f0p5n6hgec9qjd1noo12cn19i23h.png-298.5kB

    image_1f0p5nr2i19q81h7k1jaa1rj8nsn3u.png-135.2kB

    2.8 尝试在本地仓库克隆GitLab的项目dev分支到本地仓库

    #创建Git工作目录
    [root@git03 ~]# mkdir /mycode
    [root@git03 ~]# cd /mycode/
    
    
    #克隆远程仓库GitLab的dev分支
    [root@git03 mycode]# git clone -b dev http://192.168.200.31:88/root/ceshi.git
    正克隆到 'ceshi'...
    remote: Enumerating objects: 9, done.
    remote: Counting objects: 100% (9/9), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 9 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (9/9), done.
    
    
    #查看结果
    [root@git03 mycode]# ls
    ceshi
    [root@git03 mycode]# cd ceshi/
    
    [root@git03 ceshi]# ls
    test.txt
    [root@git03 ceshi]# cat test.txt 
    远程提交测试
    dev远程提分支交测试
    

    2.9 配置GitLab的SSH密钥连接方式

    #在Git03客户端生成密钥对 
    [root@git03 ~]# ssh-keygen -t rsa
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:sTppVfJqIMMJiqM5GIdx8Aa3mVp3EJUuO/gsgQgsmbY root@git03
    The key's randomart image is:
    +---[RSA 2048]----+
    |o . oo..         |
    | = + ..          |
    |ooX ... o .      |
    |*@ +.o.  *       |
    |%.o.=o. S .      |
    |=Eo oo = .       |
    |=  + .= o        |
    | .. o. o         |
    |   .             |
    +----[SHA256]-----+
    
    #公钥内容,复制到GitLab上
    [root@git03 ~]# cat /root/.ssh/id_rsa.pub
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2Sykv/orgH0qzPYj7K/B9j+AQsgmSTj5yIGjDynJL/F2kkRAIFnZdJw39XwZN0h6Eh4wVLPadK0+RSVzMRnM6hE9V4Q5LkXIvcD0LmtMPsDcAfgJhcxrzsj0i+dB1qv0oe97TLujzSoQgl6ILfm5GSPWohmDIJY1UvBZpahB5FF+hoq+8vLrqDy1q25w5l8UywGDOcj43bd7oLNceJfpVlgsG7IC1q8UBawy7NnOzwSBKAV5evnKhcLP6DqtkeVKJKJdGgkEMrvsn6SkfyQ0xt/4EZ+NCQahfLMqyXez+sJk7pan1EAMKJX6Pdd6pV4dwL2GhJ6LE4dP8VfBkvV8X root@git03
    

    在浏览器端打开GitLab http://192.168.200.31:88

    image_1f0rca35lejp1eadsq1mp61ldv9.png-238.5kB

    image_1f0rcd13brv56cm89s1v9vml3m.png-484.6kB

    image_1f0rcdttb17fn18s11cn37451k2s13.png-118.3kB

    2.10 利用ssh方式克隆GitLab上的一个项目

    [root@git03 ~]# mkdir /mycode
    [root@git03 ~]# cd /mycode/
    
    #将远程GitLab的Linux分支克隆到本地
    [root@git03 mycode]# git clone -b master git@192.168.200.31:root/ceshi.git
    正克隆到 'ceshi'...
    remote: Enumerating objects: 9, done.
    remote: Counting objects: 100% (9/9), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 9 (delta 0), reused 0 (delta 0)
    接收对象中: 100% (9/9), done.
    
    [root@git03 mycode]# ls
    ceshi
    [root@git03 mycode]# ls ceshi/
    test.txt
    [root@git03 mycode]# cat ceshi/test.txt 
    远程提交测试
    
    [root@Git01 yunjisuan]# git branch
    * dev
    

    3. 配置GitLab邮件服务

    配置邮箱服务的用途:

    • 有合并请求时,邮件通知
    • 账号注册时,邮件验证
    • 修改密码时,通过邮件修改

    配置步骤:

    • 开启QQ邮箱的smtp服务
    • 修改gitlab配置
    • 测试邮件服务是否正常

    3.1 开启GitLab服务postfix服务并开启QQ邮箱的smtp服务

    [root@git01 ~]# systemctl start postfix
    [root@git01 ~]# systemctl enable postfix
    [root@git01 ~]# systemctl status postfix | grep Active
       Active: active (running) since 五 2021-03-12 09:47:29 CST; 3 days ago
    

    image_1f0rdinpjo9n1oug94ceml13sk1g.png-416.5kB

    image_1f0rdkhcp174qrff1ge61itvgef1t.png-373.5kB

    3.2 修改gitlab配置文件/etc/gitlab/gitlab.rb

    [root@git01 ~]# vim /etc/gitlab/gitlab.rb
    [root@git01 ~]# cat -n /etc/gitlab/gitlab.rb | sed -n '53p;559,567p;725p'
        53	gitlab_rails['gitlab_email_from'] = '1773464408@qq.com'   #GitLab默认邮箱
       559	gitlab_rails['smtp_enable'] = true    #开启功能
       560	gitlab_rails['smtp_address'] = "smtp.qq.com"   #QQ的smtp服务
       561	gitlab_rails['smtp_port'] = 465
       562	gitlab_rails['smtp_user_name'] = "1773464408@qq.com"   #smtp用户邮箱
       563	gitlab_rails['smtp_password'] = "nhfbyhkzwxkpdafh"     #开通QQsmtp时返回的字符串
       564	gitlab_rails['smtp_domain'] = "qq.com"                 #smtp域名
       565	gitlab_rails['smtp_authentication'] = "login"
       566	gitlab_rails['smtp_enable_starttls_auto'] = true
       567	gitlab_rails['smtp_tls'] = true
       725	user['git_user_email'] = "1773464408@qq.com"           #git用户邮箱
    
    #重新加载GitLab配置文件
    [root@git01 ~]# gitlab-ctl reconfigure
    #以下省略若干。。。
    
    #重启动GitLab服务
    [root@git01 ~]# gitlab-ctl restart
    ok: run: alertmanager: (pid 35165) 0s
    ok: run: gitaly: (pid 35178) 0s
    ok: run: gitlab-exporter: (pid 35195) 0s
    ok: run: gitlab-workhorse: (pid 35197) 1s
    ok: run: grafana: (pid 35206) 0s
    ok: run: logrotate: (pid 35217) 1s
    ok: run: nginx: (pid 35225) 0s
    ok: run: node-exporter: (pid 35230) 1s
    ok: run: postgres-exporter: (pid 35239) 0s
    ok: run: postgresql: (pid 35247) 0s
    ok: run: prometheus: (pid 35256) 0s
    ok: run: redis: (pid 35263) 0s
    ok: run: redis-exporter: (pid 35270) 1s
    ok: run: sidekiq: (pid 35275) 0s
    ok: run: unicorn: (pid 35283) 0s
    
    #进入GitLab控制台进行邮件发送测试
    [root@git01 ~]# gitlab-rails console      #开启GitLab控制台
    --------------------------------------------------------------------------------
     GitLab:       12.3.5 (2417d5becc7)
     GitLab Shell: 10.0.0
     PostgreSQL:   10.9
    --------------------------------------------------------------------------------
    Loading production environment (Rails 5.2.3)
    irb(main):001:0> Notify.test_email('1773464408@qq.com','this is title','hello,杨哥!').deliver_now
    Notify#test_email: processed outbound mail in 64.1ms
    #以下省略。。。
    
    特别说明:
    Notify.test_email('收件人邮箱','邮件标题','邮件内容')
    

    image_1f0ret0ab11501oje1ihd1cge1l0g2a.png-205.1kB

    image_1f0retslm1674ihm1ka315t11b9d2n.png-75.7kB

    4. GitLab的账号注册及分组

    4.1 账户注册测试 http://192.168.200.31:88

    image_1f0rfc6ta1f0jts71g1di561k1j34.png-266.9kB

    image_1f0rfdr9b1pqddl14u21nkfak53h.png-308.1kB

    我们再注册的时候,用的是乱写的邮箱都能注册并登陆;
    显然这在企业里是绝对不允许的。

    4.2 开启GitLab邮箱验证功能

    1.重新登陆管理员用户,我们进行如下操作

    image_1f0rfmavukrq19e6tgb1q2kcnv3u.png-392.8kB

    2.然后我们退出管理员的登陆,重新进行重新注册

    image_1f0rfveg0g4e1egg1aof50gbfg4b.png-296.7kB

    image_1f0rg1rqu16es1f3p10m01oipl44o.png-110.9kB

    3.由上图,我们发现,此时注册用户,已经进不去GitLab了;
    这是因为GitLab已经自动发了一封邮件给注册用户的邮箱,需要用户进行邮件确认后,方可登陆

    image_1f0rgjqri12gg1dn21jc41nrm109t6c.png-128.6kB

    image_1f0titn8b9371m851rkm2km1kgn9.png-273.3kB

    image_1f0tiula7bk063khl21sm31ut5m.png-343.5kB

    4.3 创建一个项目组

    image_1f0tk0scf1t0e1u40k4i1jopbqm1t.png-292kB

    image_1f0tk8kj5ll51esa1bsgrovet2a.png-444kB

    image_1f0tkk305trp1m171sgd4brpri2n.png-369.1kB

    image_1f0tko26tist1d7l1l0i10vi62k34.png-378.6kB

    • Guest访客: 只能发表评论,不能读写项目库
    • Reporter报告者: 只能克隆代码(读),不能提交代码(修改)(产品测试)
    • Developer开发人员: 可以进行代码的读写(普通程序员)
    • Master主程序员: 可以添加项目成员,添加标签,创建和保护分支(产品经理)
    • Owner所有者: 拥有所有权限(一般不用此权限)

    添加权限成功后,GitLab会自动邮件通知这个用户

    image_1f0tkqnp4qkm8tp36h1cd218p23h.png-219.6kB

    4.4 给项目组创建一个新项目

    image_1f0tku1k4rj4de312h818j7nli3u.png-360.1kB

    image_1f0tl6o3u21inlq14hcdr514ss4b.png-432.2kB

    4.5 进行组成员的项目提交和克隆测试

    #进行组成员代码提交测试
    [root@git03 ~]# mkdir /mycode
    [root@git03 ~]# cd /mycode/
    
    #进行克隆测试
    [root@git03 mycode]# git clone http://192.168.200.31:88/uat/ceshi03.git
    正克隆到 'ceshi03'...
    Username for 'http://192.168.200.31:88': 3523916661@qq.com
    Password for 'http://3523916661@qq.com@192.168.200.31:88': 123456789
    warning: 您似乎克隆了一个空版本库。
    
    [root@git03 mycode]# ls
    ceshi03
    [root@git03 mycode]# ls ceshi03/
    [root@git03 mycode]#
    
    #推送代码到远程GitLab
    [root@git03 mycode]# cd ceshi03/
    [root@git03 ceshi03]# echo "组成员项目提交测试" >> test.txt
    [root@git03 ceshi03]# cat test.txt 
    组成员项目提交测试
    
    [root@git03 ceshi03]# git add test.txt 
    [root@git03 ceshi03]# git config --global user.name "喜羊羊"
    [root@git03 ceshi03]# git config --global user.email "3523916661@qq.com"
    [root@git03 ceshi03]# git commit -m "组成员第一次提交测试"
    [master(根提交) 16a13af] 组成员第一次提交测试
     1 file changed, 1 insertion(+)
     create mode 100644 test.txt
    
    [root@git03 ceshi03]# git push -u origin master
    Username for 'http://192.168.200.31:88': 3523916661@qq.com
    Password for 'http://3523916661@qq.com@192.168.200.31:88': 
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To http://192.168.200.31:88/uat/ceshi03.git
     * [new branch]      master -> master
    分支 master 设置为跟踪来自 origin 的远程分支 master。
    

    image_1f0tnco2ndnb66e2t01hci6is4o.png-422.7kB

    image_1f0tne4p31iqj10bh1f3u15dv32r55.png-313.3kB

  • 相关阅读:
    电脑族,记忆力下降怎么办
    asp.net入門經典讀書筆記
    GridView
    如何学习开源库
    i++与++i区别使用
    计算二次方程根
    MySQL Dump/Restore
    利用SetConsoleCursorPosition控制命令行光标位置
    How to upgrade to Ubuntu 10.10 Maverick Meerkat from ubuntu 10.04 lucid, karmic| Desktop & Server
    通过一条命令获取最快的 apt 源镜像
  • 原文地址:https://www.cnblogs.com/ywb123/p/14546503.html
Copyright © 2011-2022 走看看