標籤:and 放心 content rip http minus public make expect
在本章中,我們將看到如何建立一個遠程Git倉庫,從現在開始,我們將會把它作為Git伺服器。我們需要一個的Git伺服器允許團隊協作。
建立新使用者
# add new group
[root@CentOS ~]# groupadd dev # add new user
[[email protected] ~]# useradd -G devs -d /home/gituser -m -s /bin/bash gituser
# change password
[[email protected] ~]# passwd gituser
上面的命令會產生以下結果。
Changing password for user gituser. New password: Retype new password: passwd: all authentication tokens updated successfully.
建立一個裸庫
讓我們初始化一個新的資料庫使用init命令後面加上 -bare選項。它初始化沒有工作目錄庫。按照慣例裸庫必須命名為 .git。
[[email protected] ~]$ pwd /home/gituser
[[email protected] ~]$ mkdir project.git
[[email protected] ~]$ cd project.git/
[[email protected] project.git]$ ls
[[email protected] project.git]$ git –bare init Initialized empty Git repository in /home/gituser-m/project.git/
[[email protected] project.git]$ ls branches config description HEAD hooks info objects refs
產生公用/私人RSA金鑰組
讓我們遍曆Git伺服器端的配置過程中,使用ssh-keygen公用程式產生公用/私人RSA金鑰組,我們將使用這些鍵進行使用者認證。
開啟一個終端並輸入以下命令,直接按斷行符號為每個輸入。成功完成後,它會建立主目錄 .ssh目錄內。
[email protected] ~]$ pwd /home/tom [[email protected] ~]$ 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 [email protected] The key’s randomart image is: +–[ RSA 2048]—-+ | | | | | | | . | | Soo | | o*B. | | E = *.= | | oo==. . | | ..+Oo | +—————–+
ssh-keygen 已經產生了兩個鍵,第一個是私人的(即id_rsa),另一個是公用(即id_rsa.pub檔案)。
注: 切勿與他人共用你的私密金鑰。
添加鍵 authorized_keys
假設有兩個開發項目即Tom 和Jerry工作。兩個使用者產生公開金鑰。讓我們來看看如何使用這些密鑰進行身分識別驗證。
Tom 添加他的公開金鑰伺服器使用 ssh-copy-id這個命令下面給出
[[email protected] ~]$ pwd /home/tom
[[email protected] ~]$ 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 這個命令。
[[email protected] ~]$ pwd /home/jerry
[[email protected] ~]$ 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的init命令建立 .git 目錄來儲存元資料的存放庫。每次讀取配置從 .git/config 檔案.
Tom 建立一個新的目錄,添加READE檔案作為初始提交並提交他的變化。提交後,他確認提交資訊,運行git日誌命令。
[[email protected] ~]$ pwd /home/tom
[[email protected] ~]$ mkdir tom_repo
[[email protected] ~]$ cd tom_repo/
[[email protected] tom_repo]$ git init Initialized empty Git repository in /home/tom/tom_repo/.git/
[[email protected] tom_repo]$ echo ‘TODO: Add contents for README’ > README
[[email protected] tom_repo]$ git status -s ?? README
[[email protected] tom_repo]$ git add .
[[email protected] tom_repo]$ git status -s A README
[[email protected] 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 的日誌命令,檢查日誌訊息。
[[email protected] tom_repo]$ git log
上面的命令會產生以下結果。
commit 19ae20683fc460db7d127cf201a1429523b0e319 Author: Tom Cat <[email protected]> Date: Wed Sep 11 07:32:56 2013 +0530 Initial commit
Tom 提交了他的變化到本地資產庫。現在是時候將更改到遠程倉庫。但在此之前,我們必須添加作為遠程倉庫,這是一個時間的操作。在此之後,他可以放心地推送到遠程存放庫的更改。
注: 預設情況下,Git的推到匹配的分支:對於每一個分支退出本地端的遠程端更新,如果已經存在具有相同名稱的一個分支。在我們的教程每次我推原點主分支的變化,根據您的要求,使用適當的分支名。
[[email protected] tom_repo]$ git remote add origin [email protected]:project.git
[[email protected] 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
現在更改成功提交到遠程倉庫。
PS:如果您想和業內技術大牛交流的話,請加qq群(521249302)或者關注公眾 號(AskHarries),謝謝!
Git 建立操作