GitLab 概述
GitLab 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的 Git 項目倉庫,可通過 Web界面迚行訪問公開的或者私人項目。Ruby on Rails 是一個可以使你開發、部署、維護 web 應用程序變得簡單的框架。
GitLab 擁有不 Github 類似的功能,能夠瀏覽源代碼,管理缺陷和註釋。可以管理團隊對倉庫的訪問,它非常易亍瀏覽提交過的版本並提供一個文件歷叱庫。它還提供一個代碼片段收集功能可以輕鬆實現代碼複用,便於日後有需要的時候進行查找。
GitLab和GitHub的區別
相同點:
- 都是基於web的Git倉庫
- 都提供了分享開源項目的平臺,爲開發團隊提供了存儲、分享、發佈和合作開發項目的中心化於存儲的場所。
不同點:
- GitHub使用私有倉庫,是需要付費的。GitLab可以在上邊創建私人的免費倉庫
- GitLab讓開發團隊對他們的代碼倉庫擁有更多的控制,而GitHub則是代碼託管的首選
- 相比於GitHub,它有不少的特色:允許免費設置倉庫權限;允許用戶選擇分享一個 project 的部分代碼;允許用戶設置 project 的獲取權限,進一步的提升安全性;可以設置獲取到團隊整體的改進進度;通過 innersourcing 讓不在權限範圍內的人訪問不到該資源。
系統環境
[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@node1 ~]# uname -r
3.10.0-862.el7.x86_64
#虛擬機內存爲4G
[root@node1 ~]# free -h
total used free shared buff/cache available
Mem: 3.7G 2.8G 475M 30M 408M 542M
Swap: 2.0G 1.5G 556M
#防火牆已關閉
[root@node1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
#selinux設置爲Disabled
[root@node1 ~]# getenforce
Disabled
搭建GitLab
安裝GitLab所需的組件
yum install curl policycoreutils openssh-server openssh-clients postfix -y
開啓Postfix發送郵件
[root@node1 ~]# systemctl start postfix
#設置開機自啓
[root@node1 ~]# systemctl enable postfix
安裝GitLab
下載GitLab的RPM包,再複製到虛擬機
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.2.3-ce.0.el7.x86_64.rpm
rpm -ivh gitlab-ce-10.2.3-ce.0.el7.x86_64.rpm
修改gitlab域名
[root@node1 ~]# vim /etc/gitlab/gitlab.rb
#修改爲IP地址
external_url 'http://192.168.231.130'
修改配置文件後,重新配置GitLab
[root@node1 ~]# gitlab-ctl reconfigure
查看gitlab狀態
登錄GitLab
在瀏覽器中輸入虛擬機IP地址192.168.231.130
設置密碼
登錄
GitLab的簡單使用
1.創建羣組
2.創建用戶
選擇管理員區域->Users->New user
填寫信息
163郵箱會收到賬號創建成功的郵件
點擊設置密碼,必須8位以上
創建成功
3.創建項目
黃色警號:如果想使用https,就添加一個SSH key,這裏先不添加
創建一個html文件並提交(root用戶登錄)
設置gitlab用戶名和郵箱
[root@node1 ~]# git config --global user.name "realxw"
[root@node1 ~]# git config --global user.email "[email protected]"
獲取創建的項目(複製url):
git 相關概念
git 是一種版本控制系統,是一個命令,是一種工具
gitlib 是用於實現 git 功能的開發庫
github 是一個基於 git 實現的在線代碼託管倉庫,包含一個網站界面,向互聯網開放
gitlab 是一個基於 git 實現的在線代碼倉庫託管軟件,一般用於在企業內部網絡搭建 git 私服
注: gitlab-ce 社區版 ; gitlab-ee 是企業版
git常用命令及使用
安裝git
# yum install git -y
git config --global user.name "xxx" #設置全局用戶名
git config --global user.email [email protected] #設置全局郵箱
git config --global --list #列出用戶全局設置
git add index.html #添加文件到暫存區
git commit -m "describe" #提交文件到工作區
git status #查看工作區的狀態
git push #提交代碼到服務器
git pull #獲取代碼到本地
git log #查看操作日誌
git reset --hard HEAD^ # git版本回滾,HEAD爲當前版本, 加一個^爲上一個,^^爲上上一個版本
git reflog #獲取毎次提交的ID,可以使用--hard根據提交的ID進行版本回退
git reset --hard ID #回退到指定id的版本
# git branch #查看當前所處的分支
git checkout -- file #從服務器更新某個文件覆蓋本地的文件
演示:
git add , git commit -m ,git push origin master
#切換到項目目錄
[root@node1 test]# cd real-web/
[root@node1 real-web]# ls
index.html
#創建上傳文件
[root@node1 real-web]# echo "hello" > hello.txt
[root@node1 real-web]# ls
hello.txt index.html
#添加文件到暫存區
[root@node1 real-web]# git add hello.txt
#提交文件到工作區
[root@node1 real-web]# git commit -m 'add hello.txt'
[master f5584a6] add hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt
#提交到服務器(orgin後邊跟分支,主分支)
[root@node1 real-web]# git push origin master
Username for 'http://192.168.231.130': root
Password for 'http://[email protected]':
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.231.130/root/real-web.git
b04498e..f5584a6 master -> master
網頁刷新查看:
git log,查看提交日誌
git reset --hard ID,git reflog,git branch
修改hello.txt,提交,並恢復到修改前的版本
#修改hello.txt
[root@node1 real-web]# echo "123" > hello.txt
[root@node1 real-web]# cat hello.txt
123
#提交項目
[root@node1 real-web]# git add hello.txt
[root@node1 real-web]# git commit -m 'update hello.txt'
[master ea0fa54] update hello.txt
1 file changed, 1 insertion(+), 1 deletion(-)
#查看當前分支
[root@node1 real-web]# git branch
* master
[root@node1 real-web]# git push origin master
Username for 'http://192.168.231.130': root
Password for 'http://[email protected]':
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 276 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.231.130/root/real-web.git
088c115..ea0fa54 master -> master
#獲取每次提交的ID
[root@node1 real-web]# git reflog
ea0fa54 HEAD@{0}: commit: update hello.txt
088c115 HEAD@{1}: commit: update index.html
f5584a6 HEAD@{2}: commit: add hello.txt
b04498e HEAD@{3}: clone: from http://192.168.231.130/root/real-web.git
[root@node1 real-web]# cat hello.txt
123
#恢復hello.txt到前一個版本
[root@node1 real-web]# git reset --hard f5584a6
HEAD is now at f5584a6 add hello.txt
#恢復
[root@node1 real-web]# cat hello.txt
hello