Gitlab 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自托管的 Git 項目倉庫,可通過Web 界面進行訪問公開的或者私人的項目 Gitlab 擁有與 Github 類似的功能,能夠瀏覽源代碼,管理缺陷和注釋??梢怨芾韴F隊對倉庫的訪問,他非常易于瀏覽提交過的版本并提供一個文件歷史庫。他還提供一個代碼片段收集功能可以輕松實現代碼復用,便于日后有需要的時候進行查找
博文大綱:
一、環境準備
二、安裝部署gitlab
三、遠端庫的基本操作
四、重置gitlab管理員密碼
如果是測試環境,其內存建議2G及以上,可以去清華開源鏡像站下載所需gitlab版本,其安裝后,會自動安裝nginx提供web界面,所以要避免80端口占用。
[root@git src]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm
[root@git ~]# rpm -ivh gitlab-ce-11.9.8-ce.0.el7.x86_64.rpm #安裝rpm包
#由于我不打算做域名解析,所以需要修改其配置文件
[root@git ~]# vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.20.2' #將原本的域名改為本機IP
[root@git ~]# gitlab-ctl reconfigure #重新配置gitlab,就算不修改配置文件,也需要在安裝后重新配置gitlab
[root@git ~]# netstat -anpt | grep -w 80 #確定nginx在監聽80端口
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3872/nginx: master
客戶端訪問服務器的IP地址,可以看到以下界面(配置密碼并登陸):
上傳服務器公鑰(接下來的操作與在github上大同小異),先在服務器上生成密鑰對:
[root@git ~]# ssh-keygen -t rsa -C "916551516@qq.com"
#執行上述命令生成秘鑰對,一路回車即可,后面是自己的郵箱
[root@git ~]# cat ~/.ssh/id_rsa.pub #查看生成的公鑰并復制其內容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCVUbmw+PC2plXMviNBA6YKgWx+1419uMPJhJNGeZ8Mi7njBlAyhfzWQsCFamQqU4nwSMWua3oEJZEiT6ZRgLQQyYwZ+DESQCyhAJYdtOgwtRh4XaIgAASzlqZ0hIjhhwi+pD/GjktySiW/UvaiqkkBgfdXCuXlSH7R8T4PoRCpYZ92OwQmMV/blGd/SXLDCaqHlgz1K8As8HFoaFAkrmS+jSio616+w2o8ly2PBbVoZa129BD8SQ81KAs5drGTD2OsKHFx08rS5izSc2MKLjet7+00CXJebwZQLB1LS/TUXwrPAHLQO8ZN8AYCsoIv6coEYRhW93BoBZ8Swetys/KD 916551516@qq.com
然后回到web界面:
添加后如下:
創建一個庫:
回到服務器上進行克隆剛剛創建的庫:
[root@git ~]# git clone git@192.168.20.2:root/test01.git #克隆
[root@git ~]# cd test01/ #進入克隆的庫
[root@git test01]# ls #該目錄下的東西和剛剛web頁面創建好庫后的內容一樣
README.md
#自報家門
[root@git test01]# git config --global user.name "test"
[root@git test01]# git config --global user.email "test@test.com"
#向遠端庫上傳文件進行測試
[root@git test01]# echo "aaaaaaaaa" > test.txt
[root@git test01]# git add test.txt
[root@git test01]# git commit -m "alter from 192.168.20.2"
[root@git test01]# git push origin master #推送到遠端庫
刷新web界面的庫頁面:
當你從遠端倉庫克隆時,實際上git自動把本地的master分支和遠端的master分支對應起來了,并且遠程倉庫的默認名稱是origin。
要查看遠程庫的信息,使用以下命令:
[root@git test01]# git remote #簡略信息
origin
[root@git test01]# git remote -v #詳細信息
origin git@192.168.20.2:root/test01.git (fetch)
origin git@192.168.20.2:root/test01.git (push)
推送分支:
[root@git test01]# git push origin master #推送本地的master分支
[root@git test01]# git push origin dev #推送本地的dev分支,若遠端沒有dev分支,會自動創建
抓取分支:
[root@git test01]# git pull origin dev #根據提示將遠端的dev分支抓取下來
當我們從遠程庫克隆時,默認情況下,只能看到master分支,可以使用git branch命令確認。
當我們整個小組對同一個分支進行開發時,如果在你提交之前,你的同事已經修改了分支的內容并推送到遠端倉庫,而碰巧你也對同樣的文件做了修改,并試圖推送,那么會推送失敗,因為你的同事的最新提交的數據和你試圖提交的數據有沖突(你本地的內容比遠端倉庫的舊了),解決的辦法會在提示你推送失敗的返回信息中給出,這里我們模擬一下這一過程。
#進入另一個目錄,克隆遠端倉庫,模擬多人協作
[root@git test01]# cd /tmp
[root@git tmp]# git clone git@192.168.20.2:root/test01.git
[root@git test01]# git checkout -b dev #創建一個分支dev并進入
[root@git test01]# echo "aaaaaaaaa" > tmp.txt
[root@git test01]# git add tmp.txt
[root@git test01]# git commit -m "commmit from /tmp"
[root@git test01]# git push origin dev #將新修改的內容推送到遠端
回到web界面進行刷新,即可看到新提交的分支:
上面的操作是在/tmp目錄下進行操作的,那么現在操作/root目錄下的遠程倉庫:
#進入root目錄下的遠程倉庫并創建dev分支,推送內容至遠端倉庫
[root@git test01]# cd /root/test01/
[root@git test01]# git checkout -b dev
[root@git test01]# echo "bbbbbbbbbb" > root.txt
[root@git test01]# git add root.txt
[root@git test01]# git commit -m "commit from /root"
[root@git test01]# git push origin dev #此時我們推送,就會提示以下錯誤
To git@192.168.20.2:root/test01.git
! [rejected] dev -> dev (fetch first)
error: 無法推送一些引用到 'git@192.168.20.2:root/test01.git'
提示:更新被拒絕,因為遠程版本庫包含您本地尚不存在的提交。這通常是因為另外
提示:一個版本庫已推送了相同的引用。再次推送前,您可能需要先合并遠程變更
提示:(如 'git pull')。
提示:詳見 'git push --help' 中的 'Note about fast-forwards' 小節。
#提示遠程版本庫有我們本地版本庫沒有的提交,所以需要先將遠端版本庫pull下來,再提交
[root@git test01]# git pull origin dev #根據提示將遠端的dev分支pull下來
[root@git test01]# ls #我們在/tmp目錄下提交的文件就存在了
README.md root.txt test.txt tmp.txt
[root@git test01]# git push origin dev #然后再次將本地的dev分支推送到gitlab,即可成功
此時,web界面的dev分支就有了我們在/tmp目錄和/root目錄下提交的所有內容,如下:
但是master分支,仍然是最初的文件,如下:
[root@git test01]# git checkout master #切換至master分支
[root@git test01]# git merge dev #合并dev分支
[root@git test01]# ls #查看合并后的分支下內容
README.md root.txt test.txt tmp.txt
#提交到本地版本庫
[root@git test01]# git pull #下載遠端版本庫
[root@git test01]# git add *
[root@git test01]# git commit -m "提交"
[master 3717c1c] 提交
[root@git test01]# git push origin master #推送到遠端版本庫
現在遠程版本庫的master分支內容如下:
現在已經擁有了dev分支的所有內容,那么接下來就演示如何刪除遠程版本庫的dev分支:
[root@git test01]# git branch -d dev #刪除本地的dev分支
[root@git test01]# git branch -r -d origin/dev #刪除指定的遠程分支
[root@git test01]# git push origin :dev #將刪除的分支提交到遠程版本庫中
至此,遠端版本庫中的dev分支就被刪除了,如下:
[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):003:0> user.password='test1234'
=> "test1234"
irb(main):004:0> user.password_confirmation='test1234'
=> "test1234"
irb(main):005:0> user.save
Enqueued ActionMailer::DeliveryJob (Job ID: 17d6f678-3cae-4de2-be59-b19a6d22b9fa) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f895034e280 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):006:0> true
=> true
irb(main):007:0> exit
至此,再次登錄,就需要使用新密碼test1234進行登錄了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。