Linux的centos搭建git服务器

1.安装git用yum install git(服务器)

 

1.1.添加git用户和密码(服务器)

adduser git

passwd git 123

 

1.2.禁止shell登陆如果不知道git在哪里用:which git-shell 查找出来填写正确路径(服务器)

编辑/etc/passwd

git:x:1001:1001:,,,:/home/git:/bin/bash

改为:

git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

 

2.创建裸仓库(服务器)

[root@localhost]$ mkdir /home/workspace
[root@localhost]$ cd /home/workspace
[root@localhost]$ git init --bare wwwroot.git

 5.0.1.给权限(服务器)

chown -R git:git wwwroot.git

5.0.2生成用户秘钥( 服务器端id_rsa.pub公钥, id_rsa私钥,记住生成的目录位置)

ssh-keygen -t rsa

 

5.1你本地安装好git之后(客户端)

$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"

要配置你本地git的名称和邮箱

 5.2生成秘钥(客户端一路按下去不要加密码,直接回车即可)

ssh-keygen

5.3把生成的秘钥送给服务器(客户端)

客户端的id_rsa.pub秘钥内容到服务器端用户目录.ssh目录下面authorized_keys里面的,如果没有下面目录,直接新建创建出来(注意:目录是在git服务器上面建立,然后把本地id_rsa.pub内容添加到服务器的authorized_keys文件中)(home目录设置权限755、authorized_keys文件权限为644)

home/git/.ssh/authorized_keys

 

6.用clone远程下载git项目(客户端)

git clone git@115.28.38.789:/home/workspacewwwroot.git

IP和路径一定要填写正确

7.客户端可以操作git相关操作了

git pull origin master (更新之前代码同步到本地)

git add *

git commit -m 'add'

git remote add origin git@115.28.38.789:/home/workspacewwwroot.git   (只在第一次关联的时候使用,第二次不需要在使用,直接push就可以了

git push -u origin master (第一次的时候使用)

git push origin master

项目每次修改或添加就可以这些命令完成

8.进行git和web目录关联,同步浏览网站

设置钩子,这样每次运行git就可以同步提交浏览网站了(注意:使用钩子你要把你同步到的web目录chown更改为git用户使用权,钩子才能正确生效)

[root@localhost]$ cd /home/workspace/wwwroot.git/hooks
[root@localhost]$ cp post-receive.sample post-receive [root@localhost]$ cat
> post-receive <<EOF >#!/bin/bash >git --work-tree=/www/web/duoduo/public_html checkout -f >EOF [root@localhost]$ chown git:git post-receive
[root@localhost]$ chmod +x post-receive

使用钩子你要把你同步到的public_html目录chown更改为git用户使用权,钩子才能正确生效

钩子的第二种方法:

#!/bin/sh
export LANG=zh_CN.UTF-8
cd  /home/website/wwwroot/
unset GIT_DIR
git pull origin master

 注意你提交到哪个文件夹把对应文件夹设置为git用户

git提交代码每次输入密码很麻烦,下面文章可以解决

http://www.chenyudong.com/archives/tortoisegit-use-key-authentication.html

 

分布式服务器,每一个电脑都可以作为git服务器,每个电脑都是一个节点!祝君好运!

 

posted @ 2015-04-15 22:56  feimengv  阅读(1320)  评论(0编辑  收藏  举报