大家對Github一定不陌生,雖然我們可以在Github上申請私有倉庫,但是在某些情況下,我們還是需要一個(gè)可以自己管理倉庫的私有Git Server。
此篇文章旨在介紹如何簡單的在Mac上搭建Git Server,有需要企業(yè)級管理的同學(xué)可以搜索Gitosis、Gitolite、Gitlab之類的管理軟件。
一,創(chuàng)建Git用戶
1,在用作服務(wù)器的機(jī)器上創(chuàng)建 git 賬戶。我們可以通過 系統(tǒng)偏好設(shè)置->用戶與群組 來添加。賬戶權(quán)限給的是管理員權(quán)限為了方便操作。

2,設(shè)置遠(yuǎn)程訪問
在系統(tǒng)偏好設(shè)置->共享 中,勾選僅這些用戶允許訪問。

二,Git Server設(shè)置
1,驗(yàn)證Git用戶
$ ssh git@yourComputerName.local
按提示輸入git用戶的密碼,如圖出現(xiàn)**~ git$ **提示則說明登陸成功。

2, 生成 ssh rsa 公鑰
- 如果client端已經(jīng)創(chuàng)建ssh rsa 過公鑰,則略過此步驟:
$ cd ~
$ ssh-keygen -t rsa
# 公鑰默認(rèn)生成路徑 ~/.ssh/id_rsa.pub
# 可以拷貝到其他路徑改名$ cp ~/.ssh/id_rsa.pub /你的路徑/新名稱.pub
- 把ssh rsa 公鑰拷貝到Git Server端。
# 使用git用戶登錄,并創(chuàng)建.ssh 文件夾
$ ssh git@yourComputerName.local mkdir .ssh
$ scp ~/.ssh/id_rsa.pub git@yourComputerName.local:.ssh/authorized_keys
# 之后會出現(xiàn)文件傳輸信息
- 修改Git Server端的sshd_config文件
$ ssh git@yourComputerName.local
$ cd /etc
$ sudo chmod 666 sshd_config
注意:這里有個(gè)需要注意的地方,/etc文件夾可能沒有sshd_config文件,只有有一個(gè)sshd_config~previous文件,那我們操作的文件就換成sshd_config~previous。
- 修改sshd_config中的內(nèi)容
$ vi sshd_config
#PermitRootLogin yes 改為 PermitRootLogin no
#下面的去掉 # 注釋
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
#PasswordAuthentication no
#PermitEmptyPasswords no
#UsePAM yes 改為 UsePAM no
#保存文件并退出


- 在Git Server端創(chuàng)建空的 repository
$ cd ~
$ cd Desktop/gitrepos/
#這里的路徑是自定義的,創(chuàng)建你需要路徑,
$ mkdir newgitrepo.git # 同上 自定義名稱
$ cd newrepo.git
$ git init --bare # --bare 參數(shù)表明只是用來存儲 pushes,不會當(dāng)做本地 repository 來使用。
- 回到在Git Client端,創(chuàng)建本地倉庫并提交。
注意:先在終端中使用exit命令退出git用戶。
$ cd ~
$ cd Desktop/gitrepos/ #這里我使用和服務(wù)器相同路徑
$ mkdir newrepo
$ git init
$ touch README
$ git add .
$ git commit -m "initial commit"
$ git remote add origin git@yourComputerName:Desktop/gitrepos/newrepo.git
# 如果直接之前在 server 端創(chuàng)建的 newrepo.git 是在 home 目錄($ cd ~),則這里地址為:git@yourComputerName:newrepo.git
$ git push origin master
# 之后可以看到提交信息
總結(jié)
這樣,我們就完成了在Mac中搭建私有Git Server的操作。
如果你的應(yīng)用場景是企業(yè)級也可以參考這篇來設(shè)置服務(wù)器上的Git Server,或者搜索Gitosis、Gitolite、Gitlab之類的管理軟件。
最后
感謝閱讀,如果對大家有幫助,請?jiān)?a target="_blank" rel="nofollow">github上follow和star,本文發(fā)布在逆流的簡書博客,轉(zhuǎn)載請注明出處