gitlab 版本控制
一、git介紹
git是一個分布是的版本控制,每個人都有自己的版本庫,每次提交的版本號是sha-1算法產生的全球唯一的編號。
SVN是集中式的版本控制,只有一個版本庫。版本號從1,2,3順序編號。
幾個概念:
index 暫存區-加入跟蹤但沒提交的
work area 工作區
local repository 本地倉庫
remote repository 遠程倉庫
一個文件夾夾 ,在執行git init后,就初始化成本地倉庫。產生一個.git隱藏文件夾
.git |本地倉庫
abc.html |工作區
de.html |工作區
.git隱藏文件夾的內部:
logs |文件夾
objects |文件夾 提交后的數據
refs |文件夾
COMMIT_EDITMSG |
config |
FETCH_HEAD |
HEAD |
index |文件 暫存區
ORIG_HEAD |
packed-refs
sourcetreeconfig
gitlab 版本控制 CE版
下載:https://about.gitlab.com/installation/#centos-7
詳細說明:https://gitlab.com/gitlab-org/omnibus-gitlab/tree/master/doc/settings
客戶端用: Git-2.10.2-64-bit.exe sourcetree
1.下載gitlab安裝包
官網下載速度較慢 建議先行下載
(1).https://about.gitlab.com/downloads/
(2).國內的源 里面可以找到最新的版本https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/trusty/main/g/gitlab-ce/
(3).yum 源安裝 curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
2.安裝依賴
yum install curl policycoreutils openssh-server openssh-clients postfix -y
systemctl enable postfix
systemctl start postfix
firewall-cmd --permanent --add-service=http
systemctl reload firewalld
3.添加yum 源,安裝包
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install gitlab-ce -y
yum --installroot 指定安裝路徑
或者
curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download
rpm -i gitlab-ce-XXX.rpm
4.配置啟動GitLab
gitlab-ctl reconfigure
配置文件: /etc/gitlab/gitlab.rb
external_url 'http://tqsrv131-git' ##!這里是域名或IP
5.EMail設置:
(1)/etc/gitlab/gitlab.rb
external_url 'http://60.205.222.133'
(2)啟用postfix,(本例子中不啟用,使用smtp)
postfix 配置文件:
vi /etc/postfix/main.cf
vi /var/log/mail.log
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'huasaixiu@tianqiantek.cn'
gitlab_rails['gitlab_email_display_name'] = 'GitLab'
gitlab_rails['gitlab_email_reply_to'] = 'noreply@tianqiantek.cn'
(3)配置SMTP 代替SendMail/postfix發送郵件
gedit /etc/gitlab/gitlab.rb
external_url 'http://xx.xx.xx.xx'
# see https://gitlab.com/gitlab-org/omnibus-gitlab/tree/master/doc/settings
# Use smtp insttead of sendmail/postfix.
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.126.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "system_mail_ul@126.com"
gitlab_rails['smtp_password'] = "xxxxx"
gitlab_rails['smtp_domain'] = "smtp.126.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
重新配置
gitlab-ctl reconfigure
測試
gitlab-rails console
irb(main):003:0> Notify.test_email('huasaixiu@tianqiantek.cn', 'Message Subject--test', 'Message Body').deliver_now
注意:如果是阿里
云服務器,需要申請25端口解封,否則不通
6.gitlab-ctl管理gitlab
1)查看狀態
[root@tqsrv131-git ~]# gitlab-ctl status
run: gitaly: (pid 25445) 440241s; run: log: (pid 24853) 440351s
run: gitlab-monitor: (pid 25453) 440240s; run: log: (pid 25130) 440327s
run: gitlab-workhorse: (pid 25457) 440240s; run: log: (pid 24873) 440349s
run: logrotate: (pid 4445) 1038s; run: log: (pid 24953) 440345s
run: nginx: (pid 25474) 440239s; run: log: (pid 24919) 440347s
run: node-exporter: (pid 25482) 440239s; run: log: (pid 25058) 440337s
run: postgres-exporter: (pid 25489) 440238s; run: log: (pid 25111) 440329s
run: postgresql: (pid 25499) 440237s; run: log: (pid 24677) 440390s
run: prometheus: (pid 25507) 440237s; run: log: (pid 25035) 440339s
run:
redis: (pid 25517) 440237s; run: log: (pid 24617) 440397s
run: redis-exporter: (pid 25534) 440236s; run: log: (pid 25088) 440335s
run: sidekiq: (pid 25542) 440234s; run: log: (pid 24834) 440357s
run: unicorn: (pid 25566) 440233s; run: log: (pid 24795) 440359s
提示: 我們要保證80端口不被占用
2)查看一下端口
[root@tqsrv131-git ~]# gitlab-ctl restart
ok: run: gitaly: (pid 6461) 1s
ok: run: gitlab-monitor: (pid 6474) 0s
ok: run: gitlab-workhorse: (pid 6477) 1s
ok: run: logrotate: (pid 6487) 0s
ok: run: nginx: (pid 6494) 0s
ok: run: node-exporter: (pid 6502) 1s
ok: run: postgres-exporter: (pid 6509) 0s
ok: run: postgresql: (pid 6523) 1s
ok: run: prometheus: (pid 6532) 0s
ok: run: redis: (pid 6542) 0s
ok: run: redis-exporter: (pid 6546) 1s
ok: run: sidekiq: (pid 6553) 1s
ok: run: unicorn: (pid 6563) 0s
[root@tqsrv131-git ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 6494 root 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6495 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6496 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6497 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
nginx 6498 gitlab-www 7u IPv4 9813609 0t0 TCP *:http (LISTEN)
AliYunDun 10327 root 18u IPv4 20373 0t0 TCP tqsrv131-git:60838->140.205.140.205:http (CLOSE_WAIT)
AliYunDun 10327 root 20u IPv4 585797 0t0 TCP tqsrv131-git:52398->106.11.68.13:http (CLOSE_WAIT)
AliYunDun 10371 root 18u IPv4 20373 0t0 TCP tqsrv131-git:60838->140.205.140.205:http (CLOSE_WAIT)
AliYunDun 10371 root 20u IPv4 585797 0t0 TCP tqsrv131-git:52398->106.11.68.13:http (CLOSE_WAIT)
AliYunDun 10371 root 22u IPv4 864184 0t0 TCP tqsrv131-git:44820->106.11.68.13:http (ESTABLISHED)
3)Web:訪問
http://xxx.xxx.xxx.xxx/
按照上面設置的external_url訪問,第一次登陸默認管理員密碼和用戶名:
Username: root
Password: 根據郵件給出的連接去設置初始密碼。
gitlab關閉開放注冊
Admin-->settings --> Sign-in Restrictions
Sign-upenbaled 關閉注冊功能
Sign-inenbaled 關閉注冊登錄功能
設置內部使用,不公開
4)使用
先創建組,再用組創建項目,最后創建用戶,把用放組里
設置 項目、snippet、group 權限等級分三種:
Private 私有的,只有你自己或者組內的成員能訪問
Internal 所有登錄的用戶
Public 公開的,所有人都可以訪問
對項目進行設置
5)Git的使用
Git Bash 下載地址:https://git-scm.com/downloads
(1)添加sshkey
生成sshkey
ssh-keygen -t rsa -C "$your_email"
cat ~/.ssh/id_rsa.pub
$your_emai是你的郵箱地址,一直回車即可,此命令在C:\Users\<你的用戶名>.ssh目錄下生成一對公私密鑰,拷貝公鑰(.pub結尾的文件)的內容
(2).保存sshkey到gitlab
登陸你的GitLab賬號之后,點擊右上角的”Profile Setting” -> “SSH Keys”,輸入SSH Key標題(可自定義),將拷貝的id_rsa.pub內容拷貝到Key中,”Add Key”即可。
(3).開始上傳代碼
進入工程目錄 cd $project_root
初始化git倉庫 git init
添加文件到倉庫 git add .
提交代碼到倉庫git commit -m ‘init commit’
鏈接到git server
git remote add origin git@example.com:namespace/projectname.git
push代碼到
服務器 git push origin master
GitLab一些常用指令
gitlab-ctl start/stop/restart/
Gitlab 默認的日志文件存放在/var/log/gitlab 目錄下
gitlab-ctl tail
gitlab-ctl tail nginx/gitlab_acces.log
gitlab-ctl tail postgresql
1.GitLab 倉庫
搜索 /etc/gitlab/gitlab.rb 中的 git_data_dir
缺省倉庫路徑: git_data_dir “/var/opt/gitlab/git-data”
備份路徑: /var/opt/gitlab/backups
備份保存時間: gitlab_rails['backup_keep_time'] = 604800
2.GitLab 備份
參考文檔:https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/backups.md
http://shaonian.blog.51cto.com/2975261/1891124/
2.1方式1 直接備份配置文件和數據文件
-
備份目錄:
-
/etc/gitlab
-
/var/opt/gitlab/git-data/repositories
-
-
備份方法
-
cd /u01/gitlab/backups/
tar -zPcf $(date "+etc-gitlab_%Y%_m_%d_%H%M%S.tar.gz") /etc/gitlab
tar -zPcf $(date "+gitlab_data_%Y_%m_%d_%H%M%S.tar.gz") /var/opt/gitlab/git-data/repositories
-
定時:
-
sudo sh -c 'umask 0077; tar -cf $(date "+etc-gitlab-%s.tar") -C / etc/gitlab'
-
2.2方式2 Omnibus 方式安裝使用以下命令備份
-
指令 : /opt/gitlab/bin/gitlab-rake gitlab:backup:create
-
-
-
[root@tqsrv131-git repositories]# gitlab-rake gitlab:backup:create
-
Dumping database ...
-
Dumping PostgreSQL database gitlabhq_production ... [DONE]
-
...
-
done
-
Deleting old backups ... skipping
-
-
[root@tqsrv131-git backups]# pwd
-
/var/opt/gitlab/backups
-
[root@tqsrv131-git backups]# ll
-
total 64
-
-rw------- 1 git git 61440 Aug 15 13:39 1502775554_2017_08_15_9.4.3_gitlab_backup.tar
-
-
定時配置備份:
15 04 * * 2-6 umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab
定時備份數據倉庫:
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
3.恢復
-
cp 1493107454_2017_04_25_9.1.0_gitlab_backup.tar /var/opt/gitlab/backups/
-
gitlab-ctl stop unicorn
-
gitlab-ctl stop sidekiq
-
gitlab-ctl status
-
gitlab-rake gitlab:backup:restore BACKUP=1493107454_2017_04_25_9.1.0
-
gitlab-ctl start
-
gitlab-rake gitlab:check SANITIZE=true
Git 分支 - 分支的新建與合并
master
|->C2->C1->C0
要解決你的公司使用的問題追蹤系統中的 #53 問題
$ git checkout -b iss53
Switched to a new branch "iss53"
也就是下面兩天命令的縮寫
$ git branch iss53
$ git checkout iss53
$ git commit -a -m 'added a new footer [issue 53]'
$ git checkout master
Switched to branch 'master'
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fixed the broken email address'
[hotfix 1fb7853] fixed the broken email address
1 file changed, 2 insertions(+)
ssh-keygen -t rsa -C "huasaixiu@tianqiantek.cn" -b 4096