GitLab 是一个利用 Ruby on Rails 开发的开源应用程序, 实现一个 Git 项目仓库, 可通过 web 界面进行访问公开的或者私人的项目 GitLab 拥有与 GitHub 类似的功能, 能够浏览源代码, 管理缺陷和注释. 可以管理团队对仓库的访问, 他非常易于浏览提交过的版本并提供一个文件历史库. 他还提供一个代码片段收集功能可以轻松实现代码复用, 便于日后有需要的时候进行查找.
关于 GitLab 的详细信息建议参考 GitLab 官网 https://about.gitlab.com/ 进行详细的了解!
博文大纲:
一, 准备环境
二, 安装 GitLab
三, 汉化 GitLab
四, GitLab 基本操作
五, 远端库的基本操作
六, 重置 GitLab 管理员密码
一, 准备环境
操作系统 | 内存 | CPU |
---|---|---|
centos 7 | 4G 以上 | 双核 |
二, 安装 GitLab
1) 安装 GitLab 所需依赖
[[email protected] ~]# yum -y install epel-release curl openssh-server openssh-clients postfix cronie policycoreutils-python
2) 获取 GitLab 的 RPM 软件包
方法一: 通过清华大学的开源镜像站获取软件包 (推荐)
[[email protected] ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
方法二: 通过 GitLab 官网来获取软件包 (网络稳定时使用)
[[email protected] ~]# wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm/download.rpm
3) 安装 GitLab
- [[email protected] ~]# rpm -ivh GitLab-ce-12.3.5-ce.0.el7.x86_64.rpm
- # 安装时间较长, 耐心等待, 安装过程中会出现 GitLab 的 logo
4) 修改 GitLab 的 url 并执行 reconfigure
- [[email protected] ~]# VIM /etc/GitLab/GitLab.rb #修改 GitLab 的配置文件
- .................................... #省略部分内容
- external_url 'http://192.168.1.8' #将此处改为本机的 IP 地址, 便于访问
- [[email protected] ~]# GitLab-ctl reconfigure #重新配置 GitLab, 就算不修改配置文件, 也需要在安装后重新配置 GitLab
5)Web 页面访问测试
如图:
可以访问到表示没有问题, 但是都是英文界面, 对于英文不好的我表示很是头疼, 还好可以通过一些方法使其汉化, 方法如下!
三, 汉化 GitLab
1) 获取 GitLab 汉化补丁包 (如果不需要汉化, 则跳过此步骤即可)
- [[email protected] ~]# head -1 /opt/GitLab/version-manifest.txt #查看 GitLab 的版本
- GitLab-ce 12.3.5
- [[email protected] ~]# Git clone https://gitlab.com/xhang/gitlab.git -b v12.3.5-zh
- # 获取汉化补丁包 (注意需与 GitLab 的版本保持一致)
- [[email protected] ~]# cd GitLab/ #进入刚才 clone 下来的 GitLab 目录
- [[email protected] GitLab]# Git diff v12.3.5 v12.3.5-zh> /root/v12.3.5-zh.diff
- # 用 diff 将英文原版与中文版的对比生成. diff 文件
2) 将中文补丁导入 GitLab
- [[email protected] GitLab]# GitLab-ctl stop #停止 GitLab
- [[email protected] GitLab]# yum -y install patch
- [[email protected] GitLab]# patch -d /opt/GitLab/embedded/service/GitLab-Rails -p1 <../v12.3.5-zh.diff
- #将刚才的 diff 文件做为补丁导入到 GitLab 中
- # 该命令执行过程中, 一路回车跳过即可
- [[email protected] GitLab]# GitLab-ctl start #启动 GitLab
- [[email protected] GitLab]# GitLab-ctl reconfigure #重新配置 GitLab
3)Web 页面再次访问
如图:
四, GitLab 基本操作
1) 配置 SSH 方式免密登录
- [[email protected] ~]# SSH-keygen -t rsa -C "[email protected]"
- # 生成秘钥对, 一路回车即可,"-C" 后是自己的邮箱
- [[email protected] ~]# cat ~/.SSH/id_rsa.pub #查看生成的公钥并复制
- SSH-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvNm+nXc59ugb0SGr9iMHDSFjvmdSJk0ORuX3hbjt822Y2ofXysUrIuBSQ1Jn0Rss/LPU54K32i4bIDsa/jD9gIpN/GqU+YP1MQ9bEw3YVUONAs+nYeWJWahQ1rMTeM0HC9aKvNTrNsOqrXIboJymBrs6Odt+1NnZsYHMwA/KlpYCFsi0HQgBzsLbrD5v++cIDTvM/V4rMq6fqFsfWoYYMHWc8JeNMl/aWJV1RhJpt7wm17FEv3XiH+wyoef5ZYI60IkH5qMJkjWhKcRXCWG5SH3nphUb1fmktB4DH92TW/EGw///VQEnE7tkpNjyJpOTXDuHnEk2tw43cctDN2sJH [email protected]
回到 Web 页面, 操作如下:
2) 创建一个基本库
如图:
回到服务器上输入测试的命令, 如下:
- [[email protected] ~]# Git config --global user.name "Administrator"
- [[email protected] ~]# Git config --global user.email "[email protected]"
- [[email protected] ~]# Git clone [email protected]:root/test01.Git
- # 克隆到本地, 根据提示输入 "yes" 即可!
- [[email protected] ~]# cd test01/
- [[email protected] test01]# touch README.md
- [[email protected] test01]# Git add README.md
- [[email protected] test01]# Git commit -m "add README"
- [[email protected] test01]# Git push -u origin master
刷新 Web 页面即可, 如图:
五, 远端库的基本操作
当你从远端仓库克隆时, 实际上 Git 自动把本地的 master 分支和远端的 master 分支对应起来了, 并且远程仓库的默认名称是 origin.
1) 查看关联的远程库的信息
- [[email protected] test01]# Git remote #简洁信息
- origin
- [[email protected] test01]# Git remote -v #详细信息
- origin [email protected]:root/test01.Git (fetch)
- origin [email protected]:root/test01.Git (push)
2) 推送分支到远程仓库
- [[email protected] test01]# Git checkout -b dev #创建并切换到 dev 分支
- [[email protected] test01]# Git push origin dev #将本地的分支推送到远程仓库
刷新页面之后, 如图:
3) 解决多人协作产生的问题
当我们整个小组对同一个分支进行开发时, 如果在你提交之前, 你的同事已经修改了分支的内容并推送到远端仓库, 而碰巧你也对同样的文件做了修改, 并试图推送, 那么会推送失败, 因为你的同事的最新提交的数据和你试图提交的数据有冲突 (你本地的内容比远端仓库的旧了), 解决的办法会在提示你推送失败的返回信息中给出, 这里我们模拟一下这一过程.
- # 进入另一个目录, 克隆远端仓库, 模拟多人操作
- [[email protected] test01]# cd /tmp
- [[email protected] tmp]# Git clone [email protected]:root/test01.Git
- [[email protected] tmp]# cd test01/
- [[email protected] test01]# Git checkout -b etc #重新创建一个分支并切换
- [[email protected] test01]# echo -e "touch /tmp/test01"> tmp.txt
- [[email protected] test01]# Git add tmp.txt
- [[email protected] test01]# Git commit -m "commmit from /tmp"
- [[email protected] test01]# Git push origin etc #将新修改的内容推送到远端
回到 Web 界面进行刷新, 即可看到新提交的分支:
- # 上述操作是在 / tmp 目录下执行的, 接下来的操作在 / root 目录下执行:
- [[email protected] test01]# cd /root/test01/
- [[email protected] test01]# Git checkout -b etc
- [[email protected] test01]# echo "touch /root/test01.txt"> root.txt
- [[email protected] test01]# Git add root.txt
- [[email protected] test01]# Git commit -m "commit from /root"
- [[email protected] test01]# Git push origin etc
- # 再次推送, 将会出现以下错误
- To [email protected]:root/test01.Git
- ! [rejected] etc -> etc (fetch first)
error: 无法推送一些引用到 '[email protected]:root/test01.git'
提示: 更新被拒绝, 因为远程版本库包含您本地尚不存在的提交. 这通常是因为另外
提示: 一个版本库已推送了相同的引用. 再次推送前, 您可能需要先合并远程变更
提示:(如'git pull').
提示: 详见'git push --help' 中的'Note about fast-forwards' 小节.
- [[email protected] test01]# Git pull origin etc #将远端的 etc 分支 pull 到本地
- [[email protected] test01]# ls #可以看出在 / tmp 目录下提交的文件就存在了
- README.md root.txt tmp.txt
- [[email protected] test01]# Git push origin etc #然后再次将本地的 dev 分支推送到 GitLab, 即可成功
再次刷新 Web 页面, etc 分支下就会有了我们在 / tmp 目录和 / root 目录下提交的所有内容, 如图:
4) 合并远程分支
- [[email protected] test01]# Git checkout master #切换至 master 分支
- [[email protected] test01]# Git merge etc #合并 etc 分支
- [[email protected] test01]# ls #查看合并后的分支下内容
- README.md root.txt tmp.txt
- [[email protected] test01]# Git add .
- [[email protected] test01]# Git commit -m "提交"
- [[email protected] test01]# Git push origin master #推送到远端版本库
再次刷新 Web 页面, 如图:
5) 删除远程的分支
- [[email protected] test01]# Git branch -d etc #删除本地的 dev 分支
- [[email protected] test01]# Git branch -r -d origin/etc #删除指定的远程分支
- [[email protected] test01]# Git push origin :etc #将删除的分支提交到远程版本库中
- [[email protected] test01]# Git branch -d dev
- [[email protected] test01]# Git branch -r -d origin/dev
- [[email protected] test01]# Git push origin :dev #同上
再次刷新 Web 页面, 如下:
六, 重置 GitLab 管理员密码
- [[email protected] ~]# GitLab-Rails console production #必须是 root 用户登录服务器执行该命令
- irb(main):001:0> user = User.where(id: 1).first #id 为 1 的是超级管理员
- irb(main):002:0> user.password = 'yourpassword' #密码必须至少 8 个字符
- irb(main):003:0> user.save! #保存用户修改信息, 如没有问题返回 true
- irb(main):004:0> exit #退出
至此, 再次登录, 就需要使用新密码 yourpassword 进行登录了.
来源: http://www.bubuko.com/infodetail-3412404.html