更新時間:2021-12-23 11:33:17 來源:動力節點 瀏覽1670次
在本章中,我們將看到如何創建遠程 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 token 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
讓我們來看看配置 Git 服務器的過程,ssh-keygen實用程序會生成公鑰/私鑰 RSA 密鑰對,我們將使用它來進行用戶身份驗證。
打開終端并輸入以下命令,然后為每個輸入按回車鍵。成功完成后,它將在主目錄中創建一個.ssh目錄。
tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-keygen
上述命令將產生以下結果。
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tom/.ssh/id_rsa): Press Enter Only
Created directory '/home/tom/.ssh'.
Enter passphrase (empty for no passphrase): ---------------> Press Enter Only
Enter same passphrase again: ------------------------------> Press Enter Only
Your identification has been saved in /home/tom/.ssh/id_rsa.
Your public key has been saved in /home/tom/.ssh/id_rsa.pub.
The key fingerprint is:
df:93:8c:a1:b8:b7:67:69:3a:1f:65:e8:0e:e9:25:a1 tom@CentOS
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
|
.
|
| Soo |
| o*B. |
| E = *.= |
| oo==. . |
| ..+Oo
|
+-----------------+
ssh-keygen生成了兩個密鑰,第一個是私有的(即 id_rsa),第二個是公共的(即 id_rsa.pub)。
注意:切勿與他人共享您的私鑰。
假設有兩個開發人員在從事一個項目,即 Tom 和 Jerry。兩個用戶都生成了公鑰。讓我們看看如何使用這些密鑰進行身份驗證。
Tom 使用ssh-copy-id命令將他的公鑰添加到服務器,如下所示
[tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
上述命令將產生以下結果。
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
同樣,Jerry 使用 ssh-copy-id 命令將他的公鑰添加到服務器。
[jerry@CentOS ~]$ pwd
/home/jerry
[jerry@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa [email protected]
上述命令將產生以下結果。
[email protected]'s password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
我們在服務器上創建了一個裸存儲庫,并允許兩個用戶訪問。從現在開始,Tom 和 Jerry 可以通過將其添加為遠程來將他們的更改推送到存儲庫。
每次從.git/config文件讀取配置時,Git init 命令都會創建.git目錄來存儲有關存儲庫的元數據。
Tom 創建一個新目錄,添加 README 文件,并將他的更改作為初始提交提交。提交后,他通過運行git log命令來驗證提交消息。
[tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ mkdir tom_repo
[tom@CentOS ~]$ cd tom_repo/
[tom@CentOS tom_repo]$ git init
Initialized empty Git repository in /home/tom/tom_repo/.git/
[tom@CentOS tom_repo]$ echo 'TODO: Add contents for README' > README
[tom@CentOS tom_repo]$ git status -s
?? README
[tom@CentOS tom_repo]$ git add .
[tom@CentOS tom_repo]$ git status -s
A README
[tom@CentOS tom_repo]$ git commit -m 'Initial commit'
上述命令將產生以下結果。
[master (root-commit) 19ae206] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
Tom 通過執行 git log 命令檢查日志消息。
[tom@CentOS tom_repo]$ git log
上述命令將產生以下結果。
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
Tom 將他的更改提交到本地存儲庫?,F在,是時候將更改推送到遠程存儲庫了。但在此之前,我們必須將存儲庫添加為遠程,這是一次性操作。在此之后,他可以安全地將更改推送到遠程存儲庫。
注意- 默認情況下,Git 僅推送到匹配的分支:對于本地端存在的每個分支,如果遠程端已存在同名分支,則會更新遠程端。在我們的教程中,每次我們將更改推送到原始主分支時,請根據您的要求使用適當的分支名稱。
[tom@CentOS tom_repo]$ git remote add origin [email protected]:project.git
[tom@CentOS tom_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 [email protected]:project.git
* [new branch]
master ?> master
現在,更改已成功提交到遠程存儲庫。如果您想了解更多相關知識,不妨來關注一下動力節點的Java在線學習,里面的內容全面細致,由淺到深,適合小白學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習