建立一個git本地倉庫
1.實驗目的
學會如何快速創建一個git本地倉庫,並將其運用到實際工作中。
2.實驗步驟
通常實際項目中會使用一臺獨立的機器作為git服務器,然後在git服務器中建立一個遠程的倉庫,這樣項目中所有的人都可以通過局域網來訪問這個git服務器。當然,我們在本實驗中可以使用同一臺機器來模擬這個git服務器。
(1)git服務器端的操作
首先需要在服務器端建立一個目錄,然後初始化這個git倉庫。假設我們在“/opt/git/”目錄下面來創建。
$ cd /opt/git/
$ mkdir test.git
$ cd test.git/
$ git --bare init
Initialized empty Git repository in /opt/git/test.git/
通過git --bare init命令創建了一個空的遠程倉庫。
(2)客戶端的操作
打開另外一個終端,然後在本地工作目錄中編輯代碼,比如在home目錄下。
$ cd /home/ben/
$ mkdir test
編輯一個test.c文件,添加簡單的打印“hello world”的語句。
$ vim test.c
初始化本地的git倉庫。
$ git init
Initialized empty Git repository in /home/figo/work/test/.git/
查看當前工作區的狀態。
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.c
nothing added to commit but untracked files present (use "git add" to track)
/<file>
可以看到工作區裡有一個test.c文件,然後通過git add命令來添加test.c文件到緩存區中。
$ git add test.c
用git commit生成一個新的提交。
$ git commit –s
test: add init code for xxx project
Signed-off-by: Ben Shushu <runninglinuxkernel>
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: test.c
#
/<runninglinuxkernel>
在上述代碼中添加對這個新提交的描述,保存之後自動生成一個新的提交。
$ git commit -s
[master (root-commit) ea92c29] test: add init code for xxx project
1 file changed, 8 insertions(+)
create mode 100644 test.c
接下來需要把本地的git倉庫推送到遠程倉庫中。
首先需要通過git remote add命令添加剛才遠程倉庫的地址。
$ git remote add origin ssh://[email protected]:/opt/git/test.git
其中“192.168.0.1”是服務器端的IP地址,“ben”是服務器端的登錄名。最後用git push命令來推送。
$ git push origin master
[email protected]'s password:
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://[email protected]:/opt/git/test.git
* [new branch] master -> master
(3)複製遠端倉庫
這時我們就可以在局域網內通過git clone複製這個遠程倉庫到本地了。
$ git clone ssh://[email protected]:/opt/git/test.git
Cloning into 'test'...
[email protected]'s password:
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.
$ cd test/
$ git log
commit ea92c29d88ba9e58960ec13911616f2c2068b3e6
Author: Ben Shushu <runninglinuxkernel>
Date: Mon Apr 16 23:13:32 2018 +0800
test: add init code for xxx project
Signed-off-by: Ben Shushu <runninglinuxkernel>
/<runninglinuxkernel>/<runninglinuxkernel>
如何參與抽獎?
【關注】異步社區+點擊小程序【參與抽獎】,即有機會獲得本書。
END
喜歡的朋友請轉發到朋友圈
閱讀更多 異步社區 的文章