【0718】 代碼管理平臺

22.1 代碼管理平臺介紹html

  • 版本控制,記錄若干文件內容變化,以便未來查閱特定版本修訂狀況java

版本管理工具發展簡史,cvs—> svn—> git  參考http://luckypoem14.github.io/test/2012/04/24/scm-history/node

  • svn全稱subversion,是一個開源版本控制系統,始於2000年mysql

  • git是linux創始人linus發起的,2005年發佈,最初目的是更好管理linux內核代碼linux

  • git和svn不一樣在於git不須要依賴服務端就能夠工做,即git是分佈式的nginx

  • 關於git和svn的比較你們參考http://blog.lishiming.net/?p=305git

  • github是基於git的在線web頁面代碼託管平臺,能夠選擇付費服務github

  • gitlab能夠認爲是一個開源的github,二者沒有直接關係web


22.2 安裝svnredis

一、安裝服務端

[root@arslinux-01 ~]# yum install -y subversion

二、建立版本庫

[root@arslinux-01 ~]# mkdir -p /data/svnroot/myproject
[root@arslinux-01 ~]# svnadmin create /data/svnroot/myproject/
[root@arslinux-01 ~]# cd /data/svnroot/myproject/conf/
[root@arslinux-01 conf]# ls
authz  passwd  svnserve.conf
[root@arslinux-01 conf]# vim authz
admins = arslinux,user1
[/]
@admins = rw
*= r
[myproject:/]
user1 = rw

[/] /指的就是/data/svnroot/myproject/,在這個目錄下 admin 組權限時 rw

* 表示剩餘的人是 r 權限

[myproject:/] 通常用在 svnroot 下有多個項目的狀況下,其中一個 myproject 的項目

能夠在這個目錄下建立子目錄 / 前面 : 表示 myproject 是個項目名


三、設置密碼

[root@arslinux-01 conf]# vim passwd
[users]
arslinux = arslinux_!(*$123
user1 = user1_^^^123
user2 = user2-***123

四、編輯配置文件 svnserver.conf

[root@arslinux-01 conf]# vim svnserve.conf
[general]
anon-access = none                //匿名用戶有無權限
auth-access = write                //被受權用戶 可寫
password-db = passwd                //用戶密碼存在哪裏
authz-db = authz                    //權限控制文件
realm = /data/svnroot/myproject    //對哪一個項目生效

五、啓動 svn

[root@arslinux-01 conf]# svnserve -d -r /data/svnroot/

-d 後臺啓動

-r 指定項目所在路徑,也可寫成 svnserve -d -r /data/svnroot/myproject/

[root@arslinux-01 conf]# ps aux|grep svnserve
root      31030  0.0  0.0 180732   804 ?        Ss   22:24   0:00 svnserve -d -r /data/svnroot/
root      31060  0.0  0.0 112724   988 pts/1    S+   22:26   0:00 grep --color=auto svnserve
[root@arslinux-01 conf]# netstat -lntp|grep svnserve
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      31030/svnserve


22.3 客戶端上使用svn(linux)

一、客戶端安裝 subversion

[root@arslinux-02 ~]# yum install -y subversion

二、鏈接 svn 服務器(本地能夠保存用戶名和密碼)

[root@arslinux-02 ~]# cd /home/
[root@arslinux-02 home]# svn checkout svn://192.168.194.130/myproject --username=user1
認證領域: <svn://192.168.194.130:3690> /data/svnroot/myproject
「user1」的密碼:

-----------------------------------------------------------------------
注意!  你的密碼,對於認證域:

<svn://192.168.194.130:3690> /data/svnroot/myproject

只能明文保存在磁盤上!  若是可能的話,請考慮配置你的系統,讓 Subversion
能夠保存加密後的密碼。請參閱文檔以得到詳細信息。

你能夠經過在「/root/.subversion/servers」中設置選項「store-plaintext-passwords」爲「yes」或「no」,
來避免再次出現此警告。
-----------------------------------------------------------------------
保存未加密的密碼(yes/no)?yes
取出版本 0。

三、 拷貝任意一個文件到項目中

[root@arslinux-02 home]# cd myproject/
[root@arslinux-02 myproject]# cp /etc/fstab ..

四、添加到版本控制中心  svn add .

[root@arslinux-02 myproject]# svn add ./fstab
A         fstab

五、把文件上傳到服務器 svn commit -m "add file"

[root@arslinux-02 myproject]# svn commit -m "add fstab"
正在增長       fstab
傳輸文件數據.
提交後的版本爲 1。

六、在服務端上升級版本才能看見上傳的文件

把當前目錄下的文件都更新到最新版  svn update 可簡寫爲 svn up

[root@arslinux-01 svntest]# cd myproject/
[root@arslinux-01 myproject]# ls
[root@arslinux-01 myproject]# svn up
正在升級 '.':
A    fstab
更新到版本 1。
[root@arslinux-01 myproject]# ls
fstab

七、用戶名密碼被記錄在 /root/.subversion/auth/svn.simple/ 下

[root@arslinux-02 .subversion]# cat /root/.subversion/auth/svn.simple/7681a1260e61438030ce66c9a673594d
K 8
passtype
V 6
simple
K 8
password
V 12
user1_^^^123
K 15
svn:realmstring
V 52
<svn://192.168.194.130:3690> /data/svnroot/myproject
K 8
username
V 5
user1
END

八、若是不想用戶信息被記錄,能夠刪除 svn.simple 下的文件便可

[root@arslinux-01 svn.simple]# rm -rf /root/.subversion/auth/svn.simple/7681a1260e61438030ce66c9a673594d

九、在客戶端更改文件,添加任意一行到 fstab,並將更改數據上傳服務端

[root@arslinux-02 myproject]# echo '"xafdasfasdfsa" >> fstab
[root@arslinux-02 myproject]# svn commit -m "ch fstab"
正在發送       fstab
傳輸文件數據.
提交後的版本爲 2。

十、服務端再更新版本,發現須要密碼(提示輸入 root 密碼,可回車跳過,輸入實際用戶名密碼)

[root@arslinux-01 myproject]# svn up
正在升級 '.':
認證領域: <svn://192.168.194.130:3690> /data/svnroot/myproject
「root」的密碼:
認證領域: <svn://192.168.194.130:3690> /data/svnroot/myproject
用戶名: arslinux
「arslinux」的密碼:

-----------------------------------------------------------------------
注意!  你的密碼,對於認證域:

<svn://192.168.194.130:3690> /data/svnroot/myproject

只能明文保存在磁盤上!  若是可能的話,請考慮配置你的系統,讓 Subversion
能夠保存加密後的密碼。請參閱文檔以得到詳細信息。

你能夠經過在「/root/.subversion/servers」中設置選項「store-plaintext-passwords」爲「yes」或「no」,
來避免再次出現此警告。
-----------------------------------------------------------------------
保存未加密的密碼(yes/no)?yes
U    fstab
更新到版本 2。

十一、文件最後一行也增長了剛纔輸入的隨機字符串

[root@arslinux-01 myproject]# tail -1 fstab
xafdasfasdfsa

十二、在服務端本地刪除文件,服務器上刪除文件

[root@arslinux-01 myproject]# svn delete fstab
D         fstab
[root@arslinux-01 myproject]# svn commit -m "delete fstab"
正在刪除       fstab
提交後的版本爲 3。

1三、在客戶端上更新同步

[root@arslinux-02 myproject]# svn up
正在升級 '.':
D    fstab
更新到版本 3。
[root@arslinux-02 myproject]# ls

1四、查看變動歷史  svn log

[root@arslinux-02 myproject]# svn log
------------------------------------------------------------------------
r3 | arslinux | 2019-07-22 22:06:45 +0800 (一, 2019-07-22) | 1 行

delete fstab
------------------------------------------------------------------------
r2 | user1 | 2019-07-22 21:43:42 +0800 (一, 2019-07-22) | 1 行

ch fstab
------------------------------------------------------------------------
r1 | user1 | 2019-07-22 21:22:41 +0800 (一, 2019-07-22) | 1 行

add fstab

svn: E155015: 提交失敗(細節以下) 解決辦法:

svn resolved <文件名>


22.4 客戶端上使用svn(windows)

  • 下載 TortoiseSVN 並安裝 https://tortoisesvn.net/index.zh.html

一、安裝完軟件,再安裝語言包便可

二、硬盤裏新建 myproject 文件夾

三、右擊 myproject 文件夾,選擇 SVN Checkout...

四、填入項目的服務地址

clipboard11.png

五、點 OK 後,輸入用戶名和密碼

clipboard12.pngclipboard13.png

五、在 myproject 文件夾下新建 123.txt,內容隨便

六、右擊 123.txt,選擇 TortoiseSVN——Add

七、右擊 123.txt,SVN Commit...

八、在彈框中作一些說明

clipboard14.png

clipboard15.png

九、在 linux 服務端機器

[root@arslinux-01 myproject]# svn up
正在升級 '.':
A    123.txt
更新到版本 6。
[root@arslinux-01 myproject]# cat 123.txt
adsfadfasdfa

十、在另外一個linux客戶端的機器上編輯一個文件並上傳

[root@arslinux-02 myproject]# echo "adfadfafa\nadsfdafa" > ars4life
[root@arslinux-02 myproject]# cat ars4life
adfadfafa\nadsfdafa
[root@arslinux-02 myproject]# svn add ars4life
A         ars4life
[root@arslinux-02 myproject]# svn commit -m "add asr4life"
正在增長       ars4life
傳輸文件數據.
提交後的版本爲 7。

十一、而後在 windows 機器上,右擊 myproject ,選擇 SVN Update,myproject 目錄裏會有 ars4life 文件

簡明教程http://www.jianshu.com/p/6b3b7b915332


22.5/22.6 單機上使用git

一、安裝

[root@arslinux-01 ~]# yum install -y git

二、建立 gitroot目錄

[root@arslinux-01 ~]# mkdir /data/gitroot/
[root@arslinux-01 ~]# cd !$
cd /data/gitroot/
[root@arslinux-01 gitroot]#

三、初始化倉庫

[root@arslinux-01 gitroot]# git init
初始化空的 Git 版本庫於 /data/gitroot/.git/
[root@arslinux-01 gitroot]# ll -a
總用量 0
drwxrwxr-x  3 root root  18 7月  23 22:18 .
drwxr-xr-x 11 root root 135 7月  23 22:18 ..
drwxrwxr-x  7 root root 119 7月  23 22:18 .git

4.在庫中建立一個文件,任意內容

[root@arslinux-01 gitroot]# echo "dasfdafasfafas" > 1.txt
[root@arslinux-01 gitroot]# echo "ddfdfdfw22ghghg" >> 1.txt
[root@arslinux-01 gitroot]# echo "dftyuoiuoryfhgjg" >> 1.txt

五、將文件添加到倉庫(方法幾乎和 svn 一致)

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@arslinux-01.(none)')

六、上傳時出錯,根據參考內容,設置郵箱和用戶名

[root@arslinux-01 gitroot]# git config --global user.email "zy77xp2316@qq.com"
[root@arslinux-01 gitroot]# git config --global user.name "arslinux"

也能夠去 /root/.gitconfig 下更改

七、從新作上傳

[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master(根提交) f04992b] add 1.txt
1 file changed, 3 insertions(+)
create mode 100644 1.txt

八、更改 1.txt 內容

[root@arslinux-01 gitroot]# echo "888888" >> 1.txt
[root@arslinux-01 gitroot]# echo "777777" >> 1.txt

九、修改的內容若是提交上傳,還須要操做 git add 和 git commit -a

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 03d2ada] add 1.txt
1 file changed, 2 insertions(+)

十、查看當前倉庫中的狀態,好比是否有改動的文件

[root@arslinux-01 gitroot]# git status
# 位於分支 master
無文件要提交,乾淨的工做區

十一、若是沒有 git add 和 git commit 的步驟,會提示

[root@arslinux-01 gitroot]# echo " dasfadsf a" >> 1.txt
[root@arslinux-01 gitroot]# git status
# 位於分支 master
# 還沒有暫存以備提交的變動:
#   (使用 "git add <file>..." 更新要提交的內容)
#   (使用 "git checkout -- <file>..." 丟棄工做區的改動)
#
#修改:      1.txt
#
修改還沒有加入提交(使用 "git add" 和/或 "git commit -a")

十二、對比修改了哪些內容能夠用 git diff

[root@arslinux-01 gitroot]# git diff
diff --git a/1.txt b/1.txt
index 03086e7..e51c67f 100644
--- a/1.txt
+++ b/1.txt
@@ -3,3 +3,4 @@ ddfdfdfw22ghghg
dftyuoiuoryfhgjg
888888
777777
+ dasfadsf a

1三、再作幾回操做,查看全部提交記錄

[root@arslinux-01 gitroot]# vim 1.txt
增長一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "add 1.txt"
[master 3517c53] add 1.txt
1 file changed, 2 insertions(+)
[root@arslinux-01 gitroot]# vim 1.txt
刪除一行
[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git commit -m "ch 1.txt"
[master b68b8fb] ch 1.txt
1 file changed, 1 deletion(-)
  • 查看提交記錄

[root@arslinux-01 gitroot]# git log
commit b68b8fb900160e2ab06b4204d97f659fa14fa26a
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:45:24 2019 +0800
ch 1.txt
commit 3517c53c22f14c345206324bf9d190f157ec1f83
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:44:55 2019 +0800
add 1.txt
commit 03d2adacc5ee700de75799b90c1534ad6d9e6b9a
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:36:19 2019 +0800
add 1.txt
commit f04992b2e67da3ac3bfbc1c6a21053312f27ac76
Author: arslinux <xxxxxx@qq.com>
Date:   Tue Jul 23 22:29:07 2019 +0800
add 1.txt
  • 只顯示一行信息  git log --pretty=oneline

[root@arslinux-01 gitroot]# git log --pretty=oneline
b68b8fb900160e2ab06b4204d97f659fa14fa26a ch 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt

1四、git reset --hard 字符串  回退版本,其中後面跟的字符串是簡寫  

[root@arslinux-01 gitroot]# git reset --hard 3517c5
HEAD 如今位於 3517c53 add 1.txt
[root@arslinux-01 gitroot]# git log --pretty=oneline
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt

1五、若是回退出了錯,如今想回到 b68b8fb900 的版本

能夠直接 git reset --hard b68b8fb900,也可使用 git reflog

[root@arslinux-01 gitroot]# git reflog
3517c53 HEAD@{0}: reset: moving to 3517c5
b68b8fb HEAD@{1}: commit: ch 1.txt
3517c53 HEAD@{2}: commit: add 1.txt
03d2ada HEAD@{3}: commit: add 1.txt
f04992b HEAD@{4}: commit (initial): add 1.txt

而後在使用 git reset --hard b68b8fb

1六、若是不當心刪除了文件,那麼用 git checkout -- 文件名 來恢復

[root@arslinux-01 gitroot]# rm -rf 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
[root@arslinux-01 gitroot]# ls
1.txt

1七、若是文件本修改,add 後沒有 commit,想回到上一次提交的狀態,可使用 git reset HEAD 文件名,而後在執行git checkout -- 文件名

[root@arslinux-01 gitroot]# git add 1.txt
[root@arslinux-01 gitroot]# git reset HEAD 1.txt
重置後撤出暫存區的變動:
M1.txt
[root@arslinux-01 gitroot]# git checkout -- 1.txt

兩個步驟:

1)git reset HEAD 文件名 從緩存區撤銷

2)git checkout -- 文件名 恢復成修改前的狀態

1八、刪除文件

[root@arslinux-01 gitroot]# git rm 1.txt
rm '1.txt'
[root@arslinux-01 gitroot]# git commit -m "delete 1.txt"
[master 560134a] delete 1.txt
1 file changed, 7 deletions(-)
delete mode 100644 1.txt
[root@arslinux-01 gitroot]# ls
[root@arslinux-01 gitroot]# git checkout -- 1.txt
error: pathspec '1.txt' did not match any file(s) known to git.

1九、文件刪除後依然能夠找回,用 git reset 找回

[root@arslinux-01 gitroot]# git log --pretty=oneline
560134a1708b3815cb53af6ae160240b574e2240 delete 1.txt
3517c53c22f14c345206324bf9d190f157ec1f83 add 1.txt
03d2adacc5ee700de75799b90c1534ad6d9e6b9a add 1.txt
f04992b2e67da3ac3bfbc1c6a21053312f27ac76 add 1.txt
[root@arslinux-01 gitroot]# git reset --hard 3517c53c22f1
HEAD 如今位於 3517c53 add 1.txt
[root@arslinux-01 gitroot]# ls
1.txt


22.7 創建遠程倉庫

一、首先到 https://github.com 註冊一個帳號,建立本身的git,點右上角 「+」,選擇 New repository

二、倉庫名字自定義,好比叫,能夠添加註釋(Description),權限選擇public,點Create repository

三、添加key:右上角點本身頭像,選擇settings,左側選擇SSH and GPG keys

四、右側點 New SSH key,把linux機器上的 ~/.ssh/id_rsa.pub 內容粘貼到這裏,能夠用 ssh-keygen 生成

五、在本地建立一個倉庫並進入

[root@arslinux-01 ~]# mkdir /tmp/studygit/
[root@arslinux-01 ~]# cd /tmp/studygit/

六、根據提示進行操做,首先建立一個 README.md

[root@arslinux-01 studygit]# echo "# studygit" >> README.md

七、初始化,生成 .git

[root@arslinux-01 studygit]# git init
初始化空的 Git 版本庫於 /tmp/studygit/.git/
[root@arslinux-01 studygit]# ll -a
總用量 4
drwxrwxr-x  3 root root  80 7月  25 22:05 .
drwxrwxrwt 11 root root 340 7月  25 22:05 ..
drwxrwxr-x  7 root root 200 7月  25 22:05 .git
-rw-rw-r--  1 root root  11 7月  25 22:04 README.md

八、提交 README.md 到倉庫

[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "first commit"
[master(根提交) f6afa16] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

九、將倉庫內容推到遠程

[root@arslinux-01 studygit]# git remote add origin https://github.com/axxxxx4xxx/studygit.git

十、將更改推送到遠程

[root@arslinux-01 studygit]# git push -u origin master
Username for 'https://github.com': axxxxx4xxx
Password for 'https://axxxxx4xxx@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 220 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxx/studygit.git
* [new branch]      master -> master
分支 master 設置爲跟蹤來自 origin 的遠程分支 master。

十一、推送新增文件操做

[root@arslinux-01 studygit]# echo "73737372hhdjdjd" > 2.txt
[root@arslinux-01 studygit]# ls
2.txt  README.md
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[master 6e4549c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 studygit]# git push
warning: push.default 未設置,它的默認值將會在 Git 2.0 由 'matching'
修改成 'simple'。若要再也不顯示本信息並在其默認值改變後維持當前使用習慣,
進行以下設置:

git config --global push.default matching

若要再也不顯示本信息並從如今開始採用新的使用習慣,設置:

git config --global push.default simple

參見 'git help config' 並查找 'push.default' 以獲取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。若是您有時要使用老版本的 Git,
爲保持兼容,請用 'current' 代替 'simple' 模式)

Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
f6afa16..6e4549c  master -> master

十二、根據提示,設置一下 git config

[root@arslinux-01 studygit]# git config --global push.default simple

1三、網頁刷新,就能夠看到新增的文件

clipboard.png

22.8 克隆遠程倉庫

一、網頁端倉庫中,點右側綠色「Clone or download」,複製生成的 git 連接

二、克隆到本機

[root@arslinux-01 studygit]# cd
[root@arslinux-01 ~]# cd /home/
[root@arslinux-01 home]# git clone https://github.com/arsenal4life/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
[root@arslinux-01 home]# cd studygit/
[root@arslinux-01 studygit]# ls
2.txt  README.md

三、更改 README.md 內容,並推送到遠程

[root@arslinux-01 studygit]# echo "hello hello" >> README.md
[root@arslinux-01 studygit]# echo "bye" >> README.md
[root@arslinux-01 studygit]# git add README.md
[root@arslinux-01 studygit]# git commit -m "change README.md"
[master b2c0aa1] change README.md
1 file changed, 2 insertions(+)
[root@arslinux-01 studygit]# git push
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
6e4549c..b2c0aa1  master -> master

四、成功

2.png

五、將遠程更改文件同步到本地

——遠程修改 2.txt 內容

[root@arslinux-01 studygit]# git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
來自 https://github.com/arsenal4life/studygit
b2c0aa1..cce2494  master     -> origin/master
更新 b2c0aa1..cce2494
Fast-forward
2.txt | 3 +++
1 file changed, 3 insertions(+)
[root@arslinux-01 studygit]# cat 2.txt
73737372hhdjdjd
thank you
go go go
ore wa neko ga suki


22.9 分支管理

22.9 分支管理

  • git branch                   查看分支

  • git branch 分支名          建立分支

  • git checkout 分支名       切換分支

[root@arslinux-01 gitroot]# git branch
* master
[root@arslinux-01 gitroot]# git branch arslinux
[root@arslinux-01 gitroot]# git branch
arslinux
* master
[root@arslinux-01 gitroot]# git checkout arslinux
切換到分支 'arslinux'
[root@arslinux-01 gitroot]# git branch
* arslinux
master

——當前使用的分支前面會有一個 * 在 arslinux 分支下

——編輯 2.txt,並提交到新分支

[root@arslinux-01 gitroot]# echo "kajlhlk" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "add 2.txt"
[arslinux 0f6b36c] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt  2.txt
[root@arslinux-01 gitroot]# git checkout master
切換到分支 'master'
[root@arslinux-01 gitroot]# ls
1.txt

分支能夠隔離開文件

  • git merge 分支名          合併分支

[root@arslinux-01 gitroot]# git merge arslinux
更新 3517c53..0f6b36c
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@arslinux-01 gitroot]# ls
1.txt  2.txt

想要將文件合併到哪一個分支下,那麼在合併前,須要先切換到該分支下,再進行合併分支操做

以上操做中,合併分以後,master 分支下有了 2.txt

——合併分支時,若是合併的分支下有相同文件名的文件,那麼文件內容須要一致

[root@arslinux-01 gitroot]# echo "12sddsf2232" > 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[master bb88c13] ch 2.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout arslinux
切換到分支 'arslinux'
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# git add 2.txt
[root@arslinux-01 gitroot]# git commit -m "ch 2.txt"
[arslinux 79c2a72] ch 2.txt
1 file changed, 1 deletion(-)
[root@arslinux-01 gitroot]# git checkout master
切換到分支 'master'
[root@arslinux-01 gitroot]# git merge arslinux
自動合併 2.txt
衝突(內容):合併衝突於 2.txt
自動合併失敗,修正衝忽然後提交修正的結果。
[root@arslinux-01 gitroot]# vim 2.txt
[root@arslinux-01 gitroot]# cat 2.txt
<<<<<<< HEAD
12sddsf2232
=======
>>>>>>> arslinux
  • 若是master分支和aming分支都對2.txt進行了編輯,當合並時會提示衝突,須要先解決衝突才能夠繼續合併。

  • 解決衝突的方法是在master分支下,編輯2.txt,改成aming分支裏面2.txt的內容。 而後提交2.txt,再合併aming分支。

  • 可是這樣有一個問題,萬一master分支更改的內容是咱們想要的呢? 能夠編輯2.txt內容,改成想要的,而後提交。切換到aming分支,而後合併master分支到aming分支便可(倒着合併)。合併分支有一個原則,那就是要把最新的分支合併到舊的分支。也就是說merge後面跟的分支名字必定是最新的分支。


  • git branch -d 分支名          刪除分支

  • git branch -D arslinux      強制刪除分支

[root@arslinux-01 gitroot]# git branch -d arslinux
已刪除分支 arslinux(曾爲 fcb0316)。

若是分支沒有合併,刪除以前會提示,那就不合並,強制刪除


22.10 遠程分支管理

使用分支的原則:

  • master 分支很是重要,線上發佈代碼用這個分支,平時咱們開發代碼不要在這個分支上。

  • 建立一個 dev 分支,專門用做開發,當發佈到線上以前,纔會把 dev 分支合併到 master

  • 開發人員應該在 dev 的基礎上再分支成我的分支,我的分支(在本身pc上)裏面開發代碼,而後合併到 dev 分支

clipboard.png

dev分支合併bob分支的命令是:

git checkout dev   //先切換到dev分支,而後

git merge bob


遠程分支:

  • git ls-remote origin          查看遠程分支

[root@arslinux-01 tmp]# git clone https://github.com/axxxxxx4xxxx/studygit.git
正克隆到 'studygit'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 15 (delta 0), reused 12 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
[root@arslinux-01 tmp]# cd studygit/
[root@arslinux-01 studygit]# git branch
* master
[root@arslinux-01 studygit]# git ls-remote origin
38e08903596878b892452d53aa96dda7b76a7c64HEAD
38e08903596878b892452d53aa96dda7b76a7c64refs/heads/dev
38e08903596878b892452d53aa96dda7b76a7c64refs/heads/master

——本地新建的分支若是不推送到遠程,對其餘人就是不可見的

——git clone 的時候默認只把 master 分支克隆下來,若是想把全部分支都克隆下來,須要手動建立,在本地建立和遠程分支對應的分支,使用 git checkout -b 分支名 origin/分支名,本地和遠程分支的名稱要一致

  • git checkout -b 分支名 origin/分支名          本地建立和遠程分支對應的分支

[root@arslinux-01 studygit]# git checkout -b dev origin/dev
分支 dev 設置爲跟蹤來自 origin 的遠程分支 dev。
切換到一個新分支 'dev'
[root@arslinux-01 studygit]# git branch
* dev
master

——對於 git push 分支分兩種狀況:

1)當本地分支和遠程分支一致時

git push 會把全部本地分支的變動一同推送到遠程,若是想只推送一個分支,使用 git push origin 分支名

2)當本地分支比遠程分支多

默認 git push 只推送本地和遠程一致的分支,想要把多出來的本地分支推送到遠程時,使用 git push origin 分支名  若是推送失敗,先用 git pull 抓取遠程的新提交

一致:

[root@arslinux-01 studygit]# echo "ssssssss">2.txt
[root@arslinux-01 studygit]# echo "bbbbbbbb">>2.txt
[root@arslinux-01 studygit]# cat 2.txt
ssssssss
bbbbbbbb
[root@arslinux-01 studygit]# git add 2.txt
[root@arslinux-01 studygit]# git commit -m "add 2.txt"
[dev e7340f2] add 2.txt
1 file changed, 2 insertions(+), 5 deletions(-)
[root@arslinux-01 studygit]# git push origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/axxxxx4xxxx/studygit.git
38e0890..e7340f2  dev -> dev

不一致:

[root@arslinux-01 studygit]# git branch dev2
[root@arslinux-01 studygit]# git checkout dev2
切換到分支 'dev2'
[root@arslinux-01 studygit]# echo "aaaaaaaaa" > 3.txt
[root@arslinux-01 studygit]# git add 3.txt
[root@arslinux-01 studygit]# git commit -m "add 3.txt"
[dev2 bdf6b7d] add 3.txt
1 file changed, 1 insertion(+)
create mode 100644 3.txt
[root@arslinux-01 studygit]# git push origin dev2
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xxxxxxx4xxxx/studygit.git
e7340f2..bdf6b7d  dev2 -> dev2


22.11 標籤管理

——標籤相似於快照功能,能夠給版本庫打一個標籤,記錄某個時刻庫的狀態,也能夠隨時恢復到該狀態

  • git tag v1.0          給master打一個標籤v1.0

  • git tag                  能夠查看全部的標籤

  • git show v1.0      查看標籤信息

[root@arslinux-01 studygit]# git checkout master
切換到分支 'master'
[root@arslinux-01 studygit]# git tag v1.0
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git show v1.0
commit 38e08903596878b892452d53aa96dda7b76a7c64
Author: arslinux <zyxxxxxxxx@qq.com>
Date:   Thu Jul 25 22:37:14 2019 +0800
change2 2.txt
diff --git a/2.txt b/2.txt
index 77d3d2a..07ced64 100644
--- a/2.txt
+++ b/2.txt
@@ -1,4 +1,5 @@
73737372hhdjdjd
thank you
go go go
+dlj;adkjf;adfas
ore wa neko ga suki

——tag 是針對 commit 來打標籤的,因此能夠針對歷史的 commit 來打 tag

  • git log --pretty=oneline                                  查看歷史 commit

  • git log --pretty=oneline --abbrev-commit     查看歷史 commit,用簡寫 commit

  • git tag v版本號 commit值                                 針對歷史 commit 打標籤

  • git tag -a v版本號 -m "描述" commit值             對標籤進行描述

  • git tag -d v版本號                                              刪除標籤

  • git push origin v版本號                                      推送指定標籤到遠程

  • git push --tag v版本號                                        推送全部標籤到遠程

  • git tag v版本號 -d                                               刪除本地標籤                                               

  • git push origin:refs/tags/v版本號                    刪除遠程標籤

[root@arslinux-01 studygit]# git log --pretty=oneline
38e08903596878b892452d53aa96dda7b76a7c64 change2 2.txt
cce24941a0e74d9c94cd05f1a59e6993ac54ca01 Update 2.txt
b2c0aa1d1b2749eeb0958ca772c378790651dd0f change README.md
6e4549cbe9f7d415eb219018b3fbbfc2e1e42c98 add 2.txt
f6afa16c437cf2917d220aace0e01963575b7383 first commit
[root@arslinux-01 studygit]# git log --pretty=oneline --abbrev-commit
38e0890 change2 2.txt
cce2494 Update 2.txt
b2c0aa1 change README.md
6e4549c add 2.txt
f6afa16 first commit
[root@arslinux-01 studygit]# git tag v0.8 6e4549cbe9f7d
[root@arslinux-01 studygit]# git tag
v0.8
v1.0
[root@arslinux-01 studygit]# git tag -a v0.1 -m "first tag" f6afa16c4
[root@arslinux-01 studygit]# git show v0.1
tag v0.1
Tagger: arslinux <zxxxxxx@qq.com>
Date:   Sat Jul 27 22:18:25 2019 +0800

first tag

commit f6afa16c437cf2917d220aace0e01963575b7383
Author: arslinux <zxxxxxxxxx@qq.com>
Date:   Thu Jul 25 22:07:51 2019 +0800

first commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..eb8db93
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# studygit
[root@arslinux-01 studygit]# git tag -d v0.1
已刪除 tag 'v0.1'(曾爲 7db3981)
[root@arslinux-01 studygit]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
* [new tag]         v1.0 -> v1.0
[root@arslinux-01 studygit]# git push --tag origin
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/arsenal4life/studygit.git
* [new tag]         v0.8 -> v0.8
[root@arslinux-01 studygit]# git tag  v0.8 -d
已刪除 tag 'v0.8'(曾爲 6e4549c)
[root@arslinux-01 studygit]# git tag
v1.0
[root@arslinux-01 studygit]# git push origin :refs/tags/v0.8
Username for 'https://github.com': arsenal4life
Password for 'https://arsenal4life@github.com':
To https://github.com/arsenal4life/studygit.git
- [deleted]         v0.8


22.12 git別名

  • git config --global alias.別名 命令              設置別名

  • git config --list                                          查看全部配置

  • git config --list |grep alias                          查看別名

  • git config --global --unset alias.別名          取消別名

[root@arslinux-01 studygit]# git config --global alias.ci commit
[root@arslinux-01 studygit]# echo "dafafa" >4.txt
[root@arslinux-01 studygit]# git add 4.txt
[root@arslinux-01 studygit]# git ci -m "add 4.txt"
[master 993ce4b] add 4.txt
1 file changed, 1 insertion(+)
create mode 100644 4.txt
[root@arslinux-01 studygit]# git config --global alias.br branch
[root@arslinux-01 studygit]# git br
dev
dev2
* master
[root@arslinux-01 studygit]# git config --global alias.co checkout
[root@arslinux-01 studygit]# git co dev
切換到分支 'dev'
[root@arslinux-01 studygit]# git config --list
user.email=zxxxxxxxx@qq.com
user.name=arslinux
push.default=simple
alias.ci=commit
alias.br=branch
alias.co=checkout
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/axxxxxxxxxxx/studygit.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.dev.remote=origin
branch.dev.merge=refs/heads/dev
[root@arslinux-01 studygit]# git config --list|grep alias
alias.ci=commit
alias.br=branch
alias.co=checkout
[root@arslinux-01 studygit]# git config --global --unset alias.br
[root@arslinux-01 studygit]# git br
git:'br' 不是一個 git 命令。參見 'git --help'。
您指的是這其中的某一個麼?
branch
var

——git config 的配置能夠在 /root/.gitconfig 中定義

查詢 log 小技巧:(顏色區分顯示)

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

clipboard.png


22.13 搭建git服務器

——github 畢竟是公開的,而私有倉庫又得花錢買。因此咱們能夠想辦法搭建一個私有的,只本身公司使用的。Gitlab 是個不錯的選擇。

一、安裝 git,arslinux-02 作爲服務端


[root@arslinux-02 ~]# yum install -y git

二、添加 git 用戶,並設置 shell 爲 /usr/bin/git-shell,目的是爲了避免讓 git 用戶遠程登錄


[root@arslinux-02 ~]# useradd -s /usr/bin/git-shell git

三、建立 authorized_keys 文件,並更改屬主、屬組和權限,用來存客戶端機器上的公鑰

[root@arslinux-02 ~]# cd /home/git/
[root@arslinux-02 git]# mkdir .ssh
[root@arslinux-02 git]# touch .ssh/authorized_keys
[root@arslinux-02 git]# chmod 600 .ssh/authorized_keys
[root@arslinux-02 git]# chown -R git:git .ssh

四、將公鑰添加到服務端 authorized_keys 文件中,並在客戶端嘗試鏈接

[root@arslinux-02 git]# vim .ssh/authorized_keys
[root@arslinux-01 ~]# ssh git@192.168.194.132
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.194.132 closed.

五、服務端建立 git 倉庫,建立裸倉庫

[root@arslinux-02 git]# cd /data/
[root@arslinux-02 data]# mkdir /data/gitroot
[root@arslinux-02 data]# cd /data/gitroot/
[root@arslinux-02 gitroot]# git init --bare sample.git
初始化空的 Git 版本庫於 /data/gitroot/sample.git/
[root@arslinux-02 gitroot]# ls
sample.git
[root@arslinux-02 gitroot]# chown -R git:git sample.git/

——git init --bare sample.git // 會建立一個裸倉庫,裸倉庫沒有工做區,由於服務器上的 Git 倉庫純粹是爲了共享,因此不讓用戶直接登陸到服務器上去改工做區,而且服務器上的Git倉庫一般都以.git結尾

——以上操做是在 git 服務器上作的,平時 git 服務器是不須要開發人員登陸修改代碼的,它僅僅是充當着一個服務器的角色,就像 github 同樣,平時操做都是在咱們本身的pc上作的

六、客戶端上克隆遠程倉庫

[root@arslinux-01 ~]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
warning: 您彷佛克隆了一個空版本庫。
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ll -a
總用量 4
drwxrwxr-x   3 root root   18 7月  28 12:19 .
dr-xr-x---. 10 root root 4096 7月  28 12:19 ..
drwxrwxr-x   7 root root  119 7月  28 12:19 .git

七、建立新文件到倉庫,推送到遠程

[root@arslinux-01 sample]# cp /etc/init.d/mysqld .
[root@arslinux-01 sample]# git add mysqld
[root@arslinux-01 sample]# git commit -m "add mysqld"
[master(根提交) 7f37e19] add mysqld
1 file changed, 378 insertions(+)
create mode 100755 mysqld
[root@arslinux-01 sample]# git push
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.84 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
* [new branch]      master -> master

若是 git push 提示沒有分支,可使用 git push origin master 來操做,會在裸倉庫建立新分支

八、再推送新文件就不會提示了

[root@arslinux-01 sample]# echo "dafasdfasdf" >222.txt
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "add 222.txt"
[master a9de871] add 222.txt
1 file changed, 1 insertion(+)
create mode 100644 222.txt
[root@arslinux-01 sample]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
7f37e19..a9de871  master -> master

九、不妨到 /tmp/ 目錄下,克隆服務端的 sample.git,能夠看到,客戶端新建文件已經推到服務端了

[root@arslinux-01 sample]# cd /tmp/
[root@arslinux-01 tmp]# git clone git@192.168.194.132:/data/gitroot/sample.git
正克隆到 'sample'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收對象中: 100% (6/6), 4.09 KiB | 0 bytes/s, done.
[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld

十、若是有多個服務器鏈接 git 服務端,並且其餘服務器對服務端作了更改,那麼客戶端能夠 git pull 來更新倉庫(/tmp/sample/ 和 /data/sample/ 就當作兩個服務器)

[root@arslinux-01 tmp]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# echo "435678" >> 222.txt
[root@arslinux-01 sample]# git add 222.txt
[root@arslinux-01 sample]# git commit -m "ch 222.txt"
[master 4e5ad08] ch 222.txt
1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# git push
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.194.132:/data/gitroot/sample.git
a9de871..4e5ad08  master -> master
[root@arslinux-01 sample]# cd
[root@arslinux-01 ~]# cd sample/
[root@arslinux-01 sample]# ls
222.txt  mysqld
[root@arslinux-01 sample]# git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
來自 192.168.194.132:/data/gitroot/sample
a9de871..4e5ad08  master     -> origin/master
更新 a9de871..4e5ad08
Fast-forward
222.txt | 1 +
1 file changed, 1 insertion(+)
[root@arslinux-01 sample]# cat 222.txt
dafasdfasdf
435678

這樣就能夠很是方便協同操做


22.14/22.15 安裝gitlab

gitlab 官網 https://about.gitlab.com/gitlab-com/

官方安裝文檔:https://about.gitlab.com/install/#centos-7

要求服務器內存很多於2g,不然會卡死

一、建立 gitlab 的安裝源

[root@arslinux-01 ~]# vim /etc/yum.repos.d/gitlab.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

二、安裝 gitlab


[root@arslinux-01 ~]# yum install -y gitlab-ce

三、自動配置 gitlab

[root@arslinux-01 ~]# gitlab-ctl reconfigure
[root@arslinux-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.194.130:27017   0.0.0.0:*               LISTEN      7504/mongod
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      7504/mongod
tcp        0      0 127.0.0.1:9100          0.0.0.0:*               LISTEN      14204/node_exporter
tcp        0      0 127.0.0.1:9229          0.0.0.0:*               LISTEN      14189/gitlab-workho
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 127.0.0.1:9168          0.0.0.0:*               LISTEN      14222/puma 3.12.0 (
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      12872/unicorn maste
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12951/nginx: master
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      7332/rpc.mountd
tcp        0      0 0.0.0.0:37361           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:8082          0.0.0.0:*               LISTEN      12888/sidekiq 5.2.7
tcp        0      0 127.0.0.1:9236          0.0.0.0:*               LISTEN      14209/gitaly
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7242/sshd
tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      15042/grafana-serve
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7675/master
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      12951/nginx: master
tcp        0      0 0.0.0.0:35038           0.0.0.0:*               LISTEN      7265/rpc.statd
tcp        0      0 127.0.0.1:9121          0.0.0.0:*               LISTEN      14233/redis_exporte
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      14240/prometheus
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      7256/zabbix_agentd
tcp        0      0 127.0.0.1:9187          0.0.0.0:*               LISTEN      14284/postgres_expo
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      7330/zabbix_server
tcp        0      0 127.0.0.1:9093          0.0.0.0:*               LISTEN      14275/alertmanager
tcp6       0      0 :::3306                 :::*                    LISTEN      7579/mysqld
tcp6       0      0 :::33836                :::*                    LISTEN      -
tcp6       0      0 :::53101                :::*                    LISTEN      7265/rpc.statd
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 ::1:9168                :::*                    LISTEN      14222/puma 3.12.0 (
tcp6       0      0 :::20048                :::*                    LISTEN      7332/rpc.mountd
tcp6       0      0 :::22                   :::*                    LISTEN      7242/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      7675/master
tcp6       0      0 :::2049                 :::*                    LISTEN      -
tcp6       0      0 :::10050                :::*                    LISTEN      7256/zabbix_agentd
tcp6       0      0 :::10051                :::*                    LISTEN      7330/zabbix_server
tcp6       0      0 :::10052                :::*                    LISTEN      7264/java
tcp6       0      0 :::9094                 :::*                    LISTEN      14275/alertmanager

四、關閉 gitlab,能夠看到 gitlab 相關服務

[root@arslinux-01 ~]# gitlab-ctl stop
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 1s, normally up
ok: down: grafana: 0s, normally up
ok: down: logrotate: 1s, normally up
ok: down: nginx: 0s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 1s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 1s, normally up

五、中止本地以前安裝的 nginx 服務,redis-server

[root@arslinux-01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl):                            [  肯定  ]
[root@arslinux-01 ~]# chkconfig nginx off
[root@arslinux-01 ~]# killall redis-server

六、啓動 gitlab(gitlab-ctl stop/restart/start/status)

[root@arslinux-01 ~]# gitlab-ctl start
ok: run: alertmanager: (pid 15894) 1s
ok: run: gitaly: (pid 15905) 0s
ok: run: gitlab-monitor: (pid 15923) 1s
ok: run: gitlab-workhorse: (pid 15925) 0s
ok: run: grafana: (pid 15932) 1s
ok: run: logrotate: (pid 15938) 0s
ok: run: nginx: (pid 15946) 1s
ok: run: node-exporter: (pid 15951) 0s
ok: run: postgres-exporter: (pid 15955) 1s
ok: run: postgresql: (pid 15960) 0s
ok: run: prometheus: (pid 15962) 1s
ok: run: redis: (pid 15976) 0s
ok: run: redis-exporter: (pid 15981) 1s
ok: run: sidekiq: (pid 15986) 0s
ok: run: unicorn: (pid 15992) 0s

七、網頁訪問 gitlab,只要輸入 ip 地址便可

若是出現 502 錯誤,那麼請檢查內存是否太小,通常不小於 4G

1.png

八、修改密碼後,登陸,默認用戶名 root,密碼就是剛剛修改的

登陸後就能夠建立項目

2.png

22.16 使用gitlab

gitlab經常使用命令:https://www.cnyunwei.cc/archives/1204

進入 gitlab,先建立組,再建立項目

一、建立組:

11.png

二、建立項目:

12.png

和 github 同樣,須要添加 ssh key,才能夠和 gitlab 通訊

13.png

三、設置 ssh key

右上角頭像——setting——SSH Keys

14.png

四、建立用戶

頂部中間扳手圖標(Admin Area)——New user

15.png

16.png

五、進入用戶編輯,能夠設定用戶密碼

17.png18.png

六、退出,用新建的用戶名密碼登陸,首次登陸須要修改密碼,能夠設成和原來同樣的密碼,而後就能夠建立項目了

19.png


22.17 gitlab備份和恢復

——gitlab 備份


[root@arslinux-01 ~]# gitlab-rake gitlab:backup:create

——備份的文件默認會放到 /var/opt/gitlab/backups/ 下

[root@arslinux-01 ~]# ls /var/opt/gitlab/backups/
1564299157_2019_07_28_12.1.1_gitlab_backup.tar
[root@arslinux-01 ~]# du -sh /var/opt/gitlab/backups/1564299157_2019_07_28_12.1.1_gitlab_backup.tar
132K/var/opt/gitlab/backups/1564299157_2019_07_28_12.1.1_gitlab_backup.tar

——gitlab 恢復

1)暫停 unicorn、sidekiq 服務

[root@arslinux-01 ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq

2)恢復 gitlab-rake gitlab:backup:restore BACKUP=xxxxx (這裏是一個編號,即備份文件的前綴)

[root@arslinux-01 ~]# gitlab-rake gitlab:backup:restore BACKUP=1564299157_2019_07_28_12.1.1

——再啓動服務 gitlab-ctl start

相關文章
相關標籤/搜索