本文是基於ssh搭建git私服的學習總結,下面將會介紹如何搭建屬於自己的遠程庫
在自己的服務器上安裝git
我的服務器是centos7
yum install git
- 1
若是ubuntu或自己安裝了apt安裝管理工具 ,也可以使用apt-get install git
進行安裝,當然,安裝之前可以先升級,安裝比較新版本的git.我好想沒升級安裝的是1.8
[root@iZ2ze3ldkm1p7szyuskxiwZ mygit]# git --version git version 1.8.3.1
- 1
- 2
創建專門的遠程倉庫
其實本地庫和遠程庫沒有什么太大區別,只不過遠程庫存放了各個不同用戶的本地庫push的數據,通俗的說,也就是遠程庫保存了很多文件.用戶push一個,遠程庫就存一個.存好了,那么下次用戶就可以pul或fecth取東西了.下面就是具體在服務器創建遠程庫的具體過程:
創建一個新用戶(可有可無)
為了方便管理,我們可以在linux服務器上創建一個新用戶,就叫git.
adduser git
- 1
給這個git設置密碼
passwd git
- 1
這個新用戶信息保存在/etc/passwd文件里,我們查看后五行,如下,可以看到git賬戶信息在最一行.
他的家目錄是/home/git, shell是/bin/bash,當然,出於安全考慮,創建的git用戶應該不允許登錄shell,可以通過編輯/etc/passwd文件完成.當然,創建用戶的時候就可以使用-s參數指定禁用shell,或者使用usermod修改.我們先不修改.繼續操作.
初始化遠程庫
進入git的家目錄/home/git,創建一個倉庫test-repo
[root@iZ2ze3ldkm1p7szyuskxiwZ ~]# su git [git@iZ2ze3ldkm1p7szyuskxiwZ root]$ cd ~ [git@iZ2ze3ldkm1p7szyuskxiwZ ~]$ pwd /home/git [git@iZ2ze3ldkm1p7szyuskxiwZ ~]$ ls #我已經創建好了,沒有可以創建 test-repo [git@iZ2ze3ldkm1p7szyuskxiwZ ~]$ cd test-repo/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
初始化一個裸庫,不需要.git目錄
git init --bare
- 1
當然如果你非要這個目錄,也不是不可以.下面這張圖說明了加上–bare參數與不加有什么變化
可以看到主要文件都差不多,但也有幾個文件不一樣.
本地進行push
git服務器就基本就搭建好了.現在本地就可以遠程push了.遠程庫的地址git@182.92.200.149:/home/git/test-repo
git push git@182.92.200.149:/home/git/test-repo master
- 1
輸入git的密碼,就可以push成功了
進入遠程庫確認,如下,可以看到多了一個分支,就是剛才push的
[git@iZ2ze3ldkm1p7szyuskxiwZ test-repo]$ ls -a . .. branches config description HEAD hooks info objects refs [git@iZ2ze3ldkm1p7szyuskxiwZ test-repo]$ git branch -v * master c964f4f tom開發了一個模塊 * [git@iZ2ze3ldkm1p7szyuskxiwZ test-repo]$
- 1
- 2
- 3
- 4
- 5
- 6
本地clone
我們在本地新建一個空的test目錄,如下
現在進行clone,就把剛才的克隆下來git clone git@182.92.200.149:/home/git/test-repo master
root@xiaoxin-Inspiron-5567:/usr/local/gitrepo-tet/test# git clone git@182.92.200.149:/home/git/test-repo master Cloning into 'master'... The authenticity of host '182.92.200.149 (182.92.200.149)' can't be established. ECDSA key fingerprint is SHA256:hMZZ9soJQf5V4hhGe/ddcwdFnt91GErC5q/BxIuPxbw. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '182.92.200.149' (ECDSA) to the list of known hosts. git@182.92.200.149's password: remote: Counting objects: 6, done. remote: Compressing objects: 100% (4/4), done. remote: Total 6 (delta 1), reused 0 (delta 0) Receiving objects: 100% (6/6), done. Resolving deltas: 100% (1/1), done. root@xiaoxin-Inspiron-5567:/usr/local/gitrepo-tet/test# ls -a . .. master root@xiaoxin-Inspiron-5567:/usr/local/gitrepo-tet/test# cd master/ root@xiaoxin-Inspiron-5567:/usr/local/gitrepo-tet/test/master# ls -a . .. .git test.md root@xiaoxin-Inspiron-5567:/usr/local/gitrepo-tet/test/master#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
也沒問題.