一.GIT基礎
1.1 git簡介
- linus用C語言編寫
- 2005年誕生
- 分布式管理系統
- 速度快、適合大規模、跨地區多人協同開發
1.2 本地管理、集中式、分布式
1.3 git安裝
1 #CentOS上安裝 2 [root@linux-node1 ~]# yum -y install git 3 #Ubuntu上安裝 4 [root@linux-node1 ~]# apt-get install git
注:生產環境中,不建議這么安裝,yum裝的git版本是1.8,推薦使用2.7版本
編譯安裝:
1 #安裝依賴包 2 [root@linux-node1 ~]# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker 3 #下載安裝包 4 [root@linux-node1 ~]# wget https://github.com/git/git/archive/v2.7.4.zip 5 #解壓 6 [root@linux-node1 ~]# unzip git-2.7.4/ 7 #進入git目錄 8 [root@linux-node1 ~]# cd git-2.7.4/ 9 #編譯 10 [root@linux-node1 git-2.7.4]# make prefix=/usr/local/git all 11 #安裝 12 [root@linux-node1 git-2.7.4]# make prefix=/usr/local/git install 13 #刪除原有git命令 14 [root@linux-node1 git-2.7.4]# rm -rf /usr/bin/git 15 #軟連接編譯安裝git命令 16 [root@linux-node1 git-2.7.4]# ln -s /usr/local/git/bin/git /usr/bin/git 17 #查看git版本 18 [root@linux-node1 git-2.7.4]# git --version 19 git version 2.7.4
1.4 git初始化
1 #創建gittest目錄 2 [root@linux-node1 ~]# mkdir gittest 3 #進入目錄 4 [root@linux-node1 ~]# cd gittest 5 #初始化git倉庫 6 [root@linux-node1 gittest]# git init 7 Initialized empty Git repository in /root/gittest/.git/ 8 #配置基礎用戶信息 9 [root@linux-node1 gittest]# git config --global user.name "goodcook" 10 #配置基礎用戶郵件信息 11 [root@linux-node1 gittest]# git config --global user.email "goodcook@qq.com"
1.5 查看基本信息
1 #查看基本信息 2 [root@linux-node1 gittest]# git config --list 3 user.name=goodcook 4 user.email=goodcook@qq.com 5 core.repositoryformatversion=0 6 core.filemode=true 7 core.bare=false 8 core.logallrefupdates=true
1.6 git區域
-
遠程倉庫
-
本地倉庫
-
暫存區域
-
工作目錄
1.7 四種狀態
git如何進行版本管理的呢?它對管理的文件,會打上一個標識,這個標識,就分為四種狀態。
- untracked(未被追蹤的文件)
第一次放到庫中的文件,文件與庫沒有任何的關聯,還沒有納入系統管理版本的序列中。使用 git add將該類文件推送到暫存區,狀態就變成了staged。
- unmodified(未被修改的文件)
未被修改的文件,增加新代碼(修改)之后,會重新拉倒工作目錄中。
- modified(修改后的文件)
修改之后,將該文件git add加入到暫存區,再通過commit放入本地庫中。
- staged(暫存區的文件)
使用git commit將該類文件提交,變成unmodified(未被修改的文件),從暫存區,將文件放到本地倉庫中。
流程:
1.新文件,放到git目錄中(untracked 未被追蹤的文件)
2.使用git add將未被追蹤的文件推送到暫存區(staged 暫存區文件)
3.使用git commit將暫存區文件提交到本地倉庫中(unmodified 未被修改的文件)
4.此時如果修改文件(modified 修改的文件),然后再用git add將修改后的文件加入到暫存區(staged)
5.然后再提交git commit到本地倉庫變成(unmodified 未被修改的文件)...周而復始
1.8 git常用命令
git add 加入暫存區(索引區)
git status 查看狀態
git status -s 狀態概覽
git diff 尚未暫存的文件
git diff --staged 暫存區文件
git commit 提交更新
git reset 回滾
git rm 從版本庫中移除
git rm --cached 從暫存區中移除
git mv 相當於mv 、git rm 、git add三個命令
1 #創建一個文件 2 [root@linux-node1 gittest]# touch index.html 3 #編輯index.html 4 [root@linux-node1 gittest]# vim index.html 5 <h1> welcome to my index </h1> 6 #使用git status查看該文件狀態 7 [root@linux-node1 gittest]# git status
On branch master
Initial commit
Untracked files: #此時狀態為未被追蹤的文件
(use "git add <file>..." to include in what will be committed)
index.html
nothing added to commit but untracked files present (use "git add" to track) 8 #加入代碼庫 9 [root@linux-node1 gittest]# git add index.html 10 #再次查看狀態 11 [root@linux-node1 gittest]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html 12 #提交 13 [root@linux-node1 gittest]# git commit -m "first commit" 14 [master (root-commit) 6f3aca3] first commit 15 1 file changed, 1 insertion(+) 16 create mode 100644 index.html 17 #再次查看狀態 18 [root@linux-node1 gittest]# git status
On branch master
nothing to commit, working directory clean #此時的狀態是工作目錄中沒有還沒有提交的文件了
二.分支管理
2.1 創建一個分支
1 #創建一個分支 2 [root@linux-node1 gittest]# git branch about 3 #查看狀態 4 [root@linux-node1 gittest]# git status 5 On branch master #還是在master上 6 #切換分支 7 [root@linux-node1 gittest]# git checkout about 8 Switched to branch 'about' 9 #查看狀態 10 [root@linux-node1 gittest]# git status 11 On branch about
2.2 分支命令
git branch 查看分支
git branch -v 詳細查看
git branch --merged 查看哪些分支已經被融合
git branch --no-merged 查看哪些分支還沒有被融合
git branch -d 刪除分支
git checkout 切換分支
git merge 融合分支(將分支代碼融合到主干)
git log 查看提交事件
git stash 暫存區
git tag 打標簽
三.遠程倉庫
3.1 將github上的代碼拉到本地
1 #拉代碼 2 [root@linux-node1 ~]# git clone https://github.com/zzgxgit/saltstack-apache.git 3 Cloning into 'saltstack-apache'... 4 remote: Counting objects: 20, done. 5 remote: Total 20 (delta 0), reused 0 (delta 0), pack-reused 20 6 Unpacking objects: 100% (20/20), done. 7 Checking connectivity... done. 8 #進入庫目錄 9 [root@linux-node1 ~]# cd saltstack-apache/ 10 #查看狀態 11 [root@linux-node1 saltstack-apache]# git status 12 On branch master 13 Your branch is up-to-date with 'origin/master'. 14 nothing to commit, working directory clean
3.2 修改文件並push
1 #查看目錄內容 2 [root@linux-node1 saltstack-apache]# ll 3 total 4 4 drwxr-xr-x 3 root root 18 May 4 17:57 modules 5 -rw-r--r-- 1 root root 18 May 4 17:57 README.md 6 #編輯README.md 7 [root@linux-node1 saltstack-apache]# vim README.m 8 #增加一行內容 9 test git push 10 #添加到staged 11 [root@linux-node1 saltstack-apache]# git add . 12 #提交 13 [root@linux-node1 saltstack-apache]# git commit -m "edit README.md" 14 [master 6370f89] edit README.md 15 1 file changed, 1 insertion(+) 16 #遠程庫 17 [root@linux-node1 saltstack-apache]# git remote 18 origin 19 #查看詳細信息 20 [root@linux-node1 saltstack-apache]# git remote -v 21 origin https://github.com/zzgxgit/saltstack-apache.git (fetch) 22 origin https://github.com/zzgxgit/saltstack-apache.git (push) 23 #推送到遠程庫 24 [root@linux-node1 saltstack-apache]# git push origin master 25 #輸入賬號 26 Username for 'https://github.com': zzgxgit 27 #輸入密碼 28 Password for 'https://zzgxgit@github.com': 29 Counting objects: 3, done. 30 Compressing objects: 100% (2/2), done. 31 Writing objects: 100% (3/3), 309 bytes | 0 bytes/s, done. 32 Total 3 (delta 0), reused 0 (delta 0) 33 To https://github.com/zzgxgit/saltstack-apache.git 34 a6443a5..6370f89 master -> master
3.3 給本地庫添加遠程信息
1 #進入之前初始化的本地庫 2 [root@linux-node1 ~]# cd gittest/ 3 #查看遠程信息(沒有任何信息) 4 [root@linux-node1 gittest]# git remote 5 [root@linux-node1 gittest]# git remote -v 6 #添加遠程庫信息 7 [root@linux-node1 gittest]# git remote add origin http://192.168.100.1/goodcook.git 8 #查看信息 9 [root@linux-node1 gittest]# git remote 10 origin 11 [root@linux-node1 gittest]# git remote -v 12 origin http://192.168.100.1/goodcook.git (fetch) 13 origin http://192.168.100.1/goodcook.git (push) 14 #可以添加多個遠程庫 15 [root@linux-node1 gittest]# git remote add gitlab http://192.168.100.2/goodcook.git 16 #查看信息 17 [root@linux-node1 gittest]# git remote 18 gitlab 19 origin 20 [root@linux-node1 gittest]# git remote -v 21 gitlab http://192.168.100.2/goodcook.git (fetch) 22 gitlab http://192.168.100.2/goodcook.git (push) 23 origin http://192.168.100.1/goodcook.git (fetch) 24 origin http://192.168.100.1/goodcook.git (push)
3.4 手動打標簽
1 #查看標簽(為空) 2 [root@linux-node1 gittest]# git tag 3 #查看一下提交信息 4 [root@linux-node1 gittest]# git log 5 commit 6f3aca32bfe6caeb69be9c8b4caa856eedd495ed 6 Author: goodcook <goodcook@qq.com> 7 Date: Thu May 4 14:22:51 2017 +0800 8 9 first commit 10 #打標簽(添加一個1.0的版本信息) 11 [root@linux-node1 gittest]# git tag -a v1.0 -m "version" 12 #查看標簽 13 [root@linux-node1 gittest]# git tag 14 v1.0
四. gitlab安裝配置
4.1 安裝gitlalb
1 #安裝依賴 2 [root@linux-node1 ~]# yum -y install curl policycoreutils openssh-server openssh-clients 3 #sshd開機自啟 4 [root@linux-node1 ~]# systemctl enable sshd 5 #啟動sshd 6 [root@linux-node1 ~]# systemctl start sshd 7 #安裝postfix 8 [root@linux-node1 ~]# yum install postfix 9 #設置開機自啟 10 [root@linux-node1 ~]# systemctl enable postfix 11 #啟動 12 [root@linux-node1 ~]# systemctl start postfix 13 #安裝gitlab8.9.5 14 [root@linux-node1 ~]# rpm -ivh gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:gitlab-ce-8.9.5-ce.0.el7 ################################# [100%]
4.2 配置文件
1 #編輯配置文件 2 [root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb 3 #修改URL(沒有域名就用IP) 4 external_url 'http://192.168.56.11' 5 #自動化配置 6 [root@linux-node1 ~]# gitlab-ctl reconfigure
4.3 訪問gitlab
打開瀏覽器,輸入剛才配置的URL:192.168.56.11
設置一個密碼,不能太短:ABCD1234
【登錄】
用戶名:root
密碼:ABCD1234
至此,gitlab就安裝完成了。
4.4 gitlab常用命令
gitlab-ctl status 查看各組件狀態
gitlab-ctl start 開啟服務
gitlab-ctl stop 停止服務
gitlab-ctl restart 重啟服務
gitlab-ctl tail <service> 查看某一組件日志 例:gitlab-ctl tail nginx
4.5 gitlab組件介紹
nginx 靜態web服務器
gitlab-shell 用於處理git命令和修改authorized keys列表
logrotate 日志文件管理工具
gitlab-workhorse 輕量級的反向代理服務器
postgresql 數據庫
redis 緩存
sidekiq 用於在后台執行隊列任務(異步執行)
unicorn gitlab rails應用是托管在這個服務器上面的
4.6 gitlab常用目錄介紹
/var/opt/gitlab/git-data/repositories/root/ 庫默認存儲目錄
/opt/gitlab/ 應用代碼和相應的依賴程序
/var/opt/gitlab/ 使用gitlab-ctl reconfigure命令編譯后的應用數據和配置文件,不需要人為修改配置
/etc/gitlab/ 配置文件目錄
/var/log/gitlab/ 存放各個組件的日志目錄
/var/opt/gitlab/backups/ 備份文件生成的目錄
五.gitlab管理操作
5.1創建工程
單擊右上角的扳手圖標
單擊new group 創建一個組
創建組
創建用戶
按要求創建用戶按照此法 創建 dev1和dev2用戶
單機進去,查看用戶
回到組的頁面
給 pm 用戶授權為 master
進入項目頁面
創建一個java1的項目
設置權限
配置ssh-key
1 #創建一個秘鑰對 2 [root@linux-node1 .ssh]# ssh-keygen 3 #查看公鑰並拷貝 4 [root@linux-node1 .ssh]# cat id_rsa.pub
將公鑰加入后點擊Add key
六. gitlab備份和恢復
6.1 配置文件
1 #編輯配置文件 2 [root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb 3 #備份目錄 4 201 gitlab_rails['backup_path'] = "/data/backups/gitlab" 5 #備份保留7天 6 204 gitlab_rails['backup_keep_time'] = 604800 7 #創建備份目錄 8 [root@linux-node1 ~]# mkdir -p /data/backups/gitlab 9 #改完配置重新自動化生成 10 [root@linux-node1 ~]# gitlab-ctl reconfigure 11 #重啟服務 12 [root@linux-node1 ~]# gitlab-ctl restart 13 #授權 14 [root@linux-node1 ~]# chown -R git:git /data/backups/gitlab/ 15 #添加定時任務 16 [root@linux-node1 ~]# crontab -e 17 #每天2點全備一次 18 0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create &>/dev/null
6.2 備份測試
1 #手動執行一次 2 [root@linux-node1 ~]# gitlab-rake gitlab:backup:create 3 #查看備份 4 [root@linux-node1 ~]# ll /data/backups/gitlab/
total 40
-rw------- 1 git git 40960 May 5 03:37 1493926679_gitlab_backup.tar
6.3 恢復數據
6.3.1刪除項目
6.3.2 恢復操作
1 #要停兩個服務 2 [root@linux-node1 ~]# gitlab-ctl stop unicorn 3 ok: down: unicorn: 0s, normally up 4 [root@linux-node1 ~]# gitlab-ctl stop sidekiq 5 ok: down: sidekiq: 1s, normally up 6 #恢復備份 7 [root@linux-node1 ~]# gitlab-rake gitlab:backup:restore BACKUP=1493926679 8 #備份完,啟動服務 9 [root@linux-node1 ~]# gitlab-ctl start unicorn 10 ok: run: unicorn: (pid 84833) 1s 11 [root@linux-node1 ~]# gitlab-ctl start sidekiq 12 ok: run: sidekiq: (pid 84865) 0s
打開頁面:
注:恢復操作中的 “BACKUP=1493926679” 是備份時候生成的備份文件名前面的時間戳。
七. gitlab郵件配置
7.1 配置文件
1 #編輯配置文件 2 [root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb 3 21 gitlab_rails['time_zone'] = 'Asia/Shanghai' 4 22 gitlab_rails['gitlab_email_enabled'] = true 5 23 gitlab_rails['gitlab_email_from'] = 'goodcook@126.com' 6 24 gitlab_rails['gitlab_email_display_name'] = 'gitlab' 7 306 gitlab_rails['smtp_enable'] = true 8 307 gitlab_rails['smtp_address'] = "smtp.126.com" 9 308 gitlab_rails['smtp_port'] = 25 10 309 gitlab_rails['smtp_user_name'] = "goodcook" 11 310 gitlab_rails['smtp_password'] = "your_password" 12 311 gitlab_rails['smtp_domain'] = "126.com" 13 312 gitlab_rails['smtp_authentication'] = "login"
【開源是一種精神,分享是一種美德】
— By GoodCook
— 筆者QQ:253097001
— 歡迎大家隨時來交流
—原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。