• <th id="0iybx"></th>

    <button id="0iybx"></button>

    <em id="0iybx"><tr id="0iybx"></tr></em>

    <progress id="0iybx"><track id="0iybx"></track></progress>
    <rp id="0iybx"></rp>
  • <nav id="0iybx"></nav>

  • <rp id="0iybx"></rp>
  • 專注IT教育14年 全國咨詢/投訴熱線:400-8080-105
    動力節點LOGO圖
    始于2009,一家只教授Java的培訓機構
    首頁 hot資訊 Git創建操作步驟

    Git創建操作步驟

    更新時間:2021-12-23 11:33:17 來源:動力節點 瀏覽911次

    在本章中,我們將看到如何創建遠程 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

    生成公共/私人 RSA 密鑰對

    讓我們來看看配置 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)。

    注意:切勿與他人共享您的私鑰。

    將密鑰添加到authorized_keys

    假設有兩個開發人員在從事一個項目,即 Tom 和 Jerry。兩個用戶都生成了公鑰。讓我們看看如何使用這些密鑰進行身份驗證。

    Tom 使用ssh-copy-id命令將他的公鑰添加到服務器,如下所示

    [tom@CentOS ~]$ pwd
    /home/tom
    [tom@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.

    同樣,Jerry 使用 ssh-copy-id 命令將他的公鑰添加到服務器。

    [jerry@CentOS ~]$ pwd
    /home/jerry
    [jerry@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.

    將更改推送到存儲庫

    我們在服務器上創建了一個裸存儲庫,并允許兩個用戶訪問。從現在開始,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 <tom@tutorialspoint.com>
    Date: Wed Sep 11 07:32:56 2013 +0530
    Initial commit

    Tom 將他的更改提交到本地存儲庫?,F在,是時候將更改推送到遠程存儲庫了。但在此之前,我們必須將存儲庫添加為遠程,這是一次性操作。在此之后,他可以安全地將更改推送到遠程存儲庫。

    注意- 默認情況下,Git 僅推送到匹配的分支:對于本地端存在的每個分支,如果遠程端已存在同名分支,則會更新遠程端。在我們的教程中,每次我們將更改推送到原始主分支時,請根據您的要求使用適當的分支名稱。

    [tom@CentOS tom_repo]$ git remote add origin gituser@git.server.com: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 gituser@git.server.com:project.git
    * [new branch]
    master ?> master

    現在,更改已成功提交到遠程存儲庫。如果您想了解更多相關知識,不妨來關注一下動力節點的Java在線學習,里面的內容全面細致,由淺到深,適合小白學習,希望對大家能夠有所幫助。

    免費課程推薦 >>
    技術文檔推薦 >>
    返回頂部
    天天做完夜夜做狠狠做

  • <th id="0iybx"></th>

    <button id="0iybx"></button>

    <em id="0iybx"><tr id="0iybx"></tr></em>

    <progress id="0iybx"><track id="0iybx"></track></progress>
    <rp id="0iybx"></rp>
  • <nav id="0iybx"></nav>

  • <rp id="0iybx"></rp>