更新時(shí)間:2021-12-23 11:33:17 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1896次
在本章中,我們將看到如何創(chuàng)建遠(yuǎn)程 Git 存儲(chǔ)庫(kù);從現(xiàn)在開(kāi)始,我們將把它稱為 Git 服務(wù)器。我們需要一個(gè) Git 服務(wù)器來(lái)允許團(tuán)隊(duì)協(xié)作。
# 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
上述命令將產(chǎn)生以下結(jié)果。
Changing password for user gituser.
New password:
Retype new password:
passwd: all authentication token updated successfully.
讓我們使用init命令后跟--bare選項(xiàng)來(lái)初始化一個(gè)新的存儲(chǔ)庫(kù)。它在沒(méi)有工作目錄的情況下初始化存儲(chǔ)庫(kù)。按照慣例,裸存儲(chǔ)庫(kù)必須命名為.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
讓我們來(lái)看看配置 Git 服務(wù)器的過(guò)程,ssh-keygen實(shí)用程序會(huì)生成公鑰/私鑰 RSA 密鑰對(duì),我們將使用它來(lái)進(jìn)行用戶身份驗(yàn)證。
打開(kāi)終端并輸入以下命令,然后為每個(gè)輸入按回車鍵。成功完成后,它將在主目錄中創(chuàng)建一個(gè).ssh目錄。
tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-keygen
上述命令將產(chǎn)生以下結(jié)果。
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生成了兩個(gè)密鑰,第一個(gè)是私有的(即 id_rsa),第二個(gè)是公共的(即 id_rsa.pub)。
注意:切勿與他人共享您的私鑰。
假設(shè)有兩個(gè)開(kāi)發(fā)人員在從事一個(gè)項(xiàng)目,即 Tom 和 Jerry。兩個(gè)用戶都生成了公鑰。讓我們看看如何使用這些密鑰進(jìn)行身份驗(yàn)證。
Tom 使用ssh-copy-id命令將他的公鑰添加到服務(wù)器,如下所示
[tom@CentOS ~]$ pwd
/home/tom
[tom@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@git.server.com
上述命令將產(chǎn)生以下結(jié)果。
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.
同樣,Jerry 使用 ssh-copy-id 命令將他的公鑰添加到服務(wù)器。
[jerry@CentOS ~]$ pwd
/home/jerry
[jerry@CentOS ~]$ ssh-copy-id -i ~/.ssh/id_rsa gituser@git.server.com
上述命令將產(chǎn)生以下結(jié)果。
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.
我們?cè)诜?wù)器上創(chuàng)建了一個(gè)裸存儲(chǔ)庫(kù),并允許兩個(gè)用戶訪問(wèn)。從現(xiàn)在開(kāi)始,Tom 和 Jerry 可以通過(guò)將其添加為遠(yuǎn)程來(lái)將他們的更改推送到存儲(chǔ)庫(kù)。
每次從.git/config文件讀取配置時(shí),Git init 命令都會(huì)創(chuàng)建.git目錄來(lái)存儲(chǔ)有關(guān)存儲(chǔ)庫(kù)的元數(shù)據(jù)。
Tom 創(chuàng)建一個(gè)新目錄,添加 README 文件,并將他的更改作為初始提交提交。提交后,他通過(guò)運(yùn)行g(shù)it log命令來(lái)驗(yàn)證提交消息。
[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'
上述命令將產(chǎn)生以下結(jié)果。
[master (root-commit) 19ae206] Initial commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 README
Tom 通過(guò)執(zhí)行 git log 命令檢查日志消息。
[tom@CentOS tom_repo]$ git log
上述命令將產(chǎn)生以下結(jié)果。
commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <tom@tutorialspoint.com>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit
Tom 將他的更改提交到本地存儲(chǔ)庫(kù)。現(xiàn)在,是時(shí)候?qū)⒏耐扑偷竭h(yuǎn)程存儲(chǔ)庫(kù)了。但在此之前,我們必須將存儲(chǔ)庫(kù)添加為遠(yuǎn)程,這是一次性操作。在此之后,他可以安全地將更改推送到遠(yuǎn)程存儲(chǔ)庫(kù)。
注意- 默認(rèn)情況下,Git 僅推送到匹配的分支:對(duì)于本地端存在的每個(gè)分支,如果遠(yuǎn)程端已存在同名分支,則會(huì)更新遠(yuǎn)程端。在我們的教程中,每次我們將更改推送到原始主分支時(shí),請(qǐng)根據(jù)您的要求使用適當(dāng)?shù)姆种Q。
[tom@CentOS tom_repo]$ git remote add origin gituser@git.server.com:project.git
[tom@CentOS tom_repo]$ git push origin master
上述命令將產(chǎn)生以下結(jié)果。
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
現(xiàn)在,更改已成功提交到遠(yuǎn)程存儲(chǔ)庫(kù)。如果您想了解更多相關(guān)知識(shí),不妨來(lái)關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的內(nèi)容全面細(xì)致,由淺到深,適合小白學(xué)習(xí),希望對(duì)大家能夠有所幫助。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)