如果只是想在局域网内快速搭建 git 仓库并提供访问的话,可以通过以下步骤实现。
假设搭建 git 仓库的主机 ip 为:10.20.42.87
1)初始化本地 git 仓库
$ cd /home/zhoumin/repos/
$ mkdir ./demo
$ cd ./demo
$ git init
初始化空的 Git 版本库于 /home/zhoumin/repos/demo/.git/
2) 从别处clone
在 10.20.42.119 上操作
$ cd /root/
$ git clone zhoumin@10.20.42.87:/home/zhoumin/repos/demo
正克隆到 'demo'...
zhoumin@10.20.42.87's password:
warning: 您似乎克隆了一个空版本库。
3) 修改并提交代码
$ cd demo
$ ll
总用量 0
$ touch README
$ git add README
$ git commit -m"add README"
[master(根提交) 6b74435] add README
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
$ git push origin master
zhoumin@10.20.42.87's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
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 to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
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 some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To zhoumin@10.20.42.87:/home/zhoumin/repos/demo
! [remote rejected] master -> master (branch is currently checked out)
可见,直接push会报错,此时按照上述提示对源仓库进行简单配置即可。
4)配置源仓库
在 10.20.42.87 上操作
$ cd /home/zhoumin/repos/demo/
$ git config --local receive.denyCurrentBranch ignore
5)再次提交代码
在 10.20.42.119 上操作
$ cd /root/demo
$ git push origin master
zhoumin@10.20.42.87's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To zhoumin@10.20.42.87:/home/zhoumin/repos/demo
* [new branch] master -> master
至此,push 成功,可正常使用。