版本控制,記錄若干文件內容變化,以便未來查閱特定版本修訂狀況php
版本管理工具發展史:cvs-->svn-->githtml
svn:全稱subversion,是一個開源版本控制系統,始於2000年node
git是Linux的創始人linus發起的,2005年發佈。python
git與svn不一樣在於git不須要依賴服務端就能夠工做,即git是分佈式的。mysql
githup是基於git的在線web頁面代碼託管平臺,能夠選擇付費服務。linux
gitlab能夠認爲是一個開源的github,兩種沒有直接關係。nginx
首先安裝subversiongit
[root@ying01 ~]# yum install -y subversion
建立項目目錄,並初始化文件,建立svn資源倉庫github
[root@ying01 ~]# mkdir -p /data/svnroot/myproject //建立svnroot/myproject多層目錄 [root@ying01 ~]# ls -la /data/svnroot/myproject 總用量 0 drwxr-xr-x 2 root root 6 8月 30 22:51 . drwxr-xr-x 3 root root 23 8月 30 22:51 .. [root@ying01 ~]# svnadmin create /data/svnroot/myproject/ //用svnadmin命令初始化文件,建立svn資源倉庫 [root@ying01 ~]# ls -la /data/svnroot/myproject 總用量 8 drwxr-xr-x 6 root root 86 8月 30 22:52 . drwxr-xr-x 3 root root 23 8月 30 22:51 .. drwxr-xr-x 2 root root 54 8月 30 22:52 conf drwxr-sr-x 6 root root 233 8月 30 22:52 db -r--r--r-- 1 root root 2 8月 30 22:52 format drwxr-xr-x 2 root root 231 8月 30 22:52 hooks drwxr-xr-x 2 root root 41 8月 30 22:52 locks -rw-r--r-- 1 root root 229 8月 30 22:52 README.txt
查看其目錄下的配置文件目錄;authz :控制權限; passwd :密碼文件 ;svnserve.conf :倉庫的配置文件web
[root@ying01 ~]# cd /data/svnroot/myproject/conf/ [root@ying01 conf]# ls authz passwd svnserve.conf
在authz文件中,增長如下內容
[root@ying01 conf]# vim authz [groups] //此處爲標誌,下面內容寫在此處 admins = ying,user1 //定義用戶組 [/] //根目錄,指的是 /data/svnroot/myproject/ @admins = rw //所定義用戶組,賦予rw權利 * = r //除admins組外,給全部用戶,r的權利 [myproject:/] //項目名稱,能夠是/data/svnroot/下多個項目 user1 = rw //user1用戶具備rw權利
編輯passwd文件,增長如下內容:
[root@ying01 conf]# vim passwd [users] aming = www123 //定義ying用戶的密碼 user1 = www1234 //定義user1的用戶密碼
配置svnserve.conf文件,添加如下內容
[root@ying01 conf]# vim svnserve.conf [general] //此處爲標誌,下面內容寫在此處 anon-access = none //匿名用戶,無權利 auth-access = write //用戶名密碼登陸,能夠寫 password-db = passwd //用戶的密碼存在passwd文件 authz-db = authz //權限控制在authz文件 realm = /data/svnroot/myproject //對此項目生效(處須要絕對路徑)
配置完文件,如今能夠啓動svn;
命令:svnserve -d -r /data/svnroot/ 解釋: -d :後臺模式(daemon mode) ; -r :要服務的目錄的根目錄
[root@ying01 conf]# svnserve -d -r /data/svnroot/ //啓動svn服務 [root@ying01 conf]# ps aux |grep svn root 2229 0.0 0.0 162240 656 ? Ss 23:55 0:00 svnserve -d -r /data/svnroot/ root 2231 0.0 0.0 112720 976 pts/0 S+ 23:56 0:00 grep --color=auto svn [root@ying01 conf]# netstat -lnpt |grep svn tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN 2229/svnserve
機器配置:
服務器:ying01 192.168.112.136
客戶端:ying02 192.168.112.138
首先在客戶端ying02安裝svn
[root@ying02 ]# yum install -y subversion
在客戶端ying02建立myproject項目工做副本,而且指定應戶名的方式建立;
[root@ying02 ~]# svn checkout svn://192.168.112.136/myproject --username=ying 認證領域: <svn://192.168.112.136:3690> /data/svnroot/myproject //此目錄爲ying01上定義的項目目錄 用戶名: ying 「ying」的密碼: ----------------------------------------------------------------------- 注意! 你的密碼,對於認證域: <svn://192.168.112.136:3690> /data/svnroot/myproject 只能明文保存在磁盤上! 若是可能的話,請考慮配置你的系統,讓 Subversion 能夠保存加密後的密碼。請參閱文檔以得到詳細信息。 你能夠經過在「/root/.subversion/servers」中設置選項「store-plaintext-passwords」爲「yes」或「no」, 來避免再次出現此警告。 ----------------------------------------------------------------------- 保存未加密的密碼(yes/no)?yes 取出版本 0。
此時在root下會生成一個myprojecct的目錄;注意:在哪一個目錄下,就在哪一個目錄下生成myproject目錄
[root@ying02 ~]# ls -ld myproject/ drwxr-xr-x 3 root root 28 8月 31 23:47 myproject/
在此目錄下建立一個文本,試傳輸到服務器
[root@ying02 ~]# cd myproject/ [root@ying02 myproject]# touch 123.txt [root@ying02 myproject]# svn add ./123.txt //先add預提交,添加到版本控制中心 A 123.txt [root@ying02 myproject]# svn commit -m "add 123.txt" //而後把文件123.txt上傳到服務器ying01 正在增長 123.txt 傳輸文件數據. 提交後的版本爲 1。
此時在服務器ying01上,查看服務器端內容
首先還須要在服務端建立工做目錄,此時自動在root下生成myproject目錄
[root@ying01 ~]# svn checkout svn://192.168.112.136/myproject/ 認證領域: <svn://192.168.112.136:3690> /data/svnroot/myproject 「root」的密碼: //此時切換root用戶,不用管,回車;由於你沒有指定以哪一個用戶身份建立 認證領域: <svn://192.168.112.136:3690> /data/svnroot/myproject 用戶名: ying 「ying」的密碼: ----------------------------------------------------------------------- 注意! 你的密碼,對於認證域: <svn://192.168.112.136:3690> /data/svnroot/myproject 只能明文保存在磁盤上! 若是可能的話,請考慮配置你的系統,讓 Subversion 能夠保存加密後的密碼。請參閱文檔以得到詳細信息。 你能夠經過在「/root/.subversion/servers」中設置選項「store-plaintext-passwords」爲「yes」或「no」, 來避免再次出現此警告。 ----------------------------------------------------------------------- 保存未加密的密碼(yes/no)?yes A 123.txt 取出版本 1。
如今到客戶端ying02上,給123.txt寫入內容
[root@ying02 myproject]# echo "abcdefg" > 123.txt [root@ying02 myproject]# cat 123.txt abcdefg [root@ying02 myproject]# svn commit -m "add 123.txt" //此時由於123.txt服務端已經存在,就不須要預提交 正在發送 123.txt 傳輸文件數據. 提交後的版本爲 2。
到服務端ying01上,更新,並查看123.txt內容
[root@ying01 myproject]# svn up 正在升級 '.': U 123.txt 更新到版本 2。 [root@ying01 myproject]# cat 123.txt abcdefg
在本地刪除文件:svn delete filename
[root@ying02 myproject]# svn delete 123.txt //刪除客戶端上的123.txt D 123.txt [root@ying02 myproject]# svn commit -m "delete 123.txt" //刪除服務端上的文件 正在刪除 123.txt 提交後的版本爲 3。
在服務端ying01 查看123.txt
[root@ying01 myproject]# ls 123.txt [root@ying01 myproject]# svn up //更新後,123.txt 文件刪除 正在升級 '.': D 123.txt 更新到版本 3。
查看變動日誌:svn log
[root@ying01 myproject]# svn log ---------------------------------------------------------- r3 | ying | 2018-08-30 23:08:32 +0800 (四, 2018-08-30) | 1 行 delete 123.txt ------------------------------------------------------------------------ r2 | ying | 2018-08-30 23:05:15 +0800 (四, 2018-08-30) | 1 行 add 123.txt ------------------------------------------------------------------------ r1 | ying | 2018-08-30 22:13:23 +0800 (四, 2018-08-30) | 1 行 add 123.txt ------------------------------------------------------------------------
打開官網下載最新版TortoiseSVN:https://tortoisesvn.net/index.zh.html
下載中文簡體的語言包
軟件安裝後,在桌面建立一個myproject的文件夾,並右鍵,選擇SVN檢出
填寫服務端的庫,而後肯定
此時發現錯誤
查看svnserve是否啓動,讓其啓動狀態
[root@ying01 ~]# netstat -lnpt |grep svn [root@ying01 ~]# svnserve -d -r /data/svnroot/ [root@ying01 ~]# netstat -lnpt |grep svn tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN 1864/svnserve
此時再次檢出,發現能夠了。
配置完畢後,如今開始作測試;在桌面myproject的文件夾建立test.txt
點擊右鍵,選擇TortoiseSVN > 加入
此時文件上出現藍色加號(+),在點擊右鍵,選擇SVN提交
此時添加已經完成,點擊肯定
此時提交已經完成,點擊肯定。發現藍色加號,已經變成綠色對號
此時客戶端已經加載完成,如今服務端,進行查看
[root@ying01 myproject]# ls //test.txt沒有在 123.txt [root@ying01 myproject]# svn up //更新後,發現存在 正在升級 '.': A test.txt 更新到版本 16。 [root@ying01 myproject]# ls 123.txt test.txt [root@ying01 myproject]# cat test.txt 123456[root@ying01 myproject]#
安裝git
[root@ying01 ~]# yum install -y git
建立gitroot目錄,並在此目錄下初始化
[root@ying01 ~]# mkdir /data/gitroot [root@ying01 ~]# cd /data/gitroot/ [root@ying01 gitroot]# git init //初始化倉庫,此目錄下爲git倉庫 初始化空的 Git 版本庫於 /data/gitroot/.git/ [root@ying01 gitroot]# ls -la 總用量 0 drwxr-xr-x 3 root root 18 9月 3 08:18 . drwxr-xr-x. 13 root root 179 9月 3 08:18 .. drwxr-xr-x 7 root root 119 9月 3 08:18 .git [root@ying01 gitroot]# ls [root@ying01 gitroot]# ls .git/ branches config description HEAD hooks info objects refs
此時新建在gitroot下新建1.txt文件,可是提交的時候出現錯誤;
[root@ying01 gitroot]# echo AAAAAAAA > 1.txt [root@ying01 gitroot]# cat 1.txt AAAAAAAA [root@ying01 gitroot]# git add 1.txt [root@ying01 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@ying01.(none)')
按照操做提示,定義郵箱,以及用戶名
[root@ying01 gitroot]# git config --global user.email "txwd188@126.com" [root@ying01 gitroot]# git config --global user.name "ying"
此時再提交1.txt
[root@ying01 gitroot]# git commit -m "add 1.txt" [master(根提交) 07a22e9] add 1.txt 1 file changed, 1 insertion(+) create mode 100644 1.txt
此時在1.txt文件中,追加內容;並添加到倉庫、提交
[root@ying01 gitroot]# echo BBBBBBBB >> 1.txt [root@ying01 gitroot]# git add 1.txt [root@ying01 gitroot]# git commit -m "add 1.txt agin" [master 3036bb8] add 1.txt agin 1 file changed, 1 insertion(+)
git status:查看當前倉庫中的狀態,好比是否有改動的文件
[root@ying01 gitroot]# git status # 位於分支 master 無文件要提交,乾淨的工做區
再增長內容,可是不加載、不提交時候的狀態
[root@ying01 gitroot]# echo CCCCCCCC >> 1.txt [root@ying01 gitroot]# git status # 位於分支 master # 還沒有暫存以備提交的變動: # (使用 "git add <file>..." 更新要提交的內容) # (使用 "git checkout -- <file>..." 丟棄工做區的改動) # # 修改: 1.txt # 修改還沒有加入提交(使用 "git add" 和/或 "git commit -a")
git diff :能夠對比1.txt本次修改了什麼內容,相比較倉庫裏面的版本
[root@ying01 gitroot]# git diff 1.txt diff --git a/1.txt b/1.txt index 0e722fb..0c1d7ea 100644 --- a/1.txt +++ b/1.txt @@ -1,2 +1,3 @@ AAAAAAAA BBBBBBBB +CCCCCCCC
在增長1.txt到倉庫,並commit提交
[root@ying01 gitroot]# git add 1.txt [root@ying01 gitroot]# git commit -m "add 1.txt agin" [master 2f6c78d] add 1.txt agin 1 file changed, 1 insertion(+)
再增長一行內容到1.txt,加載,並提交
[root@ying01 gitroot]# echo DDDDDDDD >> 1.txt [root@ying01 gitroot]# git add 1.txt [root@ying01 gitroot]# cat 1.txt AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD [root@ying01 gitroot]# git commit -m "ch 1.txt agin" [master e70c0fb] ch 1.txt agin 1 file changed, 1 insertion(+
git log :查看全部提交記錄;格式比較繁瑣,有以前定義的郵箱和用戶名
[root@ying01 gitroot]# git log commit e70c0fbdff1ea50fedd4428df1c4b48531fc29d2 Author: ying <txwd188@126.com> Date: Mon Sep 3 09:00:20 2018 +0800 ch 1.txt agin commit 2f6c78df82035720f2dea41d9575e079b464d015 Author: ying <txwd188@126.com> Date: Mon Sep 3 08:55:48 2018 +0800 add 1.txt agin commit 3036bb8704eab42771f571154dd70ddd6fa85672 Author: ying <txwd188@126.com> Date: Mon Sep 3 08:51:21 2018 +0800 add 1.txt agin commit 07a22e9fde4e71dc803d32af3002ef6049a0c908 Author: ying <txwd188@126.com> Date: Mon Sep 3 08:49:20 2018 +0800 add 1.txt [root@ying01 gitroot]# [root@ying01 gitroot]# cat /root/.gitconfig //在/root/.gitconfig文件裏面就是定義的郵箱和用戶名 [user] email = txwd188@126.com name = ying
查看日誌的時候,能夠定義一行:git log --pretty=oneline
[root@ying01 gitroot]# git log --pretty=oneline e70c0fbdff1ea50fedd4428df1c4b48531fc29d2 ch 1.txt agin 2f6c78df82035720f2dea41d9575e079b464d015 add 1.txt agin 3036bb8704eab42771f571154dd70ddd6fa85672 add 1.txt agin 07a22e9fde4e71dc803d32af3002ef6049a0c908 add 1.txt [root@ying01 gitroot]# cat 1.txt AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
回退版本,其中後面跟的字符串是簡寫: git reset --hard ID
root@ying01 gitroot]# git reset --hard 2f6c78df //回退到ID號:2f6c78df這一步 HEAD 如今位於 2f6c78d add 1.txt agin [root@ying01 gitroot]# git log --pretty=oneline //此時自動退到:2f6c78df這一行 2f6c78df82035720f2dea41d9575e079b464d015 add 1.txt agin 3036bb8704eab42771f571154dd70ddd6fa85672 add 1.txt agin 07a22e9fde4e71dc803d32af3002ef6049a0c908 add 1.txt [root@ying01 gitroot]# cat 1.txt //1.txt內容也相應的退到此步 AAAAAAAA BBBBBBBB CCCCCCCC
git reflog :查看全部歷史版本
[root@ying01 gitroot]# git reflog 2f6c78d HEAD@{0}: reset: moving to 2f6c78df e70c0fb HEAD@{1}: commit: ch 1.txt agin 2f6c78d HEAD@{2}: commit: add 1.txt agin 3036bb8 HEAD@{3}: commit: add 1.txt agin 07a22e9 HEAD@{4}: commit (initial): add 1.txt [root@ying01 gitroot]# git reset --hard e70c0fb //直接恢復到e70c0fb這一行 HEAD 如今位於 e70c0fb ch 1.txt agin [root@ying01 gitroot]# git log --pretty=oneline //查看日誌,發現這一行存在 e70c0fbdff1ea50fedd4428df1c4b48531fc29d2 ch 1.txt agin 2f6c78df82035720f2dea41d9575e079b464d015 add 1.txt agin 3036bb8704eab42771f571154dd70ddd6fa85672 add 1.txt agin 07a22e9fde4e71dc803d32af3002ef6049a0c908 add 1.txt [root@ying01 gitroot]# cat 1.txt //查看1.txt,看到以前的DDDDDDD又恢復 AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
git checkout -- 1.txt :恢復1.txt
[root@ying01 gitroot]# rm -f 1.txt //刪除1.txt [root@ying01 gitroot]# ls [root@ying01 gitroot]# git checkout //檢索列表狀態 D 1.txt [root@ying01 gitroot]# git checkout -- 1.txt //直接恢復1.txt [root@ying01 gitroot]# ls 1.txt
再寫入一行內容,增長到倉庫,有撤銷增長 :git reset HEAD 1.txt
root@ying01 gitroot]# echo EEEEEEEE >> 1.txt [root@ying01 gitroot]# git add 1.txt //加載到倉庫 [root@ying01 gitroot]# git reset HEAD 1.txt 重置後撤出暫存區的變動: M 1.txt [root@ying01 gitroot]# cat 1.txt AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD EEEEEEEE [root@ying01 gitroot]# git checkout -- 1.txt //從倉庫中提出來 [root@ying01 gitroot]# cat 1.txt //EEE這行消失 AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
從倉庫中把1.txt刪除
[root@ying01 gitroot]# ls 1.txt [root@ying01 gitroot]# git rm 1.txt //從工做區刪除 rm '1.txt' [root@ying01 gitroot]# ls [root@ying01 gitroot]# git commit -m "delete 1.txt" //從倉庫中刪除1.txt [master 3e10741] delete 1.txt 1 file changed, 4 deletions(-) delete mode 100644 1.txt [root@ying01 gitroot]# ls [root@ying01 gitroot]# git checkout -- 1.txt //此時倉庫中已經沒有1.txt error: pathspec '1.txt' did not match any file(s) known to git.
能夠這樣恢復1.txt
[root@ying01 gitroot]# git log --pretty=oneline //查看git日誌 3e10741cb39e6d097a03da1e0fd811a9f4fc2866 delete 1.txt e70c0fbdff1ea50fedd4428df1c4b48531fc29d2 ch 1.txt agin 2f6c78df82035720f2dea41d9575e079b464d015 add 1.txt agin 3036bb8704eab42771f571154dd70ddd6fa85672 add 1.txt agin 07a22e9fde4e71dc803d32af3002ef6049a0c908 add 1.txt [root@ying01 gitroot]# git reset --hard e70c0fb //定義相應的ID,恢復到相應的位置 HEAD 如今位於 e70c0fb ch 1.txt agin [root@ying01 gitroot]# ls 1.txt [root@ying01 gitroot]# cat 1.txt AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
如今網站https://github.com ,註冊一個帳號
點擊註冊後,密碼有問題,從新填寫
選擇無限制免費公共存儲庫
點擊繼續,填寫一些信息,也能夠跳過此頁面
註冊成功,須要在郵箱裏面激活
點擊右上角+(加號),選擇 New repository 建立新庫
此時出現以下頁面,裏面有三種狀況的操做方法
如今設置祕鑰,在linux虛擬機上生成公鑰
[root@ying01 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:2LLmPK0JDYsyDCzScLOR54W8a+6EvaPz/dvo/vpvxWk root@ying01 The key's randomart image is: +---[RSA 2048]----+ | | | o . | |. = + . | |.+ * o o | |+.o + o S . . | |= + = o E | |o.o B +. o | | o.+.B...o . | | .==.**B=+o. | +----[SHA256]-----+ [root@ying01 ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+X/2tfggcYIe/3qp98blHj4ySD0GbVmP5QfU7tXXMbE/+FnktHOA3l9+noxXxNXFpaDjc4k+R9TV1R4yG8U0+bx8zucaVTSQWgFCiwDlKhY4pUgfQePtNX+AVUo7yf0+ysz7P3cyUTIInHORB2R/DoKzcxMEM9AHkb//G/UtLaRBhLLWhNGz/R8S5ZhdsC3+X+yKKDVffua8RWkAqevntf4lWz6KEYbuTjxzM7cOXOrHx0/w3/qtvD/Vee+I7vZHkCdqwMfQxn9pTh6c3RwBwcx9jzbJJ7YLV5KmOx0QqSK8qHylgjuO2ZS1wF1+eTdO1D2zP2aEykF6dDNhzQRb5 root@ying01
點擊右邊的符號,選擇setting,進入設置頁面
按下圖選擇 SSH keys
選擇公鑰的名稱,把以前在linux虛擬機上生成的祕鑰復,制到方框內,點擊Add SSH keys 加載此祕鑰
此時有生成新的祕鑰
在linux虛擬機上,也創建一個目錄(倉庫),名稱和網頁設置的倉庫名保持一致
[root@ying01 ~]# cd /tmp/ [root@ying01 tmp]# mkdir feng [root@ying01 tmp]# cd feng/
此時按照官網上的操做步驟(第一種,初始化),使本地和遠程倉庫相連
[root@ying01 feng]# echo "# feng" >> README.md [root@ying01 feng]# ls README.md [root@ying01 feng]# git init 初始化空的 Git 版本庫於 /tmp/feng/.git/ [root@ying01 feng]# ls -la 總用量 8 drwxr-xr-x 3 root root 35 9月 3 17:16 . drwxrwxrwt. 20 root root 4096 9月 3 17:15 .. drwxr-xr-x 7 root root 119 9月 3 17:16 .git -rw-r--r-- 1 root root 7 9月 3 17:16 README.md [root@ying01 feng]# git add README.md [root@ying01 feng]# git commit -m "first commit" [master(根提交) 42a996b] first commit 1 file changed, 1 insertion(+) create mode 100644 README.md [root@ying01 feng]# git remote add origin https://github.com/feng-01/feng.git [root@ying01 feng]# git push -u origin master //推送到遠程 Username for 'https://github.com': feng-01 //須要登陸用戶 Password for 'https://feng-01@github.com': //登陸密碼 Counting objects: 3, done. Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/feng-01/feng.git * [new branch] master -> master 分支 master 設置爲跟蹤來自 origin 的遠程分支 master。
刷新遠程倉庫的頁面,此時能夠看到 README.md 已經被推送到遠程倉庫
此時咱們有點疑問,爲何以前設置了祕鑰,爲何還要須要輸入登陸帳號以及密碼?
首先,按照官網的測試 SSH keys 的說明,按部操做
[root@ying01 feng]# ssh -T git@github.com The authenticity of host 'github.com (13.229.188.59)' can't be established. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts. Hi feng-01! You've successfully authenticated, but GitHub does not provide shell access.
從上面最後一行,已經看出設置成功,可是我繼續測試,仍是須要輸入登陸帳號和密碼;如今須要在打開git/config文件
[root@ying01 feng]# vim .git/config [remote "origin"] //默認 url = https://github.com/feng-01/feng.git fetch = +refs/heads/*:refs/remotes/origin/* [remote "origin"] url = git@github.com:feng-01/feng.git //更改後 fetch = +refs/heads/*:refs/remotes/origin/*
此時新建一個文件,而後推送,此時可以使用祕鑰登陸
[root@ying01 feng]# echo "123456" > 1.txt [root@ying01 feng]# git add 8.txt [root@ying01 feng]# git commit -m "add 1.txt" [master 3cc0df7] add 1.txt 1 file changed, 1 insertion(+) create mode 100644 1.txt [root@ying01 feng]# git push //下面的提示:永久性地將IP地址'13 .250.177.223'的RSA主機密鑰添加到已知主機列表中。 Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts. Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 215 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To git@github.com:feng-01/feng.git 508a237..3cc0df7 master -> master
怎麼刪除gitgub遠程倉庫的文件呢?
在github上只能刪除倉庫,卻沒法刪除文件夾或文件, 因此只能經過命令來解決。
[root@ying01 feng]# git rm 1.txt //刪除1.txt rm '1.txt' [root@ying01 feng]# git commit -m "delete 1.txt" [master 4fed97d] delete 1.txt 1 file changed, 1 deletion(-) delete mode 100644 1.txt [root@ying01 feng]# git push //推送到github Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (1/1), done. Writing objects: 100% (2/2), 224 bytes | 0 bytes/s, done. Total 2 (delta 0), reused 0 (delta 0) To git@github.com:feng-01/feng.git 400d346..4fed97d master -> master [root@ying01 feng]#
此時在github頁面,刷新,能夠看到1.txt已經被刪除
克隆遠程倉庫,此倉庫爲開源庫 python zer0_out庫,由於開源只有讀的權限。
[root@ying01 home]# git clone git@github.com:yifeif/zero_out.git 正克隆到 'zero_out'... remote: Counting objects: 18, done. remote: Compressing objects: 100% (15/15), done. remote: Total 18 (delta 1), reused 14 (delta 0), pack-reused 0 接收對象中: 100% (18/18), 8.75 KiB | 0 bytes/s, done. 處理 delta 中: 100% (1/1), done. [root@ying01 home]# ls mysql nfstestdir pure-ftp user10 user12 user14 user17 user3 user7 virftp zero_out nba php-fpm user1 user11 user13 user15 user18 user4 user8 ying111 [root@ying01 home]# ls zero_out/ LICENSE MANIFEST.in pip_pkg.sh README.md setup.py tensorflow_zero_out [root@ying01 zero_out]# git commit -m "change readme.md" [master 19cb892] change readme.md 1 file changed, 3 insertions(+) [root@ying01 zero_out]# git push Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts. ERROR: Permission to yifeif/zero_out.git denied to feng-01. fatal: Could not read from remote repository. Please make sure you have the correct access rights //無此權限,說明只有讀的權限 and the repository exists.
克隆有讀寫權限的庫,好比在github上建立本身的庫
在本地克隆此遠程倉庫
[root@ying01 home]# git clone git@github.com:feng-01/qingling.git 正克隆到 'qingling'... remote: Counting objects: 11, done. remote: Compressing objects: 100% (7/7), done. remote: Total 11 (delta 0), reused 0 (delta 0), pack-reused 0 接收對象中: 100% (11/11), 7.88 KiB | 0 bytes/s, done. [root@ying01 home]# ls qingling/ LICENSE MANIFEST.in pip_pkg.sh README.md setup.py [root@ying01 home]# cd qingling/ [root@ying01 qingling]# cat README.md //注意此文件內容,如下就按此文件作測試 To create the package, run ./pip_pkg.sh
在README.md寫入部份內容,並推送到遠程倉庫上
[root@ying01 qingling]# echo "AAAAABBBB" >> README.md [root@ying01 qingling]# cat README.md To create the package, run ./pip_pkg.sh AAAAABBBB [root@ying01 qingling]# git add README.md [root@ying01 qingling]# git commit -m "change README.md" [master cc29e4d] change README.md 1 file changed, 1 insertion(+) [root@ying01 qingling]# git push Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To git@github.com:feng-01/qingling.git f39ca7f..cc29e4d master -> master
在github上的qingling倉庫中的README.md查看其內容變化
那麼在github上的qingling倉庫中的README.md,編輯器內容
增長上面的內容後,點擊 commit changes
在本地把遠程倉庫的內容拉回來,用git pull
[root@ying01 qingling]# git pull remote: Counting objects: 3, done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. 來自 github.com:feng-01/qingling cc29e4d..c8d7070 master -> origin/master 更新 cc29e4d..c8d7070 Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+) [root@ying01 qingling]# cat README.md //其內容和遠程倉庫裏的內容同樣 To create the package, run ./pip_pkg.sh AAAAABBBB 12345678 CCCCCCCC
查看分支:git branch
[root@ying01 ~]# cd /data/gitroot/ [root@ying01 gitroot]# git branch * master
建立分支:git branch 分支名
[root@ying01 gitroot]# git branch ying [root@ying01 gitroot]# git branch * master ying
切換分支:git checkout 分支名
[root@ying01 gitroot]# ls 1.txt [root@ying01 gitroot]# git checkout ying 切換到分支 'ying' [root@ying01 gitroot]# git branch //星號表明 目前所在分支 master * ying [root@ying01 gitroot]# ls 1.txt
在分支ying 上建立 2.txt。可是切換到master分支上卻沒有2.txt,說明分支是相互獨立的
[root@ying01 gitroot]# echo "how are you" > 2.txt [root@ying01 gitroot]# git add 2.txt [root@ying01 gitroot]# git commit -m "add 2.txt" [ying c2cdd92] add 2.txt 1 file changed, 1 insertion(+) create mode 100644 2.txt [root@ying01 gitroot]# ls 1.txt 2.txt [root@ying01 gitroot]# git checkout master 切換到分支 'master' [root@ying01 gitroot]# ls //master分支上沒有2.txt 1.txt [root@ying01 gitroot]# git branch * master ying
合併分支:git merge 用戶名
[root@ying01 gitroot]# git merge ying 更新 e70c0fb..c2cdd92 Fast-forward 2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 2.txt [root@ying01 gitroot]# ls //此時master分支上,也出現2.txt 1.txt 2.txt [root@ying01 gitroot]# git branch * master ying
在master分支上的2.txt,增長內容,
[root@ying01 gitroot]# echo "how do you do" >> 2.txt [root@ying01 gitroot]# cat 2.txt how are you how do you do [root@ying01 gitroot]# git add 2.txt [root@ying01 gitroot]# comm comm command [root@ying01 gitroot]# git commit -m "ch 2.txt" [master bb9d5c0] ch 2.txt 1 file changed, 1 insertion(+)
在ying分支上的2.txt也增長內容
[root@ying01 gitroot]# git checkout ying 切換到分支 'ying' [root@ying01 gitroot]# echo "weclome to china" >> 2.txt [root@ying01 gitroot]# git add 2.txt [root@ying01 gitroot]# git commit -m "ch 2.txt" [ying e088a46] ch 2.txt 1 file changed, 1 insertion(+) [root@ying01 gitroot]# cat 2.txt how are you weclome to china
回到master分支上,合併ying分支,結果又衝突;由於只有2分支內容同樣才能合併。
[root@ying01 gitroot]# git checkout master 切換到分支 'master' [root@ying01 gitroot]# cat 2.txt how are you how do you do [root@ying01 gitroot]# git merge ying 自動合併 2.txt 衝突(內容):合併衝突於 2.txt 自動合併失敗,修正衝忽然後提交修正的結果。 [root@ying01 gitroot]# cat 2.txt how are you <<<<<<< HEAD how do you do ======= weclome to china >>>>>>> ying [root@ying01 gitroot]# vim 2.txt //改成個ying分支同樣的內容 how are you weclome to china
此時master和ying分支上內容同樣,此時合併
[root@ying01 gitroot]# git commit -a [master 147955f] Merge branch 'ying' [root@ying01 gitroot]# git add 2.txt [root@ying01 gitroot]# git commit -m "ch 2.txt" # 位於分支 master 無文件要提交,乾淨的工做區 [root@ying01 gitroot]# git merge ying //合併成功 Already up-to-date. [root@ying01 gitroot]# git checkout 2.txt [root@ying01 gitroot]# cat 2.txt how are you weclome to china
刪除分支:git branch -d 分支名
[root@ying01 gitroot]# git branch -d ying 已刪除分支 ying(曾爲 e088a46)。 [root@ying01 gitroot]# git branch * master
強制刪除分支:git branch -D 分支名
[root@ying01 gitroot]# git branch ying01 [root@ying01 gitroot]# git branch ying02 [root@ying01 gitroot]# git branch * master ying01 ying02 [root@ying01 gitroot]# git branch -D ying01 已刪除分支 ying01(曾爲 147955f)。 [root@ying01 gitroot]# git branch -d ying02 已刪除分支 ying02(曾爲 147955f)。 [root@ying01 gitroot]#
分支使用的原則
- master分支是很是重要的,線上發佈代碼使用master分支。平時開發代碼不要在這個分支上
- 建立一個dev分支,專門用做開發,只有當發佈到線上以前,纔會把dev分支合併到master
- 開發人員應該在dev的基礎上再分支成我的分支,在我的分支裏面開發代碼,而後合併到dev分支。
好比dev合併bob分支(dev,bob分支事先建立好):
如今遠程倉庫,建立一個新的文件aaa.txt
再建立一個分支dev
在本地倉庫下,把倉庫更新內容pull下來,可是本地仍是隻能看到一個分支master
[root@ying01 ~]# cd /tmp/feng/ [root@ying01 feng]# ls README.md [root@ying01 feng]# git branch //查看分支,此時只有maser * master [root@ying01 feng]# git pull //從遠程推送 remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. 來自 github.com:feng-01/feng 4fed97d..2e97337 master -> origin/master * [新分支] dev -> origin/dev 更新 4fed97d..2e97337 Fast-forward aaa.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 aaa.txt [root@ying01 feng]# git branch //仍是隻有master分支 * master
查看遠程分支:git ls-remote origin
[root@ying01 feng]# git ls-remote origin 2e973378072fdc2f5f782652420a521ffe5995a7 HEAD 2e973378072fdc2f5f782652420a521ffe5995a7 refs/heads/dev 2e973378072fdc2f5f782652420a521ffe5995a7 refs/heads/master
取出遠程分支dev:git checkout -b dev origin/dev
[root@ying01 feng]# git checkout -b dev origin/dev 分支 dev 設置爲跟蹤來自 origin 的遠程分支 dev。 切換到一個新分支 'dev' [root@ying01 feng]# git branch * dev master [root@ying01 feng]# ls aaa.txt README.md
在dev分支下,建立bbb.txt,並推送到遠程倉庫
[root@ying01 feng]# echo "12345678" > bbb.txt [root@ying01 feng]# git add bbb.txt [root@ying01 feng]# git commit -m "ch bbb.txt" [dev 038d30b] ch bbb.txt 1 file changed, 1 insertion(+) create mode 100644 bbb.txt [root@ying01 feng]# git push Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 303 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:feng-01/feng.git 2e97337..038d30b dev -> dev
dev分支下的文件,裏面有bbb.txt
由於是在dev分支下建立的分支,因此推送上來,master分支文件未變,可是master本地有變化,此時master也變化
從新建立一個分支dev2,並建立ccc.txt
[root@ying01 feng]# git branch dev2 [root@ying01 feng]# git branch * dev dev2 master [root@ying01 feng]# git checkout dev2 切換到分支 'dev2' [root@ying01 feng]# ls aaa.txt bbb.txt README.md [root@ying01 feng]# echo "88888888" > ccc.txt [root@ying01 feng]# git add ccc.txt [root@ying01 feng]# git commit -m "add ccc.txt" [dev2 e90c679] add ccc.txt 1 file changed, 1 insertion(+) create mode 100644 ccc.txt
只推送到dev2,這樣即便其餘分支有變化,遠程倉庫只是的dev2分支有變化
[root@ying01 feng]# git push origin dev2 Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To git@github.com:feng-01/feng.git * [new branch] dev2 -> dev2
在遠程倉庫能夠看到,新的分支dev2,以及新增長的ccc.txt文件
定義:
標籤相似於快照功能,能夠給版本庫打一個標籤,記錄某個時刻庫的狀態。也能夠隨時恢復到該狀態。
定義標籤須要在master下; 定義標籤:git tag v1.0
[root@ying01 feng]# git checkout master //切換到master 切換到分支 'master' [root@ying01 feng]# git tag v1.0 //定義標籤 [root@ying01 feng]# git tag //查看標籤 v1.0
查看指定標籤信息:git show v1.0
[root@ying01 feng]# git show v1.0 commit 2e973378072fdc2f5f782652420a521ffe5995a7 //標籤是針對的commit Author: feng-01 <42930606+feng-01@users.noreply.github.com> Date: Tue Sep 4 23:43:01 2018 +0800 Create aaa.txt diff --git a/aaa.txt b/aaa.txt new file mode 100644 index 0000000..43bcdaa --- /dev/null +++ b/aaa.txt @@ -0,0 +1,4 @@ +AAAAAAA +BBBBBBB +CCCCCCCC +DDDDDDDD [root@ying01 feng]# git log --pretty=oneline //查看master下的commit 2e973378072fdc2f5f782652420a521ffe5995a7 Create aaa.txt 4fed97dd8fe02fccf4af2456a7b56bf348811fc9 delete 1.txt 08bff06220b25b0a33c073e506bd77c967d6f2ae add 1.txt 42a996bd16c5095439e78bd92d5e69d737abb8e4 first commit
查看歷史的commit信息
[root@ying01 feng]# git log --pretty=oneline --abbrev-commit 2e97337 Create aaa.txt 4fed97d delete 1.txt 08bff06 add 1.txt 42a996b first commit
針對歷史commit打標籤:git tag v0.8 4fed97d
[root@ying01 feng]# git tag v0.8 4fed97d [root@ying01 feng]# git tag v0.8 v1.0
能夠對標籤進行描述
[root@ying01 feng]# git tag -a v0.1 -m "first tag" 2e97337 [root@ying01 feng]# git tag v0.1 v0.8 v1.0 [root@ying01 feng]# git show v0.1 //查看vtag v0.1標籤信息 Tagger: ying <txwd188@126.com> Date: Wed Sep 5 00:22:25 2018 +0800 first tag //描述信息 commit 2e973378072fdc2f5f782652420a521ffe5995a7 Author: feng-01 <42930606+feng-01@users.noreply.github.com> Date: Tue Sep 4 23:43:01 2018 +0800 Create aaa.txt diff --git a/aaa.txt b/aaa.txt new file mode 100644 index 0000000..43bcdaa --- /dev/null +++ b/aaa.txt @@ -0,0 +1,4 @@ +AAAAAAA +BBBBBBB +CCCCCCCC +DDDDDDDD
刪除標籤:git tag -d v0.1
[root@ying01 feng]# git tag -d v0.1 已刪除 tag 'v0.1'(曾爲 5462580) [root@ying01 feng]# git tag v0.8 v1.0
此時遠程倉庫尚未標籤
把指定的標籤推送到github上的倉庫裏: git push origin v1.0
[root@ying01 feng]# git push origin v1.0 Total 0 (delta 0), reused 0 (delta 0) To git@github.com:feng-01/feng.git * [new tag] v1.0 -> v1.0
把V1.0標籤推送上去後,刷新頁面
把全部的標籤推送到遠程倉庫:git push --tag origin
[root@ying01 feng]# git push --tag origin Total 0 (delta 0), reused 0 (delta 0) To git@github.com:feng-01/feng.git * [new tag] v0.8 -> v0.8
此時遠程倉庫下,多了v0.8
在本地刪除一個標籤,遠程也刪除標籤:git push origin :refs/tags/v0.8
[root@ying01 feng]# git tag -d v0.8 已刪除 tag 'v0.8'(曾爲 4fed97d) [root@ying01 feng]# git tag v1.0 [root@ying01 feng]# git push origin :refs/tags/v0.8 To git@github.com:feng-01/feng.git - [deleted] v0.8
git下命令好比checkout過長,不易拼寫,用別名能夠提升咱們的工做效率。
git定義別名的語句:git config --global alias.br branch
[root@ying01 feng]# git config --global alias.br branch //定義branch的別名爲br [root@ying01 feng]# git br dev dev2 * master [root@ying01 feng]# ls aaa.txt README.md [root@ying01 feng]# git config --global alias.ci commit //定義commit的別名爲ci [root@ying01 feng]# echo "99999999" > ddd.txt [root@ying01 feng]# git add ddd.txt [root@ying01 feng]# git ci -m "add ddd.txt" [master 76a9c9c] add ddd.txt 1 file changed, 1 insertion(+) create mode 100644 ddd.txt [root@ying01 feng]# git config --global alias.co checkout //定義checkout的別名爲co [root@ying01 feng]# git co dev 切換到分支 'dev'
其實這些命令都會在git配置文件中生成,固然也能夠直接在配置文件中填寫
[root@ying01 feng]# vim /root/.gitconfig [user] email = txwd188@126.com name = ying [push] default = matching [alias] br = branch ci = commit co = checkout
小技巧:能夠針對 log的別名設置
[root@ying01 feng]# 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"
效果以下:
查看git別名清單
[root@ying01 feng]# git config --list |grep alias alias.br=branch alias.ci=commit alias.co=checkout alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
取消別名 git config --global --unset alias.br
[root@ying01 feng]# git config --global --unset alias.br [root@ying01 feng]# git br git:'br' 不是一個 git 命令。參見 'git --help'。 您指的是這其中的某一個麼? branch var [root@ying01 feng]# ^C [root@ying01 feng]# git br git:'br' 不是一個 git 命令。參見 'git --help'。 您指的是這其中的某一個麼? branch var
搭建git服務器的應用場景:
github畢竟是公開的,而私有倉庫又得花錢買。因此咱們能夠想辦法搭建一個私有的,只本身公司使用的,開發人員不多。此時可使用命令行的git服務器
機器分配
ying02上安裝git工具
[root@ying02 ~]# yum -y install git
在ying02上,添加git用戶,而且設置shell爲/usr/bin/git-shell,目的是爲了避免讓git用戶遠程登錄
[root@ying02 ~]# useradd -s /usr/bin/git-shell git [root@ying02 ~]# tail -1 /etc/passwd git:x:1001:1001::/home/git:/usr/bin/git-shell [root@ying02 ~]# cd /home/git/
建立 .ssh/authorized_keys 文件,用來放客戶機端的公鑰
[root@ying02 git]# mkdir .ssh [root@ying02 git]# touch .ssh/authorized_keys [root@ying02 git]# ls -l .ssh/authorized_keys -rw-r--r-- 1 root root 0 9月 5 02:59 .ssh/authorized_keys [root@ying02 git]# chmod 600 .ssh/authorized_keys [root@ying02 git]# ls -l .ssh/authorized_keys -rw------- 1 root root 0 9月 5 02:59 .ssh/authorized_keysh [root@ying02 git]# chown -R git:git .ssh [root@ying02 git]# ls -ld .ssh drwxr-xr-x 2 git git 29 9月 5 02:59 .ssh
在客戶機ying01上,複製器ssh的公鑰
[root@ying01 ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+X/2tfggcYIe/3qp98blHj4ySD0GbVmP5QfU7tXXMbE/+FnktHOA3l9+noxXxNXFpaDjc4k+R9TV1R4yG8U0+bx8zucaVTSQWgFCiwDlKhY4pUgfQePtNX+AVUo7yf0+ysz7P3cyUTIInHORB2R/DoKzcxMEM9AHkb//G/UtLaRBhLLWhNGz/R8S5ZhdsC3+X+yKKDVffua8RWkAqevntf4lWz6KEYbuTjxzM7cOXOrHx0/w3/qtvD/Vee+I7vZHkCdqwMfQxn9pTh6c3RwBwcx9jzbJJ7YLV5KmOx0QqSK8qHylgjuO2ZS1wF1+eTdO1D2zP2aEykF6dDNhzQRb5 root@ying01
把客戶機上的公鑰粘貼到ying02的authorized_keys文件裏
[root@ying02 git]# vim .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+X/2tfggcYIe/3qp98blHj4ySD0GbVmP5QfU7tXXMbE/+FnktHOA3l9+noxXxNXFpaDjc4k+R9TV1R4yG8U0+bx8zucaVTSQWgFCiwDlKhY4pUgfQePtNX+AVUo7yf0+ysz7P3cyUTIInHORB2R/DoKzcxMEM9AHkb//G/UtLaRBhLLWhNGz/R8S5ZhdsC3+X+yKKDVffua8RWkAqevntf4lWz6KEYbuTjxzM7cOXOrHx0/w3/qtvD/Vee+I7vZHkCdqwMfQxn9pTh6c3RwBwcx9jzbJJ7YLV5KmOx0QqSK8qHylgjuO2ZS1wF1+eTdO1D2zP2aEykF6dDNhzQRb5 root@ying01
在客戶機ying01上,鏈接ying02服務器
[root@ying01 ~]# ssh git@192.168.112.138 fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access. Connection to 192.168.112.138 closed.
在服務器ying02上,建立gitroot目錄做爲git倉庫
[root@ying02 git]# mkdir /data/gitroot [root@ying02 git]# cd /data/gitroot [root@ying02 gitroot]# git init --bare sample.git //初始化裸倉庫 初始化空的 Git 版本庫於 /data/gitroot/sample.git/ [root@ying02 gitroot]# ls sample.git [root@ying02 gitroot]# ls -l 總用量 0 drwxr-xr-x 7 root root 119 9月 5 03:09 sample.git [root@ying02 gitroot]# chown -R git:git sample.git
git init --bare sample.git
會建立一個裸倉庫,裸倉庫沒有工做區,由於服務器上的Git倉庫純粹是爲了共享,因此不讓用戶直接登陸到服務器上去改工做區,而且服務器上的Git倉庫一般都以.git結尾
服務器已經搭建好,如今客戶端ying01上測試
在客戶端ying01下,克隆服務器端(ying)的倉庫,即遠程倉庫
[root@ying01 ~]# git clone git@192.168.112.138:/data/gitroot/sample.git 正克隆到 'sample'... warning: 您彷佛克隆了一個空版本庫。 [root@ying01 ~]# ls sample/ [root@ying01 ~]# cd sample/
在root/sample本地工做區進行測試,
[root@ying01 sample]# pwd /root/sample root@ying01 sample]# ls -la 總用量 4 drwxr-xr-x 3 root root 18 9月 5 03:12 . dr-xr-x---. 42 root root 4096 9月 5 03:12 .. drwxr-xr-x 7 root root 119 9月 5 03:12 .git [root@ying01 sample]# cp /etc/init.d/mysqld . //複製一個文件到根下,好比mysqld [root@ying01 sample]# ls mysqld [root@ying01 sample]# git add . //加載根目錄 [root@ying01 sample]# git commit -m "add new file" //根目錄提交 [master(根提交) ff7de76] add new file 1 file changed, 378 insertions(+) create mode 100755 mysqld [root@ying01 sample]# git push origin master //推送到遠程倉庫master分支 Counting objects: 3, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 3.85 KiB | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.168.112.138:/data/gitroot/sample.git * [new branch] master -> master
在此工做區root/sample下,新建文件,並推送到遠程倉庫(服務器ying02)
[root@ying01 sample]# echo "wwwwwwww" > 111.txt [root@ying01 sample]# git add 111.txt [root@ying01 sample]# git commit -m "add 111.txt" [master 5b72359] add 111.txt 1 file changed, 1 insertion(+) create mode 100644 111.txt [root@ying01 sample]# git push Counting objects: 4, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 268 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@192.168.112.138:/data/gitroot/sample.git ff7de76..5b72359 master -> master
在/tmp/目錄建立一個工做區(與/root/sample區分),模擬其餘用戶
[root@ying01 ~]# cd /tmp/ //cd到tmp下,克隆遠程倉庫 [root@ying01 tmp]# git clone git@192.168.112.138:/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.08 KiB | 0 bytes/s, done. [root@ying01 tmp]# ls sample/ 111.txt mysqld
在此工做區/tmp/sample下,更改111.txt內容,到另外一工做區去查看
[root@ying01 tmp]# cd sample/ [root@ying01 sample]# pwd /tmp/sample [root@ying01 sample]# echo "12345678" >> 111.txt //更改111.txt內容 [root@ying01 sample]# git add 111.txt [root@ying01 sample]# git commit -m "ch 111.txt" [master 2585a5e] ch 111.txt 1 file changed, 1 insertion(+) [root@ying01 sample]# git push //推送到遠程倉庫 Counting objects: 5, done. Delta compression using up to 2 threads. 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 git@192.168.112.138:/data/gitroot/sample.git 5b72359..2585a5e master -> master
到/root/sample工做區下,查看111.txt未變化
[root@ying01 sample]# cd /root/sample/ [root@ying01 sample]# ls 111.txt mysqld [root@ying01 sample]# cat 111.txt wwwwwwww
把遠程倉庫拉下來(更新),此時111.txt內容和/tmp/sample工做區下111.txt內容一致
[root@ying01 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.112.138:/data/gitroot/sample 5b72359..2585a5e master -> origin/master 更新 5b72359..2585a5e Fast-forward 111.txt | 1 + 1 file changed, 1 insertion(+) [root@ying01 sample]# cat 111.txt //此時111.txt內容有變化 wwwwwwww 12345678
gitlab分社區版(ce)和企業版(ee),官方推薦安裝gitlab至少4G內存。
安裝方法:https://about.gitlab.com/installation/#centos-7?version=ce
gitlab清華大學鏡像站:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
因爲gitlab軟件比較大,因此選擇國內源,下載比較快。
先建立gitlab.repo,寫入如下語句
[root@ying01 ~]# 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
此時yum安裝gitlab-ce
[root@ying01 ~]# yum -y install gitlab-ce
gitlab從新配置
[root@ying01 ~]# gitlab-ctl reconfigure ....... Running handlers: Running handlers complete Chef Client finished, 429/614 resources updated in 04 minutes 23 seconds gitlab Reconfigured! //配置成功
常看有關gitlab進程,竟然如此之多,可見其複雜程度
[root@ying01 ~]# ps aux |grep gitlab root 2624 0.0 0.0 4380 296 ? Ss 11:11 0:00 runsvdir -P /opt/gitlab/service log: ........................................................................................................................................................................................................................................................................................................................................................................................................... gitlab-+ 2646 0.4 0.3 39480 8816 ? Ssl 11:11 0:06 /opt/gitlab/embedded/bin/redis-server 127.0.0.1:0 gitlab-+ 2707 0.0 1.4 809888 42752 ? Ss 11:12 0:00 /opt/gitlab/embedded/bin/postgres -D /var/opt/gitlab/postgresql/data gitlab-+ 2709 0.0 0.5 810088 15544 ? Ss 11:12 0:00 postgres: checkpointer process gitlab-+ 2710 0.0 0.2 809888 7604 ? Ss 11:12 0:00 postgres: writer process gitlab-+ 2711 0.0 0.6 809888 18396 ? Ss 11:12 0:00 postgres: wal writer process gitlab-+ 2712 0.0 0.1 810456 2996 ? Ss 11:12 0:00 postgres: autovacuum launcher process gitlab-+ 2713 0.0 0.0 32932 2444 ? Ss 11:12 0:00 postgres: stats collector process git 2911 0.0 0.0 11688 1024 ? Ss 11:13 0:00 /bin/bash /opt/gitlab/embedded/bin/gitlab-unicorn-wrapper git 2929 3.0 14.2 706252 414480 ? Sl 11:13 0:41 unicorn master -D -E production -c /var/opt/gitlab/gitlab-rails/etc/unicorn.rb /opt/gitlab/embedded/service/gitlab-rails/config.ru git 2948 4.1 16.2 923028 471368 ? Ssl 11:13 0:56 sidekiq 5.1.3 gitlab-rails [0 of 25 busy] root 2964 0.0 0.0 4228 152 ? Ss 11:13 0:00 runsv gitlab-workhorse root 2999 0.0 0.0 11680 940 ? Ss 11:13 0:00 /bin/sh /opt/gitlab/embedded/bin/gitlab-logrotate-wrapper gitlab-+ 3083 0.1 0.3 19588 10568 ? Ssl 11:13 0:01 /opt/gitlab/embedded/bin/node_exporter --web.listen-address=localhost:9100 --collector.textfile.directory=/var/opt/gitlab/node-exporter/textfile_collector root 3173 0.0 0.0 4228 156 ? Ss 11:13 0:00 runsv gitlab-monitor gitlab-+ 3194 0.0 0.3 227944 9632 ? Ssl 11:13 0:00 /opt/gitlab/embedded/bin/redis_exporter -web.listen-address=localhost:9121 -redis.addr=unix:///var/opt/gitlab/redis/redis.socket git 3271 0.0 14.1 704208 411220 ? Sl 11:13 0:00 unicorn worker[0] -D -E production -c /var/opt/gitlab/gitlab-rails/etc/unicorn.rb /opt/gitlab/embedded/service/gitlab-rails/config.ru git 3274 0.0 14.1 704208 410564 ? Sl 11:13 0:01 unicorn worker[1] -D -E production -c /var/opt/gitlab/gitlab-rails/etc/unicorn.rb /opt/gitlab/embedded/service/gitlab-rails/config.ru git 3277 0.0 14.1 704208 412300 ? Sl 11:13 0:01 unicorn worker[2] -D -E production -c /var/opt/gitlab/gitlab-rails/etc/unicorn.rb /opt/gitlab/embedded/service/gitlab-rails/config.ru gitlab-+ 3382 0.0 0.3 915316 9104 ? Ss 11:14 0:00 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3521 0.0 0.3 915440 9628 ? Ss 11:15 0:00 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3553 0.0 0.3 915316 9500 ? Ss 11:15 0:00 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3579 0.0 0.3 915436 11000 ? Ss 11:15 0:00 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3584 0.0 0.3 915328 10244 ? Ss 11:15 0:00 postgres: gitlab gitlabhq_production [local] idle root 3652 0.0 0.0 4372 348 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/unicorn root 3653 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/sidekiq root 3657 0.0 0.0 4372 344 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/postgresql git 3663 0.0 0.5 224080 14828 ? Ssl 11:15 0:00 /opt/gitlab/embedded/bin/gitla-workhorse -listenNetwork unix -listenUmask 0 -listenAddr /var/opt/gitlab/gitlab-workhorse/socket -authBackend http://localhost:8080 -authSocket /var/opt/gitlab/gitlab-rails/sockets/gitlab.socket -documentRoot /opt/gitlab/embedded/service/gitlab-rails/public -pprofListenAddr -prometheusListenAddr localhost:9229 -secretPath /opt/gitlab/embedded/service/gitlab-rails/.gitlab_workhorse_secret -config config.toml root 3669 0.0 0.0 4372 348 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/gitlab-workhorse git 3678 0.1 0.8 433212 23644 ? Ssl 11:15 0:01 /opt/gitlab/embedded/bin/gitaly /var/opt/gitlab/gitaly/config.toml root 3688 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/redis root 3690 0.0 0.0 4372 344 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/node-exporter git 3697 0.5 0.9 325992 27724 ? Ssl 11:15 0:07 /opt/gitlab/embedded/bin/ruby /opt/gitlab/embedded/bin/gitlab-mon web -c /var/opt/gitlab/gitlab-monitor/gitlab-monitor.yml root 3700 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/gitlab-monitor root 3705 0.0 0.0 4372 564 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/nginx root 3706 0.0 0.0 4372 348 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/logrotate gitlab-+ 3710 0.6 2.1 603440 61856 ? Ssl 11:15 0:07 /opt/gitlab/embedded/bin/prometheus -web.listen-address=localhost:9090 -storage.local.path=/var/opt/gitlab/prometheus/data -storage.local.chunk-encoding-version=2 -storage.local.target-heap-size=85764014 -config.file=/var/opt/gitlab/prometheus/prometheus.yml root 3719 0.0 0.0 4372 564 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/gitaly gitlab-+ 3726 0.0 0.3 321140 11148 ? Ssl 11:15 0:00 /opt/gitlab/embedded/bin/alertmanager --web.listen-address=localhost:9093 --storage.path=/var/opt/gitlab/alertmanager/data --config.file=/var/opt/gitlab/alertmanager/alertmanager.yml root 3734 0.0 0.0 4372 344 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/redis-exporter gitlab-+ 3749 0.0 0.3 142072 11224 ? Ssl 11:15 0:00 /opt/gitlab/embedded/bin/postgres_exporter --web.listen-address=localhost:9187 --extend.query-path=/var/opt/gitlab/postgres-exporter/queries.yaml gitlab-+ 3754 0.0 0.3 915740 10540 ? Ss 11:15 0:01 postgres: gitlab-psql postgres [local] idle root 3755 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/prometheus root 3756 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/postgres-exporter root 3759 0.0 0.0 4372 568 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/alertmanager git 3764 0.4 2.0 1316560 60876 ? Sl 11:15 0:04 ruby /opt/gitlab/embedded/service/gitaly-ruby/bin/gitaly-ruby 3678 /tmp/gitaly-ruby645930785/socket.0 git 3766 0.3 2.0 1316456 60848 ? Sl 11:15 0:04 ruby /opt/gitlab/embedded/service/gitaly-ruby/bin/gitaly-ruby 3678 /tmp/gitaly-ruby645930785/socket.1 gitlab-+ 3786 0.0 0.3 916044 11240 ? Ss 11:16 0:01 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3787 0.0 0.3 915036 8836 ? Ss 11:16 0:00 postgres: gitlab gitlabhq_production [local] idle gitlab-+ 3788 0.0 0.3 915324 9760 ? Ss 11:16 0:00 postgres: gitlab gitlabhq_production [local] idle root 6717 0.0 0.1 38452 3112 ? Ss 11:35 0:00 /opt/gitlab/embedded/sbin/nginx -p /var/opt/gitlab/nginx root 6729 0.0 0.0 112724 984 pts/0 S+ 11:35 0:00 grep --color=auto gitlab
gitlab也自帶nginx服務,本機以前安裝了nginx,須要把以前的nginx讀物停掉;
[root@ying01 ~]# chkconfig nginx off [root@ying01 ~]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9100 0.0.0.0:* LISTEN 3083/node_exporter tcp 0 0 127.0.0.1:9229 0.0.0.0:* LISTEN 3663/gitlab-workhor tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 587/rpcbind tcp 0 0 127.0.0.1:9168 0.0.0.0:* LISTEN 3697/ruby tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 2929/unicorn master tcp 0 0 0.0.0.0:20048 0.0.0.0:* LISTEN 880/rpc.mountd tcp 0 0 127.0.0.1:8082 0.0.0.0:* LISTEN 2948/sidekiq 5.1.3 tcp 0 0 127.0.0.1:9236 0.0.0.0:* LISTEN 3678/gitaly tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 656/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 885/master tcp 0 0 0.0.0.0:8060 0.0.0.0:* LISTEN 7312/nginx tcp 0 0 127.0.0.1:9121 0.0.0.0:* LISTEN 3194/redis_exporter 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 3710/prometheus tcp 0 0 0.0.0.0:33058 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:9187 0.0.0.0:* LISTEN 3749/postgres_expor tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 761/zabbix_server tcp 0 0 0.0.0.0:43716 0.0.0.0:* LISTEN 824/rpc.statd tcp 0 0 127.0.0.1:9093 0.0.0.0:* LISTEN 3726/alertmanager tcp 0 0 192.168.112.136:27017 0.0.0.0:* LISTEN 1281/mongod tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1281/mongod tcp6 0 0 :::111 :::* LISTEN 587/rpcbind tcp6 0 0 ::1:9168 :::* LISTEN 3697/ruby tcp6 0 0 :::80 :::* LISTEN 657/httpd tcp6 0 0 :::20048 :::* LISTEN 880/rpc.mountd tcp6 0 0 :::43955 :::* LISTEN - tcp6 0 0 :::22 :::* LISTEN 656/sshd tcp6 0 0 ::1:25 :::* LISTEN 885/master tcp6 0 0 :::2049 :::* LISTEN - tcp6 0 0 :::10051 :::* LISTEN 761/zabbix_server tcp6 0 0 :::9094 :::* LISTEN 3726/alertmanager tcp6 0 0 :::40073 :::* LISTEN 824/rpc.statd tcp6 0 0 :::3306 :::* LISTEN 1170/mysqld
中止gitlab服務:gitlab-ctl stop 涉及其餘服務比較多,涉及如下幾項
[root@ying01 ~]# gitlab-ctl stop /opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/omnibus-ctl-0.5.0/lib/omnibus-ctl.rb:684: warning: Insecure world writable dir /tmp/ in PATH, mode 041777 ok: down: alertmanager: 0s, normally up ok: down: gitaly: 1s, normally up ok: down: gitlab-monitor: 0s, normally up ok: down: gitlab-workhorse: 1s, normally up ok: down: logrotate: 0s, normally up ok: down: nginx: 0s, normally up ok: down: node-exporter: 0s, normally up ok: down: postgres-exporter: 0s, normally up ok: down: postgresql: 1s, normally up ok: down: prometheus: 0s, normally up ok: down: redis: 1s, normally up ok: down: redis-exporter: 0s, normally up ok: down: sidekiq: 0s, normally up ok: down: unicorn: 0s, normally up
同時redis-server不能開啓,否則會干擾
[root@ying01 ~]# killall redis-server redis-server: no process found
開啓gitlab服務:gitlab-ctl stop
[root@ying01 ~]# gitlab-ctl start /opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/omnibus-ctl-0.5.0/lib/omnibus-ctl.rb:684: warning: Insecure world writable dir /tmp/ in PATH, mode 041777 ok: run: alertmanager: (pid 7775) 0s ok: run: gitaly: (pid 7787) 1s ok: run: gitlab-monitor: (pid 7799) 0s ok: run: gitlab-workhorse: (pid 7813) 1s ok: run: logrotate: (pid 7821) 0s ok: run: nginx: (pid 7829) 1s ok: run: node-exporter: (pid 7831) 0s ok: run: postgres-exporter: (pid 7836) 0s ok: run: postgresql: (pid 7842) 1s ok: run: prometheus: (pid 7924) 0s ok: run: redis: (pid 7936) 1s ok: run: redis-exporter: (pid 7944) 0s ok: run: sidekiq: (pid 7950) 1s ok: run: unicorn: (pid 7957) 0s
查看相關nginx進程
[root@ying01 ~]# ps aux |grep nginx root 2979 0.0 0.0 4228 164 ? Ss 11:13 0:00 runsv nginx root 3705 0.0 0.0 4372 488 ? S 11:15 0:00 svlogd -tt /var/log/gitlab/nginx root 8314 0.0 0.0 38456 2064 ? Ss 11:46 0:00 nginx: master process /opt/gitlab/embedded/sbin/nginx -p /var/opt/gitlab/nginx gitlab-+ 8326 0.0 0.2 42708 6348 ? S 11:46 0:00 nginx: worker process gitlab-+ 8327 0.0 0.2 42708 6336 ? S 11:46 0:00 nginx: worker process gitlab-+ 8328 0.0 0.0 38456 1456 ? S 11:46 0:00 nginx: cache manager process root 20040 0.0 0.0 112724 984 pts/0 S+ 13:22 0:00 grep --color=auto nginx
此時本地gitlab完成。
設置域名訪問
[root@ying01 ~]# ls /var/opt/gitlab/nginx/conf/ gitlab-http.conf nginx.conf nginx-status.conf [root@ying01 ~]# vim /var/opt/gitlab/nginx/conf/gitlab-http.conf listen *:80; //設置端口 server_name gitlab.example.com; //設置域名
這裏直接使用ip 192.168.112.136訪問,自動加載到更改密碼的頁面
登陸後,在右上角,選擇設置,把語言選爲簡體中文
按圖示,先創建一個羣組yinglinux
在新建一個項目,羣組選擇yinglinux
此時會彈出黃色的信息,須要設置SSH祕鑰
把本地的機器的公鑰,複製到方框內,就能夠二者兩聯;
新建用戶名
注意:由於360瀏覽器顯示不出來,扳手符號,因此改用 Microsoft Edge
點擊菜單欄中 小扳手,點擊 new user
定義你所但願的用戶名;此時密碼不會讓你設置
生成用戶後,頁面頂上會顯示 user was successtully created ;點擊右上角edit,進行編輯
此時密碼選項出現空白,隨便填寫一個密碼。
退出當前的用戶,登陸新建立的用戶:feng-01
此時會加載到,更換密碼的頁面。
登陸後,在feng-01用戶下,能夠建立新項目,新羣組 不用建立了,由於root用戶已經建立了。
建立的新項目,會加載feng-01用戶目錄下
新項目study創建後,後出現SSH 和 http 兩種方式 與本地相連
下面就是官方提供的在本地使用命令行的解決方案
備份
[root@ying01 ~]# gitlab-rake gitlab:backup:create Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] done Dumping repositories ... * yinglinux/feng-project ... [SKIPPED] [SKIPPED] Wiki * feng-01/study ... [SKIPPED] [SKIPPED] Wiki done Dumping uploads ... done Dumping builds ... done Dumping artifacts ... done Dumping pages ... done Dumping lfs objects ... done Dumping container registry images ... [DISABLED] Creating backup archive: 1536133882_2018_09_05_11.2.3_gitlab_backup.tar ... done Uploading backup archive to remote storage ... skipped Deleting tmp directories ... done done done done done done done done Deleting old backups ... skipping [root@ying01 ~]# ls /var/opt/gitlab/backups/ //備份的目錄 1536133882_2018_09_05_11.2.3_gitlab_backup.tar [root@ying01 ~]# du -sh /var/opt/gitlab/backups/ //文件很小,只有80K 80K /var/opt/gitlab/backups/
恢復
在恢復以前,須要中止兩個服務,目的防止繼續寫入。
[root@ying01 ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq /opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/omnibus-ctl-0.5.0/lib/omnibus-ctl.rb:684: warning: Insecure world writable dir /tmp/ in PATH, mode 041777 ok: down: unicorn: 0s, normally up /opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/omnibus-ctl-0.5.0/lib/omnibus-ctl.rb:684: warning: Insecure world writable dir /tmp/ in PATH, mode 041777 ok: down: sidekiq: 0s, normally up [root@ying01 ~]# gitlab-rake gitlab:backup:restore BACKUP=1536133882_2018_09_05_11.2.3 //恢復備份的文件 [root@ying01 ~]# gitlab-ctl start //開啓服務