第1章 Git服务器
Git中人人都是中心,可以使用GitHub、码云等公共代码仓库(个人选择),或者GitLab私有仓库(企业选择)进行数据交互。
1.1 搭建Gitlab私有仓库
提示:官方安装文档:https://about.gitlab.com/installation/#centos-7
1.1.1 下载gitlab rpm包
清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
1.1.2 查看主机端口是否被占用
[root@centos7 ~]# netstat -lntup | egrep "9100|9168|8080|80|8060|9121|9090|9187" [root@centos7 ~]# [root@centos7 ~]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program na tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1165/sshd tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 3291/zabbix_ag tcp6 0 0 :::22 :::* LISTEN 1165/sshd
注意:如果有以上端口被占用则要关掉相关占用端口的服务或杀掉相关进程后方可进行下面的步骤。
1.1.3 上传rpm包后本地安装
[root@centos7 ~]# cd /server/tools/ [root@centos7 tools]# yum localinstall -y gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
1.1.4 初始化Gitlab(仅执行一次即可)
[root@centos7 tools]# gitlab-ctl reconfigure
1.1.5 开启及关闭Gitlab服务
注意:此处不需要再手动开启gitlab服务,因为初始化后gitlab服务会自动开启。
[root@centos7 ~]# gitlab-ctl start [root@centos7 ~]# gitlab-ctl stop
1.1.6 查看Gitlab状态
[root@centos7 tools]# gitlab-ctl status run: gitaly: (pid 126041) 2095s; run: log: (pid 125755) 2130s run: gitlab-monitor: (pid 126097) 2093s; run: log: (pid 125977) 2107s run: gitlab-workhorse: (pid 126052) 2095s; run: log: (pid 125776) 2124s run: logrotate: (pid 125828) 2121s; run: log: (pid 125827) 2121s run: nginx: (pid 125785) 2123s; run: log: (pid 125784) 2123s run: node-exporter: (pid 125917) 2117s; run: log: (pid 125916) 2117s run: postgres-exporter: (pid 126082) 2094s; run: log: (pid 125956) 2109s run: postgresql: (pid 125570) 2163s; run: log: (pid 125569) 2163s run: prometheus: (pid 126069) 2094s; run: log: (pid 125897) 2119s run: redis: (pid 125504) 2174s; run: log: (pid 125503) 2174s run: redis-exporter: (pid 125942) 2111s; run: log: (pid 125941) 2111s run: sidekiq: (pid 125739) 2135s; run: log: (pid 125738) 2135s run: unicorn: (pid 125704) 2137s; run: log: (pid 125703) 2137s
1.2 Gitlab Web界面使用
通过浏览器访问页面,设置初始密码,其他操作类似GitHUB。
1.2.1 登录Gitlab
1.2.1.1 首次登录需要创建root用户密码(密码必须大于8位)
1.2.1.2 设置完成root密码后登录
1.2.2 创建项目工程
1.2.3 设置SSH-Key免密钥登录
1.2.3.1 在客户端制作SSH密钥
# 生成密钥 [root@web01 ~]# ssh-keygen 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: 2a:32:f4:3d:fc:b6:e0:d8:cc:56:a3:20:12:59:31:cb root@web01 The key's randomart image is: +--[ RSA 2048]----+ | o. | | ..o | | oE | |o | | .. S | |.....o .o | | .o.o.*o . | | o Bo+. | | ..=.o. | +-----------------+
# 查看密钥 [root@web01 ~]# cat /root/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZqNyubQc+uR2Mh1nR8D9NIrlyCCRAJN2iVPyThbp9No5FF6U2ix9Ebim0UysryEqsTZFvpOwnkBopDwycyYZYOsSSC/tWSo7OuoxGefRRrw ...省略部分内容... [root@web01 ~]# cat /var/opt/gitlab/.ssh/authorized_keys
1.2.3.2 在Web页面配置SSH
1.2.3.3 客户端免密钥访问gitlab
[root@web01 ~]# ssh [email protected] PTY allocation request failed on channel 0 Welcome to GitLab, Administrator! # 显示可以登录 Connection to 10.0.0.211 closed.
提示:此处登录后会直接退出,是因为git用户无权限对服务器进行操作。
1.2.4 对新建的项目工程进行编辑
1.2.4.1 主页面会有客户端配置安装的帮助文档:
1.2.4.2 在客户端进行配置
# 设置git全局配置 [root@web01 ~]# git config --global user.name "ly" [root@web01 ~]# git config --global user.email "[email protected]" [root@web01 ~]# git config --global color.ui true
# 首次需使用clone命令进行全量克隆 [root@web01 ~]# git clone [email protected]:root/39team.git Cloning into '39team'... warning: You appear to have cloned an empty repository. [root@web01 ~]# ls 39team
# 创建README文件 [root@web01 ~]# cd 39team [root@web01 39team]# touch README.md [root@web01 39team]# git add README.md [root@web01 39team]# git commit -m "add README" [master (root-commit) aa16865] add README 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md [root@web01 39team]# git push -u origin master # 推送本地文件到gitlab上 Counting objects: 3, done. Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:root/39team.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
1.2.4.3 Web页面编辑README文件
提示:此处使用Markdown语法进行编辑。
1.2.4.4 编辑后效果
1.2.4.5 将README文件拉取到本地
[root@web01 39team]# git pull remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From 10.0.0.211:root/39team 08720a0..80a98bf master -> origin/master Updating 08720a0..80a98bf Fast-forward README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
第2章 GITHUB使用
Github顾名思义是一个Git版本库的托管服务,是目前全球最大的软件仓库,拥有上百万的开发者用户,也是软件开发和寻找资源的最佳途径,Github不仅可以托管各种Git版本仓库,还拥有了更美观的Web界面,您的代码文件可以被任何人克隆,使得开发者为开源项贡献代码变得更加容易,当然也可以付费购买私有库,这样高性价比的私有库真的是帮助到了很多团队和企业。
提示:具体使用方法见博客http://blog.oldboyedu.com/git/

我的微信
如果有技术上的问题可以扫一扫我的微信