如何搭建git服务器 windows,第1张

GitLab是由Ruby语言开发的基于Linux的Git服务器,是我见过的最强大的Git服务器。发现它之后,立即决定将Git服务器换成GitLab。但安装好GitLab之后面临一个问题,如何将服务器上的git项目直接导入到GitLab,之前的Git服务器是由是git+apache搭建的(详见在Linux上用Apache搭建Git服务器)。在网上发现了这篇文档——ImportbarerepositoriesintoyourGitLabinstance,并按之进行了操作。1)设置存放代码库的主目录vi/etc/gitlab/gitlabrb比如这里设置为:git_data_dir"/gitlab/repos"2)访问刚搭建的GitLab站点,创建一个group,比如cnblogs。这时会在/gitlab/repos下创建/gitlab/repos/repositories/cnblogs文件夹。然后在/gitlab/repos/repositories/创建一个文件夹,比如cnblogs3)将现有的所有git项目文件复制到这个文件夹cp-r/data/git//gitlab/repos/repositories/cnblogs4)修改一下复制过来的文件夹的所有者:chown-Rgit:git/gitlab/repos/repositories/cnblogs5)运行GitLab导入命令cd/var/opt/gitlabgitlab-rakegitlab:import:repos等了一段时间之后,显示done,却一个项目也没导入进来。经研究发现,在导入时,GitLab只认文件夹名以git结尾的项目。于是,将要导入的项目文件夹名称加上git后缀,再次进行导入。结果显示导入成功,比如:Processingcnblogs/CNBlogsJobgitCreatedCNBlogsJob(cnblogs/CNBlogsJobgit)Done!可以是GitLab站点上却看不到已导入的项目。多次努力,也没能解决这个问题。后来,实在没法,改为手动导入,导入方法如下:1)在GitLab站点上创建与要导入的项目同名的项目。2)进入刚创建的项目文件夹cd/gitlab/repos/repositories/cnblogs/项目名称git3)删除该文件下的所有文件rm-rf4)将要导入的项目文件夹下的所有文件复制过来cp-r/data/git/CNBlogsJob//gitlab/repos/repositories/cnblogs/CNBlogsJobgit就这样将项目一个一个地导入进来。5)导入完成后,修改一下导入的所有项目的文件所有者chown-Rgit:git/gitlab/repos/repositories/cnblogs如果不修改所有者,客户端无法进行gitpush。就这样手动地完成了现有Git项目的导入。备注:操作系统是CentOS62,GitLab版本是784。

1 下面我们以ubuntu server为例,介绍一下如何使用Git来管理项目代码,并且部署代码的web站点目录。

2 1 准备工作:

1台拥有SSH权限的服务器 (LNMP,git,ssh安装配置)

一台装有git客户端的PC (本文实际在windows下的msysgit操作)

3 LNMP,git,ssh安装配置

# 安装LNMP$ wget -c http://softvpsernet/lnmp/lnmp10-fulltargz && tar zxvf lnmp10-fulltargz && cd lnmp10-full && /ubuntush# 安装Git ssh$ sudo apt-get install git git-core ssh

windows下安装 msysgit

4 2在你的web服务器上建立git裸仓库、编写hooks、导入用户公钥、设置目录权限

在服务器上建立一个用户,这里命名为git,修改git的密码,并禁用shell登录

$ sudo useradd git# 修改git的密码$ passwd git# 禁用shell登录$ sudo vi /etc/passwd修改git:x:1001:1001:,,,:/home/git:/bin/bash为git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

初始化Git仓库,建立git 裸仓库,设置hook脚本(web目录需要git用户有权限,否则git的hook脚本将没有办法执行操作)

$ cd /opt $ mkdir git$ cd git$ sudo git init --bare demositegit# 文件夹权限$ sudo chown -R git:git demositegit$ cd /demositegit/hooks$ sudo vi post-receive写入以下内容#!/bin/shGIT_WORK_TREE=/home/wwwroot/wwwdemositecom git checkout -f# 写入权限$ chmod +x post-receive# 建立web目录,如果目录不存在,git不会创建目录的$ mkdir /home/wwwroot/wwwdemositecom -p# web目录的文件夹权限$ sudo chown -R git:git /home/wwwroot/wwwdemositecom

创建证书登录

收集所有需要登录的用户的公钥,就是他们自己的id_rsapub文件,把所有公钥导入到/home/git/ssh/authorized_keys文件里,一行一个。

客户端用户证书生成请参考下文。

5 3在git客户端生成公钥、导入服务器、coding 、add、commit、push

生成公钥,导入服务器

$ ssh-keygen -t rsa -C "your_email@youremailcom"一路回车或者设置密码windows下回提示在哪里生成了密钥windows8下提示在C:\Users\username\ssh 下,linux的话会在~/ssh下进入目录,打开id_rsapub,复制里面的key,添加到上文提到的web服务器的/home/git/ssh/authorized_keys里。

clone coding add commit push

$ git clone git@serverip:/opt/git/demositegitCloning into 'demosite'warning: You appear to have cloned an empty repository$ cat 'hello world!' > indexhtml$ git add indexhtml$ git commit -m'first commit'$ git remote add origin ssh://git@serverip:/opt/git/demositegit$ git push origin master

这里使用公钥来做权限验证,运行git里的hook的脚本权限为git用户的权限,当然你也可以使用自己在server上的用户来进行git操作,前面对于git用户的操作和客户端公钥的生成则可以省略,在git上脚本的执行权限则是你登录用户的权限,相应的origin的设置应该为:

$ git remote add origin ssh://yourusername@serverip:/opt/git/demositegit

这时候每次push将需要输入密码验证。

忽略某些文件

你可以在你的顶层工作目录中添加一个叫"gitignore"的文件,来告诉Git系统要忽略 掉哪些文件,下面是文件内容的示例:

使"git add " 和"git commit -a" 变得实际可用。

# 以'#' 开始的行,被视为注释# 忽略掉所有文件名是 footxt 的文件footxt# 忽略所有生成的 html 文件,html# foohtml是手工维护的,所以例外!foohtml# 忽略所有o 和 a文件[oa]#Eclipsesettings/buildpathproject

1、打开控制面板->管理工具->服务项目,禁用Windows防火墙服务。

2、打开控制面板->管理工具,双击“路由和远程访问”。然后右键点击服务器图标,再点击“配置并启用路由和远程访问”。

3、在“路由和远程服务器安装向导”中,选择“自定义配置”。

4、勾选“***访问”和“NAT和基本防火墙”。

5、点击“完成”。系统提示“要开始服务吗?”,选择“是”。

6、接下来开始配置路由和远程访问,我们先点击本地服务旁边的+按钮,把左侧菜单展开,再点击IP路由选择旁边的+按钮。

7、下面配置静态IP地址。右键点击本地服务,点击属性,再点击IP选项卡。

8、点选“静态地址池”,点击“添加”。输入一个起始IP地址和结束IP地址范围,推荐输入1921681100到1921681254这个范围,因为192168段属于本地保留IP段。最后点击“确定”。

9、右键点击“静态路由”,再点击“新建静态路由”。

10、目标和网络掩码都输入0,网关请和TCP/IP属性中的默认网关保持一致。

11、删除“DHCP中继代理程序”中的“内部”项目。

12、然后右键点击“DHCP中继代理程序”,再点击“新增接口”。

13、选择“本地连接”,再点击“确定”,然后在弹出的对话框中点击“确定”。

14、右键点击“NAT/基本防火墙”,再点击“新增接口”,然后选择“本地连接”,点击“确定”。

15、在弹出的对话框中先点选“公共接口连接到Internet”,再勾选“在此接口上启用NAT”,最后点击“确定”。

首先我们分别在Git服务器和客户机中安装Git服务程序(刚刚实验安装过就不用安装了):

[root@linuxprobe ~]# yum install git

Loaded plugins: langpacks, product-id, subscription-manager

This system is not registered to Red Hat Subscription Management You can use subscription-manager to register

Package git-1831-4el7x86_64 already installed and latest version

Nothing to do

然后创建Git版本仓库,一般规范的方式要以git为后缀:

[root@linuxprobe ~]# mkdir linuxprobegit

修改Git版本仓库的所有者与所有组:

[root@linuxprobe ~]# chown -Rf git:git linuxprobegit/

初始化Git版本仓库:

[root@linuxprobe ~]# cd linuxprobegit/

[root@linuxprobe linuxprobegit]# git --bare init

Initialized empty Git repository in /root/linuxprobegit/

其实此时你的Git服务器就已经部署好了,但用户还不能向你推送数据,也不能克隆你的Git版本仓库,因为我们要在服务器上开放至少一种支持Git的协议,比如HTTP/HTTPS/SSH等,现在用的最多的就是HTTPS和SSH,我们切换至Git客户机来生成SSH密钥:

[root@linuxprobe ~]# ssh-keygen

Generating public/private rsa key pair

Enter file in which to save the key (/root/ssh/id_rsa):

Created directory '/root/ssh'

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/ssh/id_rsa

Your public key has been saved in /root/ssh/id_rsapub

The key fingerprint is:

65:4a:53:0d:4f:ee:49:4f:94:24:82:16:7a:dd:1f:28 root@linuxprobecom

The key's randomart image is:

+--[ RSA 2048]----+

| o+ooo |

| oo + |

| + E o |

| o = + = |

| S o o |

| |

| |

| |

| |

+-----------------+

将客户机的公钥传递给Git服务器:

[root@linuxprobe ~]# ssh-copy-id 1921681010

root@1921681010's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '1921681010'"

and check to make sure that only the key(s) you wanted were added

此时就已经可以从Git服务器中克隆版本仓库了(此时目录内没有文件是正常的):

[root@linuxprobe ~]# git clone root@1921681010:/root/linuxprobegit

Cloning into 'linuxprobe'

warning: You appear to have cloned an empty repository

[root@linuxprobe ~]# cd linuxprobe

[root@linuxprobe linuxprobe]#

初始化下Git工作环境:

[root@linuxprobe ~]# git config --global username "Liu Chuan"

[root@linuxprobe ~]# git config --global useremail "root@linuxprobecom"

[root@linuxprobe ~]# git config --global coreeditor vim

向Git版本仓库中提交一个新文件:

[root@linuxprobe linuxprobe]# echo "I successfully cloned the Git repository" > readmetxt

[root@linuxprobe linuxprobe]# git add readmetxt

[root@linuxprobe linuxprobe]# git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use "git rm --cached " to unstage)

#

# new file: readmetxt

#

[root@linuxprobe linuxprobe]# git commit -m "Clone the Git repository"

[master (root-commit) c3961c9] Clone the Git repository

Committer: root

1 file changed, 1 insertion(+)

create mode 100644 readmetxt

[root@linuxprobe linuxprobe]# git status

# On branch master

nothing to commit, working directory clean

但是这次的操作还是只将文件提交到了本地的Git版本仓库,并没有推送到远程Git服务器,所以我们来定义下远程的Git服务器吧:

[root@linuxprobe linuxprobe]# git remote add server root@1921681010:/root/linuxprobegit

将文件提交到远程Git服务器吧:

[root@linuxprobe linuxprobe]# git push -u server master

Counting objects: 3, done

Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done

Total 3 (delta 0), reused 0 (delta 0)

To root@1921681010:/root/linuxprobegit

[new branch] master -> master

Branch master set up to track remote branch master from server

为了验证真的是推送到了远程的Git服务,你可以换个目录再克隆一份版本仓库(虽然在工作中毫无意义):

[root@linuxprobe linuxprobe]# cd /Desktop

[root@linuxprobe Desktop]# git clone root@1921681010:/root/linuxprobegit

Cloning into 'linuxprobe'

remote: Counting objects: 3, done

remote: Total 3 (delta 0), reused 0 (delta 0)

Receiving objects: 100% (3/3), done

[root@linuxprobe Desktop]# cd linuxprobe/

[root@linuxprobe linuxprobe]# cat readmetxt

I successfully cloned the Git repository

http://wwwlinuxprobecom/chapter-21html#214_Git这篇是详细介绍Git的,中间有一部分是怎么去搭建,你可以看下

PHP 54

Nginx 180

GIT version 294 (#git --version)

我的码云 -> 点击相应项目A -> 管理-> WebHooks 设置 -> 添加;具体填写内容如下图所示

不多说,直接帖码。修改好自定义选项,上传服务器后到gitee后台点击测试验证是否通过。

如果没有给777权限,pull时会报各种权限错误 (除非你对用户及用户组权限设置的很到位并可以解决站点www用户组权限的冲突) ,当然也可以将项目A中的git目录权限都设置为777,如下命令是我当前使用的具体操作。

注意: 是 git 目录

因为PHP脚本涉及到shell命令执行所有需要php开启exec命令权限

phpini:

执行push的会触发WebHooks,同时推送消息给项目A中WebHooks 设置的请求地址,最后完成服务器的git pull。

如果还没有本地初始化项目,可以根据 码云gitee本地初始化项目基本设置 这篇进行操作。

至此完成码云gitee自动部署。

如在操作过程中遇到问题,欢迎留言。

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » 如何搭建git服务器 windows

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情