博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
inux下Git和gitosis的安装与配置
阅读量:4041 次
发布时间:2019-05-24

本文共 3854 字,大约阅读时间需要 12 分钟。

1、增加git用户

1
2
3
4
root@stu-system:/#useradd git
root@stu-system:/#vim /etc/passwd
git:x:501:501::/home/git:/bin/bash/git-shell
root@stu-system:/sudo passwd git

2、安装git和gitosis

依次运行一下命令进行安装,其中gitosis是git的权限管理工具

1
2
3
4
5
sudo apt-get install git
sudo apt-get install openssh-server
sudo apt-get install python2.6
sudo apt-get install python-setuptools
apt-get install gitosis

3、git和gitosis配置

修改gitweb.conf

1
2
3
vim /etc/gitweb.conf
# path to git projects (<project>.git)
$projectroot = "/home/git/repositories/";

依次运行以下命令,使用ssh-keygen生成密钥对SSH_KEY和SSH_KEY.pub,并初始化gitosis-admin.git库

1
2
ssh-keygen -t rsa
sudo -H -u git gitosis-init <SSH_KEY.pub

为/home/git/repositories/gitosis-admin.git/hook/post-update增加执行权限

1
chmod +x /home/git/repositories/gitosis-admin.git/hook/post-update

4、创建git新库的脚本

写了个脚本create-repo.sh方便创建新库,并解决了最后提到的一些问题;在给脚本文件加上执行权限后,只要执行“/home/git/create-repo.sh repo-name.git”即可完成新库创建,其中repo-name替换为新库名称。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
root@stu-system:/home/git# cat create-repo.sh
 
#!/bin/bash
 
if [ $# -ne 1 ];
then
    
echo "Usage: $0 repo-name"
    
exit
fi
 
cd /home/git/repositories/
mkdir $1.git
cd $1.git
git init
git config --bool core.bare true
 
cd ..
chown -R git *
chgrp -R git *

5、使用gitosis进行git权限管理

使用git修改gitosis-admin.git库中的gitosis.conf文件,对应的用户公钥放在keydir文件夹中,公钥文件以“用户名.pub”命名,修改完后push回去就可以。提示:使用ssh key generator在Windows下生成的密钥放到gitosis-admin.git前需要在Linux用以下命令来将密钥转换为Linux下的格式。

1
ssh-keygen -if 公钥文件名>新公钥文件名

实际上push到git主库后,gitosis.conf文件会被更新到/home/git/repositories/gitosis-admin.git/gitosis.conf,这个才是真正生效的权限配置文件,如果通过git库改错了的话,可以在服务器上直接修改这个文件来解决。

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
#vim  /home/git/repositories/gitosis-admin.git/gitosis.conf
[gitosis]
gitweb=yes
 
[group admin]
members = maple root@stu-system
 
[group tech]
members = @admin
 
[group gitosis-admin]
writable = gitosis-admin
members = @admin
 
[group test]
writable = test
members = @tech
 
[group test-readonly]
readonly = test
members = maple2

6、各种问题

(1)出现’gitosis-admin’ does not appear to be a git repository,短路径无效时替换为全路径

在服务器上使用短路径会取现下面这个问题,原因是没有找到对应匹配的密钥所致,如果正确使用了ssh密钥则不会出现这个问题,这里就不去折腾了

1
2
3
4
5
root@stu-system:/home/git/repositories# git clone git@127.0.0.1:gitosis-admin.git
Cloning into gitosis-admin...
git@127.0.0.1's password:
fatal: 'gitosis-admin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

使用以下命令解决,只不过每次push和pull的时候都需要输入一遍git用户的密码,略为繁琐

1
git clone git@127.0.0.1:/home/git/repositories/gitosis-admin.git

(2)出现Unable to create temporary file: Permission denied

在Windows上使用TortoiseGit执行Push时出现以下错误

01
02
03
04
05
06
07
08
09
10
git.exe push --force --progress  "origin" master:master
 
Counting objects: 189, done.
Compressing objects: 100% (187/187)
Writing objects:   7% (14/189)
fatal: Unable to create temporary file: Permission denied
fatal: sha1 file '<stdout>' write error: Invalid argument
error: failed to push some refs to 'git@10.73.4.46:channelv.git'
 
git did not exit cleanly (exit code 1)

原来是服务器上是用root账户建立的库目录,导致git账户无权写入,方法就是修改文件夹的所属用户和所属用户组

1
2
root@stu-system:/home/git/repositories# chown -R git *
root@stu-system:/home/git/repositories# chgrp -R git *

(3)出现failed to push some refs to ‘git@10.73.4.46:channelv.git’

在Windows上使用TortoiseGit执行Push时出现以下错误

01
02
03
04
05
06
07
08
09
10
11
12
git.exe push --progress  "origin" master:master
 
Counting objects: 189, done.
Compressing objects: 100% (158/158)
Writing objects: 100% (189/189), 1016.00 KiB | 997 KiB/s
Writing objects: 100% (189/189), 1.12 MiB | 997 KiB/s, done.
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git@10.73.4.46:channelv.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@10.73.4.46:channelv.git'
 
git did not exit cleanly (exit code 1)

在服务器对应的库目录下执行以下命令增加配置即可

1
git config --bool core.bare true

转载地址:http://tjadi.baihongyu.com/

你可能感兴趣的文章
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
关于进制转换的具体实现代码
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>