CentOS 7 學習(四)Git配置(一)


CentOS 7 學習(四)Git配置(一) 
1、對於版本管理系統,目前常用的是Subverion和Git,Subversion是集中式版本管理系統中最好的,所有人的代碼都要提交到服務器上,如果要知道修改歷史,就需要訪問服務器;Git的哲學不同,是分布式管理版本,即本地也維護一個或者多個版本或分支,需要的時候才會提交到主服務器上,提供了非常優秀的分支合並功能,這種方式非常適合於分布式開發,即可以在本機開發完成,再同步到主干上,同時本機也可以擁有所有的歷史修改信息。 
2、環境: 
主服務器:CentOS 7, 192.168.1.14 
從服務器:CentOS 7, 192.168.1.12 
3、CentOS 7帶的git版本太低了,是1.8.3,為方便起見,從主站上下載了git-2.0.4.tar.gz,運行命令 
tar xvf git-2.0.4.tar.gz 
cd git-2.0.4 
./configure 
make 
make install 
在make命令,可能會出現錯誤,我遇上的如下: 
/usr/bin/perl Makefile.PL PREFIX='/usr/local' INSTALL_BASE='' --localedir='/usr/local/share/locale' 
Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at Makefile.PL line 3. 
BEGIN failed--compilation aborted at Makefile.PL line 3. 
make[1]: *** [perl.mak] 錯誤 2 
make: *** [perl/perl.mak] 錯誤 2 
這是缺少一些Perl的擴展包,運行命令yum install perl-ExtUtils-* 
再執行make指令就可以了 
[root@centos1 git-2.0.4]# git --version 
git version 2.0.4 
這說明安裝成功了 
按照相似的步驟,在另外一台機器上創建同樣的git 
4、在從服務器上做些測試 
1)創建一個用戶git,主目錄為/opt/git,這個用戶是將來連接git服務器的用戶名,/opt/git是git倉庫的起始目錄 
useradd git -d /opt/git -U -m -s /bin/bash 
2)創建版本庫 
[git@centos-s1 ~]$ su - git 
[git@centos-s1 ~]$ mkdir mysite 
[git@centos-s1 ~]$ cd mysite 
[git@centos-s1 mysite]$ ls 
[git@centos-s1 mysite]$ git init 
初始化空的 Git 版本庫於 /opt/git/mysite/.git/ 
3)配置 
git的配置通常用git config命令完成,這個命令有很多參數,大致如下 
[git@centos-s1 mysite]$ git config 
用法:git config [選項] 

配置文件位置 
--global 使用全局配置文件 
--system 使用系統級配置文件 
--local 使用版本庫級配置文件 
-f, --file <文件> 使用指定的配置文件 
--blob <數據對象ID> 從給定的數據對象讀取配置 

操作 
--get 獲取值:name [value-regex] 
--get-all 獲得所有的值:key [value-regex] 
--get-regexp 根據正則表達式獲得值:name-regex [value-regex] 
--get-urlmatch 獲得 URL 取值:section[.var] URL 
--replace-all 替換所有匹配的變量:name value [value_regex] 
--add 添加一個新的變量:name value 
--unset 刪除一個變量:name [value-regex] 
--unset-all 刪除所有匹配項:name [value-regex] 
--rename-section 重命名小節:old-name new-name 
--remove-section 刪除一個小節:name 
-l, --list 列出所有 
-e, --edit 打開一個編輯器 
--get-color <slot> 找到配置的顏色:[默認] 
--get-colorbool <slot> 
找到顏色設置:[stdout-is-tty] 

類型 
--bool 值是 "true" 或 "false" 
--int 值是十進制數 
--bool-or-int 值是 --bool or --int 
--path 值是一個路徑(文件或目錄名) 

其它 
-z, --null 終止值是NUL字節 
--includes 查詢時參照 include 指令遞歸查找 

可以看到配置的等級有3層,全局、系統、本地版本庫,使用-l或--list命令可以看到已有的配置,目前還什么都沒有。 
[git@centos-s1 mysite]$ git config --global user.name "Shi Yongqiang" 
[git@centos-s1 mysite]$ git config --global user.email "flysyq@163.com
[git@centos-s1 mysite]$ git config --global --list 
user.name=Shi Yongqiang 
user.email=flysyq@163.com 
用戶名和用戶郵件是必須的,這是用來標識你的個人身份,會體現在你的提交歷史中。 
4)常用命令 
git add file_name #增加文件到暫存區 
git commit file_name -m "file comment" 提交到本地 
git log #查看log 
git status #顯示狀態,即是否有文件修改了沒有提交 
git branch RB1.0 master #創建分支(必須主分支有了內容這個命令才生效),前一個參數為分支名,后一個為源分支名,系統默認創建的分支,即根分支,叫master 
git checkout RB1.0 #切換到RB1.0分支,這時候查看當前目錄,會發現已經變成RB1.0的內容了,如果創建了分支RB1.0之后修改了master分支的文件,會看的更清楚 
git checkout -b RB2.0 RB1.0 #創建一個新分支,並切換到新分支 
git merge RB1.0 #當前分支為master,這個命令將RB1.0的內容合並到master分支上,這時候有可能會出現沖突,需要手工解決之后才能提交,這時候其內容已經提交到master的當前目錄了,但是還沒有最后提交,你可以修改文件內容,提交,合並完成了,如果沒有沖突,這個命令很容易就完成了。 
日常來說,這些命令就夠了,不過很少有人會這么用,一般都會使用開發工具如Eclipse帶的git工具來實現。 
5)協同工作 
本機工作的git沒什么特別之處,其實和本機安裝一個SubVersion沒太多區別,其優勢在於其分布式開發。 
a)連接遠程服務器 
git clone https://github.com/alibaba/otter.git #從遠程服務器下載git項目到本地,這個地址指向的阿里巴巴的mysql同步項目-otter 
b)自己架設服務器 
在從服務器上按照上述辦法架設一個git服務器,這樣主從服務器就都有了git環境,理想環境是在從服務器上開發,主服務器上發布。 
對於分布式管理git項目,對於分支的選擇會很麻煩,可以參考這個博客http://www.ruanyifeng.com/blog/2012/07/git.html 
一般來說,都會有至少兩個分支,一個主分支master,一個開發分支developer,開發結束之后,將開發分支合並到主分支上發布,如果出現同時開發的現象,可以將developer分支再創建一個分支,修改完畢之后,合並到開發分支和主分支上,這個分支在主分支發布之后要立刻刪除,否則會不好管理。需要注意的是,這種情況下開發分支和主分支再次合並很可能會出現沖突,需要耐心解決。 
訪問git服務器常用的協議有ssh、git、http,通常來說ssh適合小團隊讀寫,http適合大范圍讀,git相對來較難架設,且使用非標准端口,只能在內部系統使用。更詳細的說明,可以看git官方文檔。 
使用ssh協議連接服務器有兩種辦法,一種是通過用戶名密碼連接,一種是通過公鑰私鑰連接,前者容易配置,但是沒法區分人群,除非不停的增加系統用戶,為簡單起見,都試驗一下: 
i)使用用戶名密碼 
在主服務器(192.168.1.14)上創建一個git倉庫,如下命令 
[git@centos1 ~]$ mkdir gitsite 
[git@centos1 ~]$ cd gitsite/ 
[git@centos1 gitsite]$ git init 
初始化空的 Git 版本庫於 /opt/git/gitsite/.git/ 
[git@centos1 gitsite]$ vim test.txt 
[git@centos1 gitsite]$ git add test.txt 
[git@centos1 gitsite]$ git commit -m "git 測試" 
[master(根提交) 82e77f9] git 測試 
1 file changed, 1 insertion(+) 
create mode 100644 test.txt 
記得要設置用戶git的密碼
在從服務器上(192.168.1.12)運行命令 
[git@centos-s1 ~]$ git clone git@192.168.1.14:/opt/git/gitsite 
正克隆到 'gitsite'... 
git@192.168.1.14's password: 
remote: 對象計數中: 3, 完成. 
remote: Total 3 (delta 0), reused 0 (delta 0) 
接收對象中: 100% (3/3), 完成. 
檢查連接... 完成。 
本地出現一個目錄gitsite,進入目錄,可以看到text.txt文件,運行git log可以看到提交歷史。 
這樣就創建了一個本地版本, 
[git@centos-s1 gitsite]$ git status 
位於分支 master 
您的分支與上游分支 'origin/master' 一致。 

無文件要提交,干凈的工作區 

本地編輯一個文件,提交到14服務器上,如下 
[git@centos-s1 gitsite]$ vim t.txt 
[git@centos-s1 gitsite]$ git add . 
[git@centos-s1 gitsite]$ git commit -m "add by 12" 
[master d1cd89a] add by 12 
1 file changed, 1 insertion(+), 1 deletion(-) 
[git@centos-s1 gitsite]$ git commit -m "add by 12" 
位於分支 master 
您的分支領先 'origin/master' 共 1 個提交。 
(使用 "git push" 來發布您的本地提交) 

無文件要提交,干凈的工作區 

可以看出本地創建了t.txt,提交到本地,在第二次提交的時候,系統給了提示,可以用git push來更新到遠程目錄 
運行命令如下 

[git@centos-s1 gitsite]$ git push 
warning: push.default 尚未設置,它的默認值在 Git 2.0 已從 'matching' 
變更為 'simple'。若要不再顯示本信息並保持傳統習慣,進行如下設置: 

git config --global push.default matching 

若要不再顯示本信息並從現在開始采用新的使用習慣,設置: 

git config --global push.default simple 

當 push.default 設置為 'matching' 后,git 將推送和遠程同名的所有 
本地分支。 

從 Git 2.0 開始,Git 缺省采用更為保守的 'simple' 模式,只推送當前 
分支到遠程關聯的同名分支,即 'git push' 推送當前分支。 

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

git@192.168.1.14's password: 
對象計數中: 6, 完成. 
Delta compression using up to 2 threads. 
壓縮對象中: 100% (4/4), 完成. 
寫入對象中: 100% (6/6), 591 bytes | 0 bytes/s, 完成. 
Total 6 (delta 0), reused 0 (delta 0) 
remote: error: refusing to update checked out branch: refs/heads/master 
remote: error: By default, updating the current branch in a non-bare repository 
remote: error: is denied, because it will make the index and work tree inconsistent 
remote: error: with what you pushed, and will require 'git reset --hard' to match 
remote: error: the work tree to HEAD. 
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to 
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into 
remote: error: its current branch; however, this is not recommended unless you 
remote: error: arranged to update its work tree to match what you pushed in some 
remote: error: other way. 
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set 
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. 
To git@192.168.1.14:/opt/git/gitsite 
! [remote rejected] master -> master (branch is currently checked out) 
error: 無法推送一些引用到 'git@192.168.1.14:/opt/git/gitsite' 

這是個常見錯誤,看提示信息可以看出很多東西,首先來說,提示設置push.default變量,2.0之前默認是matching,提交所有和遠程同名的分支,2.0之后默認是simple,只提交當前分支。后面的提示是遠程的分支正處於checkout狀態,所以無法推送。 
可以在服務器上配置,可以提交當前分支,如下 
[git@centos1 gitsite]$ git config --global receive.denyCurrentBranch "ignore" 
這樣12服務器就可以提交了。 
所以如果是大型的項目,多個分支、多個版本,一般來說,主服務器上的版本庫都會建立裸庫,即沒法進行checkout,add,commit的庫,不過對於小型項目,主版本庫還是能操作比較好,如果已經比較成熟了,就可以采用裸庫。 

有些情況下,是先有本地的版本庫,然后采用主版本庫,需要將本地的版本庫復制到主本版本庫,然后可以同步 
在12機器創建版本庫gitsite1,然后 
[git@centos-s1 ~]$ git init --template=/opt/git/gitsite1 gitsite1.git 
初始化空的 Git 版本庫於 /opt/git/gitsite1.git/.git/ 
[git@centos-s1 ~]$ cd gitsite1.git/ 
[git@centos-s1 gitsite1.git]$ ls 
[git@centos-s1 gitsite1.git]$ pwd 
/opt/git/gitsite1.git 
[git@centos-s1 gitsite1.git]$ cd .. 
[git@centos-s1 ~]$ scp -r gitsite1.git/ ssh:git@192.168.1.14:/opt/git 
git@192.168.1.14's password: 
test.txt 100% 28 0.0KB/s 00:00 
HEAD 100% 23 0.0KB/s 00:00 
config 
100% 92 0.1KB/s 00:00 
這樣就傳輸到了主目錄 
在本地和遠程建立連接 
[git@centos-s1 gitsite1]$ git push git@192.168.1.14:/opt/git/gitsite1.git master 
git@192.168.1.14's password: 
對象計數中: 3, 完成. 
寫入對象中: 100% (3/3), 234 bytes | 0 bytes/s, 完成. 
Total 3 (delta 0), reused 0 (delta 0) 
To git@192.168.1.14:/opt/git/gitsite1.git 
* [new branch] master -> master 

master是分支名稱 
也可以為遠程服務器建立名稱,這樣用起來會舒服一些 
[git@centos-s1 gitsite1]$ git remote add orign git@192.168.1.14:/opt/git/gitsite1.git 
[git@centos-s1 gitsite1]$ git push orign master 
git@192.168.1.14's password: 
Everything up-to-date 

遠程的常用命令 
git push #推送 
git pull #下載遠程代碼 

ii)使用密鑰
密鑰的使用非常簡單,客戶端創建公鑰和密鑰,設置密鑰密碼,將公鑰上傳到服務器,服務器將公鑰加入信任文件中,然后客戶端操作會提示輸入密碼,就是密碼密碼,具體如下 
[git@centos-s1 gitsite1]$ ssh-keygen -t rsa 
Generating public/private rsa key pair. 
Enter file in which to save the key (/opt/git/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /opt/git/.ssh/id_rsa. 
Your public key has been saved in /opt/git/.ssh/id_rsa.pub. 
The key fingerprint is: 
20:70:11:47:f8:ca:91:37:6d:5d:43:cd:13:1c:40:61 git@centos-s1 
The key's randomart image is: 
+--[ RSA 2048]----+ 
| . +=o oE*oo | 
| o.. .o = | 
| .o.. . . . . | 
| o.+.o . | 
| . + oS | 
| o | 
| | 
| | 
| | 
+-----------------+ 
[git@centos-s1 gitsite1]$ cd 
[git@centos-s1 ~]$ cd .ssh 
[git@centos-s1 .ssh]$ ls 
id_rsa id_rsa.pub known_hosts 
[git@centos-s1 .ssh]$ mv id_rsa.pub id_rsa_shiyq.pub 
[git@centos-s1 .ssh]$ scp id_rsa_shiyq.pub git@192.168.1.14:/opt/git 
Enter passphrase for key '/opt/git/.ssh/id_rsa': 
git@192.168.1.14's password: 
id_rsa_shiyq.pub 100% 395 0.4KB/s 00:00 
[git@centos-s1 .ssh]$ scp id_rsa_shiyq.pub git@192.168.1.14:/opt/git 
Enter passphrase for key '/opt/git/.ssh/id_rsa': 
id_rsa_shiyq.pub 
服務器端 
[git@centos1 ~]$ mv id_rsa_shiyq.pub .ssh/. 
[git@centos1 ~]$ cd .ssh/ 
[git@centos1 .ssh]$ ls 
authorized_keys id_rsa id_rsa.pub id_rsa_shiyq.pub known_hosts 
[git@centos1 .ssh]$ cat id_rsa_shiyq.pub >> authorized_keys 
客戶端 
[git@centos-s1 gitsite1]$ vim test.txt 
[git@centos-s1 gitsite1]$ git add . 
[git@centos-s1 gitsite1]$ git commit -m "add by 12 下午" 
[master 77801a8] add by 12 下午 
1 file changed, 6 insertions(+), 1 deletion(-) 
[git@centos-s1 gitsite1]$ git push git@192.168.1.14:/opt/git/gitsite1.git master 
Enter passphrase for key '/opt/git/.ssh/id_rsa': 
對象計數中: 3, 完成. 
Delta compression using up to 2 threads. 
壓縮對象中: 100% (2/2), 完成. 
寫入對象中: 100% (3/3), 304 bytes | 0 bytes/s, 完成. 
Total 3 (delta 0), reused 0 (delta 0) 
To git@192.168.1.14:/opt/git/gitsite1.git 
4cbda98..77801a8 master -> master 
從遠程服務器下載數據常見的辦法是git fetch和git pull 
git pull比較危險,因為直接從服務器上下載,覆蓋本地 
git fetch是將遠程的分支下載到本地的臨時分支上,可以看有多大區別,然后可以選擇是否合並。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM