在自己電腦上搭建Git私有服務器
一開始肯定是百度踊兜,因為整個問題維度很低犹赖,大概一篇文章就夠用,篩選后應該就是這篇: git 服務器搭建董瞻,在自己服務器上搭建私有倉庫
步驟開始:
在服務器上(我用的是Ubuntu)安裝git寞蚌,簡單,不說了钠糊。
在客戶機上(Win10)安裝Git挟秤,簡單,不說了抄伍。
-
在服務器上添加一個用戶艘刚,一般都叫g(shù)it,這個不重要截珍。
# useradd git -d /home/git -m -s /bin/bash -d:指定用戶目錄 -m:如果目錄不存在則創(chuàng)建 -s:可以指定用戶使用的命令
-
創(chuàng)建git倉庫
$ su git $ cd ~ $ git init --bare myserver.git --bare選項(bare漢語意思是:裸,裸的)初始化的版本庫(暫且稱為bare repository)只會生成一類文件:用于記錄版本庫歷史記錄的攀甚。
摘自git init 與 git init --bare 的區(qū)別,大概的意思就是生成了一些初始化的系統(tǒng)配置文件岗喉。
服務端設置從現(xiàn)在看就差不多了秋度,一會兒還有別的,先配置客戶機
-
生成密鑰對
ssh-keygen -t rsa // 會在 ~/.ssh/钱床,生成 'id_rsa' 和 'id_rsa.pub' 2個文件
生成這倆東西的作用僅僅是以后免密登錄荚斯,摘自ssh-kengen的使用說明。
將
id_rsa.pub
傳到服務器上诞丽,執(zhí)行cat id_rsa.pub >> ~/.ssh/authorized_keys
鲸拥,此語句作用大家都懂。-
命令行下輸入
git clone git@IP:/home/git/myserver.git
僧免,常用命令如下刑赶,含義顧名思義。$ git add . $ git commit -m "first commit" $ git push
-
這樣會報一個錯:
remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable t remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int remote: error: its current branch; however, this is not recommended unless you remote: error: arranged to update its work tree to match what you pushed in som remote: error: other way. remote: error: remote: error: To squelch this message and still keep the default behaviour, se remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'. To root@101.200.159.138:/data0/htdocs/www/test/study/.git ! [remote rejected] master -> master (branch is currently checked out) ...
解決方法參照《git push錯誤 remote: error: refusing to update checked out branch: refs/heads/master解決方式》:
這是由于git默認拒絕了push操作懂衩,需要進行設置撞叨,修改當前倉庫目錄下.git/config文件后面添加如下代碼:
[receive]
denyCurrentBranch = ignore
重新git push即可
-
其實就能正常用了,在WebStorm中配置是這樣的:
VCS > Checkout from Version Control > Git
-
URL
填入ssh://git@IP:PORT/home/git/myserver.git
浊洞,OK牵敷。
我把本地目錄傳上去是個很繞的過程,先將WebStorm生成的工程備份(因為會重名)法希,myserver改名myserver2枷餐,在WebStrom當前工程目錄clone服務器上的倉庫,生成myserver目錄苫亦,然后將myserver2中的文件復制到myserver目錄中毛肋,然后執(zhí)行
add
怨咪,commit
,push
操作润匙,myserver2就可以刪掉了诗眨,這樣講需要的東西全部傳到服務器上,然后刪除myserver孕讳,因為一會兒在WebStorm中clone時會路徑?jīng)_突匠楚,最后在WebStorm中引入倉庫路徑就結(jié)束了。-
安全問題:
為安全考慮Git賬號只允許使用git-shell厂财。在passwd文件中找到git用戶芋簿,把/bin/bash直接修改成/usr/bin/git-shell 登錄root賬號,并修改git的用戶權(quán)限蟀苛。
$ su # vim /etc/passwd
這樣git用戶只能
git-shell
命令不能登錄了益咬。使用
su git
命令就會出現(xiàn)下面提示,git用戶就無法登錄到shell帜平,這樣就OK了幽告。# su git fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access.