Gitlab 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的 Git 項目倉庫,可通過Web 界面進行訪問公開的或者私人的項目 Gitlab 擁有與 Github 類似的功能,能夠瀏覽源代碼,管理缺陷和註釋。可以管理團隊對倉庫的訪問,他非常易於瀏覽提交過的版本並提供一個文件歷史庫。他還提供一個代碼片段收集功能可以輕鬆實現代碼複用,便於日後有需要的時候進行查找
一、環境準備
如果是測試環境,其內存建議2G及以上,可以去清華開源鏡像站下載所需gitlab版本,其安裝後,會自動安裝nginx提供web界面,所以要避免80端口占用。
二、安裝部署gitlab
1. 安裝gitlab
[root@git /]# mkdir git
[root@git /]# cd git/
[root@git git]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
[root@git git]# rpm -ivh gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
#當gitlab安裝完畢後會有一個大狐狸頭
#由於我不打算做域名解析,所以需要修改其配置文件
[root@git git]# vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.171.134' # 將原本的域名改爲本機IP
[root@git /]# gitlab-ctl reconfigure #重新配置gitlab,就算不修改配置文件,也需要在安裝後重新配置gitlab,此處會等一會
[root@git /]# netstat -anput | grep -w 80 #確定nginx在監聽80端口
2.配置gitlab
客戶端訪問服務器的IP地址,可以看到以下界面(配置密碼並登陸):
上傳服務器公鑰(接下來的操作與在github上大同小異),先在服務器上生成密鑰對:
[root@git /]# ssh-keygen -t rsa -C "[email protected]"
[root@git /]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7mTAcNqGbsRoPPAd2M+xfdVfsa4lLPVs37WHbM+iept0DdSIgVaoz++w4oHTccWcH6K5hvCJo5AvTzIuSn3rAV8E6sROL2yCafcLE+//rSpW5/cenvsuYhNV5jkqfuCs1moVev0BHnW//9XZJpyQFIQ8JNlASFGmRXOycNuP9NeacSo5IoXjRzHTA28674+LZwo6D6+klfAYo75rD7JQ61DhrAEq/xBHQ+5zigo6i95xsRdUMPpKRsb0fruTBYUC0jwNfKzgal1CuGTmqJ+l9MI4cnbHNYk2TYP74NgcUYHfp0n6Lr7HkUDAtsyGbT9zPfatU3nVOihN8efdupb8d [email protected]
然後回到web界面:
添加後如下:
創建一個庫:
回到服務器上進行克隆剛剛創建的庫:
[root@git /]# git clone [email protected]:root/test1.git # 進行克隆
[root@git /]# cd test1/ # 可以看到這裏和剛纔庫裏的東西一樣
[root@git test1]# ls
README.md
#定義用戶名及email
[root@git test1]# git config --global user.name "test"
[root@git test1]# git config --global user.email "[email protected]"
#創建測試文件,並推送到遠端庫中測試
[root@git test1]# echo "aaaa" > test.txt
[root@git test1]# git add test.txt
[root@git test1]# git commit -m "alter from 192.168.171.134"
[master 81dc1f8] alter from 192.168.171.134
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@git test1]# git push origin master
刷新web界面的庫頁面:
三、遠端庫的基本操作
當你從遠端倉庫克隆時,實際上git自動把本地的master分支和遠端的master分支對應起來了,並且遠程倉庫的默認名稱是origin。
要查看遠程庫的信息,使用以下命令:
[root@git test1]# git remote # 簡略信息
origin
[root@git test1]# git remote -v # 詳細信息
origin [email protected]:root/test1.git (fetch)
origin [email protected]:root/test1.git (push)
推送分支:
[root@git test1]# git push origin master #推送本地的master分支
[root@git test1]# git push origin dev #推送本地的dev分支,若遠端沒有dev分支,會自動創建
抓取分支:
[root@git test1]# git pull origin dev #根據提示將遠端的dev分支抓取下來
當我們從遠程庫克隆時,默認情況下,只能看到master分支,可以使用git branch命令確認。
解決多人協作容易產生的問題
當我們整個小組對同一個分支進行開發時,如果在你提交之前,你的同事已經修改了分支的內容並推送到遠端倉庫,而碰巧你也對同樣的文件做了修改,並試圖推送,那麼會推送失敗,因爲你的同事的最新提交的數據和你試圖提交的數據有衝突(你本地的內容比遠端倉庫的舊了),解決的辦法會在提示你推送失敗的返回信息中給出,這裏我們模擬一下這一過程。
[root@git /]# mkdir test2
[root@git /]# cd test2/
[root@git test2]# git clone [email protected]:root/test1.git
[root@git test2]# cd test1/
[root@git test1]# git checkout -b dev
Switched to a new branch 'dev'
[root@git test1]# echo "bbbb" > tmp.txt
[root@git test1]# git add tmp.txt
[root@git test1]# git commit -m "commit from /test2"
[dev ba44ec3] commit from /test2
1 file changed, 1 insertion(+)
create mode 100644 tmp.txt
[root@git test1]# git push origin dev
回到web界面進行刷新,即可看到新提交的分支:
上面的操作是在/test2目錄下進行操作的,那麼現在操作/root目錄下的遠程倉庫:
[root@git /]# cd root/test1/ #進入root目錄下的遠程倉庫並創建dev分支,推送內容至遠端倉庫
[root@git test1]# git checkout -b dev
Switched to a new branch 'dev'
[root@git test1]# echo "ccccc" > root.txt
[root@git test1]# git add root.txt
[root@git test1]# git commit -m "commit from /root"
[dev f8fd78d] commit from /root
1 file changed, 1 insertion(+)
create mode 100644 root.txt
[root@git test1]# git push origin dev #此時我們推送,就會提示以下錯誤
To [email protected]:root/test1.git
! [rejected] dev -> dev (fetch first)
error: failed to push some refs to '[email protected]:root/test1.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
#無法推送一些引用到'[email protected]:root/test1.git'
#提示遠程版本庫有我們本地版本庫沒有的提交,所以需要先將遠端版本庫pull下來,再提交
[root@git test1]# git pull origin dev #根據提示將遠端的dev分支pull下來
[root@git test1]# ls
README.md root.txt test.txt tmp.txt
[root@git test1]# git push origin dev #然後再次將本地的dev分支推送到gitlab,即可成功
此時,web界面的dev分支就有了我們在/test2目錄和/root目錄下提交的所有內容,如下:
但是master分支,仍然是最初的文件,如下:
現在進行遠程版本庫的分支合併,如下:
[root@git test1]# git checkout master # 切換到master分支
Switched to branch 'master'
[root@git test1]# git merge dev # 合併dev分支
Updating 81dc1f8..386d906
Fast-forward
root.txt | 1 +
tmp.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 root.txt
create mode 100644 tmp.txt
[root@git test1]# git pull
Already up-to-date.
[root@git test1]# git add *
[root@git test1]# git commit -m "提交"
[root@git test1]# git push origin master # 推送到遠端庫
現在已經擁有了dev分支的所有內容,那麼接下來就演示如何刪除遠程版本庫的dev分支:
[root@git test1]# git branch -d dev # 刪除本地的dev分支
Deleted branch dev (was 386d906).
[root@git test1]# git branch -r -d origin/dev # #刪除指定的遠程分支
Deleted remote branch origin/dev (was 386d906).
[root@git test1]# git push origin :dev # #將刪除的分支提交到遠程版本庫中
To [email protected]:root/test1.git
- [deleted] dev
至此,遠端版本庫中的dev分支就被刪除了,如下:
四、重置gitlab管理員密碼
[root@git /]# gitlab-rails console production #執行該命令,只有第一個命令字可以tab出來
-------------------------------------------------------------------------------------
GitLab: 11.9.8 (48528bc)
GitLab Shell: 8.7.1
postgresql: 9.6.11
-------------------------------------------------------------------------------------
Loading production environment (Rails 5.0.7.1)
irb(main):001:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password='test1234'
=> "test1234"
irb(main):003:0> user.password_confirmation='test1234'
=> "test1234"
irb(main):004:0> user.save
Enqueued ActionMailer::DeliveryJob (Job ID: 86d1357c-d974-4bd9-ad0c-03b4496d9327) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f33612fa170 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0> true
=> true
irb(main):006:0> exit
至此,再次登錄,就需要使用新密碼test1234進行登錄了。