在本章中,我們將看到如何創建一個遠程Git倉庫,從現在開始,我們將會把它作為Git服務器。我們需要一個的Git服務器允許團隊協作。
## 創建新用戶
# add new group
[root@CentOS ~]# groupadd dev
# add new user
[root@CentOS ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser
# change password
[root@CentOS ~]# passwd gituser
上面的命令會產生以下結果。
Changing password for user gituser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
## 創建一個裸庫
讓我們初始化一個新的資料庫使用init命令后面加上 -bare選項。它初始化沒有工作目錄庫。按照慣例裸庫必須命名為 .git。
[gituser@CentOS ~]$ pwd
/home/gituser
[gituser@CentOS ~]$ mkdir project.git
[gituser@CentOS ~]$ cd project.git/
[gituser@CentOS project.git]$ ls
[gituser@CentOS project.git]$ git --bare init
Initialized empty Git repository in /home/gituser-m/project.git/
[gituser@CentOS project.git]$ ls
branches config description HEAD hooks info objects refs
## 生成公共/私有RSA密鑰對
讓我們遍歷Git服務器端的配置過程中,使用ssh-keygen實用程序生成公共/私有RSA密鑰對,我們將使用這些鍵進行用戶認證。
打開一個終端并輸入以下命令,直接按回車為每個輸入。成功完成后,它會創建主目錄 .ssh目錄內。
sampson@CentOS ~]$ pwd
/home/sampson
[sampson@CentOS ~]$ ssh-keygen
上面的命令會產生以下結果。
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sampson/.ssh/id_rsa): Press Enter Only
Created directory '/home/sampson/.ssh'.
Enter passphrase (empty for no passphrase): ---------------> Press Enter Only
Enter same passphrase again: ------------------------------> Press Enter Only
Your identification has been saved in /home/sampson/.ssh/id_rsa.
Your public key has been saved in /home/sampson/.ssh/id_rsa.pub.
The key fingerprint is:
df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 sampson@CentOS
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
|
.
|
| Soo |
| o*B. |
| E = *.= |
| oo==. . |
| ..+Oo
|
+-----------------+
ssh-keygen 已經產生了兩個鍵,第一個是私有的(即id_rsa),另一個是公共(即id_rsa.pub文件)。
> 注: 切勿與他人共享你的私鑰。
## 添加鍵 authorized_keys
假設有兩個開發項目即Sampson 和Byron工作。兩個用戶生成公鑰。讓我們來看看如何使用這些密鑰進行身份驗證。
Sampson 添加他的公鑰服務器使用 ssh-copy-id這個命令下面給出
[sampson@CentOS ~]$ pwd
/home/sampson
[sampson@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git.server.com
上面的命令會產生以下結果。
gituser@git.server.com's password:
Now try logging into the machine, with "ssh 'gituser@git.server.com'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
同樣,Byron 也增加了他的公共密鑰服務器使用 ssh-copy-id 這個命令。
[Byron@CentOS ~]$ pwd
/home/Byron
[Byron@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa gituser@git.server.com
上面的命令會產生以下結果。
gituser@git.server.com's password:
Now try logging into the machine, with "ssh 'gituser@git.server.com'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
## 推修改到版本庫
我們已經創建了裸庫在服務器上,并允許兩個用戶訪問。從現在Sampson 和 Byron 可以把他們修改到版本庫,將其添加為遠程。
Git的init命令創建 .git 目錄來存儲元數據的存儲庫。每次讀取配置從 .git/config 文件.
Sampson 創建一個新的目錄,添加READE文件作為初始提交并提交他的變化。提交后,他確認提交信息,運行git日志命令。
[sampson@CentOS ~]$ pwd
/home/sampson
[sampson@CentOS ~]$ mkdir sampson_repo
[sampson@CentOS ~]$ cd sampson_repo/
[sampson@CentOS sampson_repo]$ git init
Initialized empty Git repository in /home/sampson/sampson_repo/.git/
[sampson@CentOS sampson_repo]$ echo 'TODO: Add contents for README' > README
[sampson@CentOS sampson_repo]$ git status -s
?? README
[sampson@CentOS sampson_repo]$ git add .
[sampson@CentOS sampson_repo]$ git status -s
A README
[sampson@CentOS sampson_repo]$ git commit -m 'Initial commit'
上面的命令會產生以下結果。
[master (root-commit) 19ae206] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
Sampson 執行git 的日志命令,檢查日志消息。
[sampson@CentOS sampson_repo]$ git log
上面的命令會產生以下結果。
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Sampson Cat
Date: Wed Sep 11 07:32:56 2015 +0530
Initial commit
Sampson 提交了他的變化到本地資源庫。現在是時候將更改到遠程倉庫。但在此之前,我們必須添加作為遠程倉庫,這是一個時間的操作。在此之后,他可以放心地推送到遠程存儲庫的更改。
> 注: 默認情況下,Git的推到匹配的分支:對于每一個分支退出本地端的遠程端更新,如果已經存在具有相同名稱的一個分支。在我們的教程每次我推原點主分支的變化,根據您的要求,使用適當的分支名。
[sampson@CentOS sampson_repo]$ git remote add origin gituser@git.server.com:project.git
[sampson@CentOS sampson_repo]$ git push origin master
上面的命令會產生以下結果。
Counting objects: 3, done.
Writing objects: 100% (3/3), 242 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To gituser@git.server.com:project.git
* [new branch]
master ?> master
現在更改成功提交到遠程倉庫。