使用Git分佈式控制系統

問題1:爲什麼要有這個Git?
個人理解:開發者們通過郵件向Linus發送着自己編寫的源代碼文件,由Linus本人通過手工的方式將代碼合併。能不能每個人本地都有一個服務端的源碼數據庫,這樣不必再完全依賴於服務端的源碼數據庫,使得源代碼的發佈和合並更加方便。所以git誕生了!

實例:GitHub是通過Git進行版本控制的軟件源代碼託管服務,用戶可以免費創建公開的代碼倉庫。(程序員都知道吧!)

問題2:什麼是Git?
答:Git 是一個分佈式版本控制軟件,與CVS、Subversion一類的集中式版本控制工具不同,它採用了分佈式版本庫的作法,不需要服務器端軟件,就可以運作版本控制,使得源代碼的發佈和交流極其方便!

一:Git服務程序運行思路
文字:
1.在工作目錄中修改數據文件。
2.將文件的快照放入暫存區域。
3.將暫存區域的文件快照提交到Git倉庫中。
圖片:
在這裏插入圖片描述

二:Git服務基礎命令:
個人的用戶名稱和電子郵件地址:
git config --global user.name “Name”
git config --global user.email “root@email”
提交數據:
git add readme.txt
將暫存區的文件提交到Git版本倉庫:
git commit -m “解釋說明”
查看下當前Git版本倉庫的狀態:
git status

三:部署Git服務器
Git是分佈式的版本控制系統,我們只要有了一個原始的Git倉庫,就可以讓其他主機克隆我們的原始版本倉庫,從而使得一個Git版本倉庫可以被同時分佈到不同的主機之上。
1.安裝git服務
[root@caq ~]# yum install git
2.創建Git版本倉庫,要以.git爲後綴:
[root@caq ~]# mkdir caq.git
3.修改Git版本倉庫的所有者與所有組
[root@caq ~]# chown -Rf git:git caq.git/
4.初始化Git版本倉庫:
[root@caq ~]# cd caq.git/
[root@caq caq.git]# git --bare init Initialized empty Git repository in /root/caq.git/
5.服務器上的Git倉庫此時已經部署好了,但是還不能分享給其他人。需要有個協議來限制。猜到了吧,是ssh協議!
在客戶端生成ssh密鑰
[root@caq ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
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:
65:4a:53:0d:4f:ee:49:4f:94:24:82:16:7a:dd:1f:28 [email protected]
The key’s randomart image is:
±-[ RSA 2048]----+
| .o+oo.o. |
| .oo *.+. |
| …+ E * o |
| o = + = . |
| S o o |
| |
| |
| |
| |
±----------------+
將客戶機的公鑰傳遞給Git服務器:
[root@caq ~]# ssh-copy-id 192.168.10.10 [email protected]’s password: Number of key(s) added: 1 Now try logging into the machine, with: “ssh ‘192.168.10.10’” and check to make sure that only the key(s) you wanted were added.

6.現在客戶端就可以克隆服務端的版本倉庫了。
[root@caq ~]# git clone [email protected]:/root/caq.git Cloning into ‘caq’… warning: You appear to have cloned an empty repository.

7.初始化下Git工作環境:
[root@caq ~]# git config --global user.name “Liu Chuan” [root@caq ~]# git config --global user.email “[email protected]” [root@caq ~]# git config --global core.editor vim
8.向Git版本倉庫中提交一個新文件:
[root@caq caq]# echo “I am fine” > readme.txt
[root@caq caq]# git add readme.txt
[root@caq caq]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm --cached …” to unstage)
#
# new file: readme.txt
#
[root@caq caq ]# git commit -m “Clone the Git repository”
[master (root-commit) c3961c9] Clone the Git repository
Committer: root
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@linuxprobe caq]# git status
# On branch master
nothing to commit, working directory clean

9.向服務器發送文件
[root@caq caq]# git remote add server [email protected]:/root/caq.git
[root@caq caq]# git push -u server master
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]:/root/caq.git
[new branch] master -> master
Branch master set up to track remote branch master from server.
10.查看遠程服務器的倉庫有無客戶端發來的文件(我是又克隆了一份,當然也可以直接切換到服務器共享倉庫查看)
[root@caq caq]# cd …/~
[root@caq ~]# git clone [email protected]:/root/caq.git
Cloning into ‘caq’…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@caq ~]# cd caq/
[root@caq caq]# cat readme.txt I am fine

git內容知識點很多,這篇要是看的人多的話我在出剩餘的知識吧。偷個懶,嘻嘻嘻。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章