Git可以在本地的倉庫中使用,自己管理版本,但是要團隊合作的話就需要一個Git伺服器來進行協作,最典型的Git伺服器是美國的GitHub、中國的碼雲等,也可以自己搭建類似GitHub的Git伺服器例如免費的Gitlab,但是GitLab需要Nginx或者Apache的支援,搭建比較麻煩,而且對伺服器的性能要求也高了不少,以後再用GitLab吧,現在先搭建一個基于控制台的Git伺服器。
Git伺服器運作條件:Ubuntu16.04、git、ssh等。
1、假設根據上級需求,我們要開發一個密碼鎖的項目,取名叫lock,在ubuntu伺服器上建立一個lock檔案夾并使用Git初始化成倉庫:
tq@ubuntu:~/lock$ git --bare init
Initialized empty Git repository in /home/tq/lock/
tq@ubuntu:~/lock$ ls
branches config description HEAD hooks info objects refs
注意使用的是git –bare init而不是git init,詳細原因:一般個人使用,用git init,這時候你的工作區也在這裡。你要是想建立一個固定的位址讓大家一起用,就在伺服器上用git –bare init。其實你可以看到,init建立的.git目錄内容和–bare建立的目錄内容是差不多的。在初始化遠端倉庫時最好使用 git –bare init 而不要使用:git init。這樣在使用hooks的時候,會有用處。如果使用了git init初始化,則遠端倉庫的目錄下,也包含work tree,當本地倉庫向遠端倉庫push時, 如果遠端倉庫正在push的分支上(如果當時不在push的分支,就沒有問題), 那麼push後的結果不會反應在work tree上, 也即在遠端倉庫的目錄下對應的檔案還是之前的内容,必須得使用git reset –hard才能看到push後的内容。(參考http://blog.haohtml.com/archives/12265)
如果使用git init初始化倉庫的話,本地倉庫向伺服器執行push操作的時候如果所push的分支和伺服器倉庫工作區正處在的倉庫是一樣的話會報錯:
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.
......
如果将遠端倉庫的分支切換到其他分支,然後本地分支在執行push操作,這時候伺服器倉庫工作區的分支和push操作的分支不一樣push操作就能正常執行了,但是這樣很不友善,是以還是不要用git init來初始化服務區上的倉庫了,雖然用git –bare init初始化的倉庫沒有工作區,但是不妨礙伺服器作為協同多人工作的任務,我們要工作在自己本地倉庫的工作區工作就行了。
2、現在在伺服器上建立了一個空的倉庫,什麼都沒有,我們可以在本地建立一個非空的倉庫然後上傳到伺服器倉庫:
tangquan@yalishiquede MINGW32 /i/me
$ git init
Initialized empty Git repository in I:/me/.git/
tangquan@yalishiquede MINGW32 /i/me (master)
$ vi a
tangquan@yalishiquede MINGW32 /i/me (master)
$ git add a
warning: LF will be replaced by CRLF in a.
The file will have its original line endings in your working directory.
tangquan@yalishiquede MINGW32 /i/me (master)
$ git commit -m "add a"
[master (root-commit) 7ffebb8] add a
warning: LF will be replaced by CRLF in a.
The file will have its original line endings in your working directory.
1 file changed, 1 insertion(+)
create mode 100644 a
tangquan@yalishiquede MINGW32 /i/me (master)
$ git log
commit 7ffebb82156da03dc17c486c28308799fa7479a0
Author: tangquan <[email protected]>
Date: Tue Sep 19 10:43:17 2017 +0800
add a
tangquan@yalishiquede MINGW32 /i/me (master)
$ git remote add origin [email protected]:/home/tq/lock
tangquan@yalishiquede MINGW32 /i/me (master)
$ git push origin master
[email protected]'s password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/tq/lock
* [new branch] master -> master
這個本地倉庫是在Windows下建立的,使用指令:git remote add origin [email protected]:/home/tq/lock 添加遠端倉庫的位址,然後使用指令:git push origin master 将本地倉庫送出到伺服器倉庫。這時候伺服器倉庫上就有了master分支了。
3、開發工作需要多人進行協同工作,假設lock項目組有一個成員叫tom,他需要先clone伺服器倉庫到本地:
tangquan@yalishiquede MINGW32 /i/tom (master)
$ git clone [email protected]:/home/tq/lock
Cloning into 'lock'...
[email protected]'s password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
tangquan@yalishiquede MINGW32 /i/tom (master)
$ cd lock/
tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ git b
* master
好了現在tom已經獲得了項目工程源碼了,可以開始他的工作了。我們需要tom開發一個IIC驅動,tom不願意也得做:
原來工程中的a檔案内容隻有一行tangquan:
tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ cat a
tangquan
tom要開發IIC驅動,他不能直接在master分支上開發,他需要建立一個新分支IIC:
tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ git co -b iic
Switched to a new branch 'iic'
然後tom添加了IIC驅動然後送出到本地倉庫并且上傳到伺服器:
tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ vi a
tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ cat a
tangquan
tom add iic driver
tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git add a
tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git commit -m "tom add iic driver"
[iic f46a3d2] tom add iic driver
1 file changed, 1 insertion(+)
tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git push origin iic
[email protected]'s password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/home/tq/lock
* [new branch] iic -> iic
然後我們檢視伺服器端的倉庫内容:
tq@ubuntu:~/lock$ git b
iic
* master
tq@ubuntu:~/lock$ git co iic
fatal: This operation must be run in a work tree
我們試圖在伺服器上切換分支的時候提示這個操作必須在一個工作區上運作,然而我們伺服器的倉庫是由git –bare init建立的,沒有工作區,好,那我們在原始建立倉庫的工作目錄下進行檢視(這個原始建立倉庫的人就是主導開發項目的經理,就是我):
tangquan@yalishiquede MINGW32 /i/me (master)
$ git fetch origin iic:iic
[email protected]'s password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.1.143:/home/tq/lock
* [new branch] iic -> iic
+ e3f3088...f46a3d2 iic -> origin/iic (forced update)
tangquan@yalishiquede MINGW32 /i/me (master)
$ git b
iic
* master
tangquan@yalishiquede MINGW32 /i/me (master)
$ git co iic
Switched to branch 'iic'
tangquan@yalishiquede MINGW32 /i/me (iic)
$ cat a
tangquan
tom add iic driver
tangquan@yalishiquede MINGW32 /i/me (iic)
$ git co master
Switched to branch 'master'
tangquan@yalishiquede MINGW32 /i/me (master)
$ git merge iic
Updating 7ffebb8..f46a3d2
Fast-forward
a | 1 +
1 file changed, 1 insertion(+)
tangquan@yalishiquede MINGW32 /i/me (master)
$ cat a
tangquan
tom add iic driver