git下添加新项目及用户

我们在上一节(http://blog.haohtml.com/archives/12658)配置成功了git-server。这里我们新创建一个git项目并允许开者使用.

1. 生成密钥与公钥

[dev1@bogon ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dev1/.ssh/id_rsa):
Created directory ‘/home/dev1/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dev1/.ssh/id_rsa.
Your public key has been saved in /home/dev1/.ssh/id_rsa.pub.
The key fingerprint is:
99:4c:e4:00:56:a9:a2:e2:68:20:8c:60:90:2e:b9:b8 dev1@bogon
The key’s randomart image is:
+–[ RSA 2048]—-+
| . ooo.. |
|o . .+ |
|o. . o |
|+o. . o o |
|Bo . S |
|O. |
|*. |
|Eo |
|. |
+—————–+

[dev1@bogon ~]$ ls .ssh
id_rsa id_rsa.pub

2.将公钥上传到git server的gitosis-admin/keydir/ 目录里,文件名就是是用户名(这里文件名必须与客户端的用户名一致,否则无法使用.不知道同名的情况下如何处理?)
注意:我这里是在同一台机器上测试的,不同的机器可以用scp命令来实现上传文件,主要pub文件上传到keydir目录下才可以(scp ~/.ssh/id_rsa.pub root@192.168.0.77:/tmp/sxf.pub) 

cd /root/gitosis-admin/keydir/
cp /home/dev1/.ssh/id_rsa.pub ./dev1.pub

3.创建新项目,将dev1用户添加进去

cd /home/gitosis-admin/

vi gitosis.conf
[group project2]
writable = project2
members = dev1

4.保存修改,并将修改提交到git server上

git add .
git commit -a -m ‘add new user dev1’
git push

5.在客户端测试(用户为dev1)
创建项目project2目录(一定要和项目名称一样)

mkdir /data/project2
cd /data/project2
git init
touch test.txt
git add .

git config –global user.email “you@example.com”
git config –global user.name “Your Name”

git commit -a -m ‘init project2’
git remote add origin git@xxx:project2.git
git push origin master

git pull

可以看到已经提交成功了!

如果客户端为Linux的话,在执行git pull的时候,出现

 Permissions 0644 for ‘/root/.ssh/id_rsa’ are too open.

等错误显示了,只要把权限降到0600就ok了.这种情况下一般是属于将另其它地方的公钥直接放在其它客户端用的原因。

输入命令

chmod 0600 ~/.ssh/id_rsa

然后再执行相应的命令即可.这样就可以密钥登陆了.

相关教程:
git remote 常用命令总结