ubuntu10.04 Git服务器搭建之无权限

        项目需要,在此搭建Git服务器对全志,RK,新岸线源码SDK进行管理。现整理文档以供交流学习。

一:搭建简单无需权限的GIT服务器



①安装相关软件:
sudo apt-get install git-core openssh-server openssh-client
sudo apt-get install git-daemon-run

②然后添加git 用户:
sudo useradd  -m -s /bin/bash -d /home/git git  (-m 自动创建用户目录 /home/git -s 指定登入后所用shell -d 用户登入后所默认进入的目录)
sudo passwd git ---为了以后su git登陆好记密码 我一样用git

③配置git-daemon(配置后重启:sudo sv restart git-daemon)
sudo vim /etc/sv/git-daemon/run

  1 #!/bin/sh
  2 exec 2>&1
  3 echo 'git-daemon starting.'
  4 #exec chpst -ugitdaemon \
  5 #  /usr/lib/git-core/git-daemon --export-all --base-path=/pub/gittrees /pub/gittrees 
  6 exec /usr/lib/git-core/git-daemon  --verbose --export-all --base-path=/pub/gittrees  --enable=receive-pack

④测试
服务器上创建git仓库
ubuntu@server:/pub/gittrees$ pwd
/pub/gittrees
ubuntu@server:/pub/gittrees$mkdir testpro
ubuntu@server:/pub/gittrees/testpro$git init
ubuntu@server:/pub/gittrees/testpro$echo "test project" > test.txt
ubuntu@server:/pub/gittrees/testpro$git add .
ubuntu@server:/pub/gittrees/testpro$git commit -a -m "init test project"

客户端远程克隆服务上git仓库
[mid@MID ~/work]$mkdir testgit
[mid@MID ~/work]$cd testgit
[mid@MID ~/work/testgit]$git clone git://192.168.10.122/testpro (服务器仓库根目录为/pub/gittrees,则服务器远程仓库路径为/pub/gittrees/testpro)
[mid@MID ~/work/testgit]$ls
testpro
[mid@MID ~/work/testgit/testpro]$ls
test.txt  
[mid@MID ~/work/testgit/testpro]$vim test2.txt
[mid@MID ~/work/testgit/testpro]$git add .
[mid@MID ~/work/testgit/testpro]$git cm -a -m "add test2.txt by mid"
[mid@MID ~/work/testgit/testpro]$git push origin master
......
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git://192.168.10.122/testpro
 ! [remote rejected] master -> master (branch is currently checked out)
error: 无法推送一些引用到 'git://192.168.10.122/testpro'

服务器端查看日志分析:
ubuntu@server:/pub/gittrees/testpro$ cat /var/log/git-daemon/current
由于git默认拒绝了push操作,需要进行设置,修改.git/config文件后面添加如下代码:
ubuntu@server:/pub/gittrees/testpro$ vim .git/config
[receive]

denyCurrentBranch = ignore

[mid@MID ~/work/testgit/testpro]$git push origin master 
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://192.168.10.122/testpro
   efe7f0f..756f1d5  master -> master


此时,服务器端并不能查看已经提交的test2.txt。必须得使用命令 git reset --hard 才能看到push后的内容
ubuntu@server:/pub/gittrees/testpro$ git reset --hard
ubuntu@server:/pub/gittrees/testpro$ ls
test2.txt  test.txt


在初始化远程仓库时最好使用 git --bare init   而不要使用: git init


ubuntu@server:/pub/gittrees$ mkdir test.git
ubuntu@server:/pub/gittrees$ cd test.git/
ubuntu@server:/pub/gittrees/test.git$ 
ubuntu@server:/pub/gittrees/test.git$ git init --bare 
Initialized empty Git repository in /pub/gittrees/test.git/
ubuntu@server:/pub/gittrees/test.git$ ls
branches  config  description  HEAD  hooks  info  objects  refs
ubuntu@server:/pub/gittrees/test.git$ 


客服端克隆远程test.git仓库:
[mid@MID ~/work/testgit]$git clone git://192.168.10.122/test.git
正克隆到 'test'...
warning: 您似乎克隆了一个空版本库。
检查连接... 完成。
[mid@MID ~/work/testgit]$ls
test  testpro
[mid@MID ~/work/testgit/test]$git cm -a -m "add test.txt by mid"
[master(根提交) 325d762] add test.txt by mid
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[mid@MID ~/work/testgit/test]$git push origin master 
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git://192.168.10.122/test.git
 * [new branch]      master -> master
[mid@MID ~/work/testgit/test]$


常见错误:
1.git fatal: The remote end hung up unexpectedl
检查/etc/sv/git-daemon/run的--base-path参数,确保和server的git目录相同
2.git fatal no matching remote head
server上如果没有任何commit,则会出现该错误
3.git 'receive-pack': service not enabled for
在/etc/sv/git-daemon/run的git-daemon命令中加入: --enable=receive-pack

你可能感兴趣的:(ubuntu10.04 Git服务器搭建之无权限)