git局域网同步代码(守护进程)
场景
- 没办法连接到代码服务器的情况,又需要协同工作的情况下
- 临时使用
说明
对于快速且无需授权的 Git 数据访问,这是一个理想之选。请注意,因为其不包含授权服务,任何通过该协议管理的内容将在其网络上公开。
配置过程
参数说明
A 机器开启守护进程,使用 git 协议公开当前目录.下的仓库
git daemon --reuseaddr --base-path=. --verbose --export-all --informative-errors --enable=receive-pack
--reuseaddr允许服务器直接重启而不用等待连接超时--base-path设置仓库文件夹路径--verbose消息提示--export-all将--base-path路径下的所有项目暴露--informative-errors将详细错误告诉客户端--enable=receive-pack允许客户端推送到服务器,默认只能拉取
B 机器克隆 A 机器的仓库:
git clone git://$hostip_a/respostory_name
其中 $hostip_a 为 A 机器的 IP, respostory_name 为 --base-path 指定目录下的仓库名称。
示例
在A机器的f:/git_deamon_test文件夹下,有个git仓库test1
A机器 MINGW64 /f/git_daemon_test
$ ls
test1/
A机器 MINGW64 /f/git_daemon_test
$ ls test1/ -a
./ ../ .git/ 1.txt
执行命令启动git守护线程
A机器 MINGW64 /f/git_daemon_test
$ git daemon --reuseaddr --base-path=. --verbose --export-all --informative-errors --enable=receive-pack
[1428] Ready to rumble
在B机器拉取test1仓库
git clone git://$hostip_a/test1
对仓库进行修改之后,使用在服务器添加一个新的分支方式来推送代码
B机器 MINGW64 ~/Desktop/test1 (master)
$ git push origin master:new
error: pack-objects died of signal 2
发现迟没有回应,此时查看A机器的消息,这时候--verbose的作用体现了
[11984] unable to set SO_KEEPALIVE on socket: No such file or directory
[11984] Extended attribute "host": 192.168.22.23
[11984] Request receive-pack for '/test1'```
需要在B机器执行以下命令即可
B机器 MINGW64 ~/Desktop/test1 (master)
$ git config --global sendpack.sideband false
B机器 MINGW64 ~/Desktop/test1 (master)
$ git push origin master:new
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To git://192.168.22.23/test1
* [new branch] master -> new
无法直接推送到A机器master分支
B机器 MINGW64 ~/Desktop/test1 (master)
$ git push
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To git://192.168.22.23/test1
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git://192.168.22.23/test1'
在A机器中可以看到
[14588] You can set the 'receive.denyCurrentBranch' configuration variable
[14588] to 'ignore' or 'warn' in the remote repository to allow pushing into
[14588] its current branch; however, this is not recommended unless you
[14588] arranged to update its work tree to match what you pushed in some
[14588] other way.
[14588] To squelch this message and still keep the default behaviour, set
[14588] 'receive.denyCurrentBranch' configuration variable to 'refuse'.
[13996] [14588] Disconnected
需要设置即可
A机器 MINGW64 /f/git_daemon_test
git config receive.denyCurrentBranch ignore
