本文用到了node-git-server
1、檢測本地git版本
該包的使用需要機器上本來就安裝git,且git的版本大於等於2.7:
1 ╭─root@lt /home/workspace 2 ╰─# git --version 129 ↵ 3 git version 2.7.4
2、利用npm安裝包
1 ╭─root@lt /home/workspace 2 ╰─# npm install node-git-server 3 /home/workspace 4 └─┬ node-git-server@0.3.0 5 ├─┬ http-duplex@0.0.2 6 │ ├── duplex-pipe@0.0.2 7 │ └── inherits@1.0.2 8 └── through@2.3.8 9 10 npm WARN enoent ENOENT: no such file or directory, open '/home/workspace/package.json' 11 npm WARN workspace No description 12 npm WARN workspace No repository field. 13 npm WARN workspace No README data 14 npm WARN workspace No license field.
有警告可以先忽略~
3、編寫example
cd ./node_modules/node-git-server/example/
編輯index.js
1 const Server = require('node-git-server'); 2 const repos = new Server('/tmp/repos'); 3 const port = process.env.PORT || 80; 4 5 repos.on('push', (push) => { 6 console.log('push ' + push.repo + '/' + push.commit 7 + ' (' + push.branch + ')' 8 ); 9 push.accept(); 10 }); 11 12 repos.on('fetch', (fetch) => { 13 console.log('fetch ' + fetch.commit); 14 fetch.accept(); 15 }); 16 17 repos.listen(port, () => { 18 console.log(`node-git-server running at http://localhost:${port}`) 19 });
4、運行
1 ╭─root@lt /home/workspace/node_modules/node-git-server/example 2 ╰─# node index.js 3 node-git-server running at http://localhost:80
5、測試git服務器
由於我的git服務器是在aliyun上跑的,並且綁定了www.beautifulzzzz.com,所以我先在本地新建一個git倉庫,並將其推送同步到雲端,然后再clone下來來做測試:
可見將本地git倉庫同步到雲端和普通的git服務器沒有區別,簡直太簡單方便了!!!
同樣git clone也比較簡單!
6、雲端git倉庫的位置
在index.js中我們指定git倉庫存放在:const repos = new Server('/tmp/repos');目錄下
登錄遠程服務器可以發現在/tmp/repos/目錄下存在我們同步的git倉庫:(其中beep.git是之前push的一個)
1 ╭─root@lt /tmp/repos 2 ╰─# ls 3 beautifulzzzz.git beep.git
:: 如果您覺得不錯,請推薦給更多人,幫助他們更快地解決實際問題中的坑~
@beautifulzzzz 智能硬件、物聯網,熱愛技術,關注產品 博客:http://blog.beautifulzzzz.com 園友交流群:414948975