在 windows 下配置 git 公共库

参考:Git server on Windows

Repositories

Create a directory that contains your Git repositories, for example: D:/dev/repo/git. To get you started, go to the directory and create an empty Git repository.

1 cd D:/dev/repo/git
2 git init --bare Test.git

Apache configuration

In this tutorial I use Apache 2.2.19. You need to setup git-http-backend.exe in order to serve Git through Apache. First copy ..\msysgit\mingw\bin\libiconv-2.dll to ..\msysgit\libexec\git-core or else you will get a 500 error from Apache. To test if your setup works run ..\msysgit\libexec\git-core\git-http-backend.exe

Add the following to your Apache conf\httpd.conf:

01 SetEnv GIT_PROJECT_ROOT D:/dev/repo/git
02 SetEnv GIT_HTTP_EXPORT_ALL
03 ScriptAliasMatch \
04         "(?x)^/(.*/(HEAD | \
05                         info/refs | \
06                         objects/(info/[Apache Git server on Windows^/]+ | \
07                                  [0-9a-f]{2}/[0-9a-f]{38} | \
08                                  pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
09                         git-(upload|receive)-pack))$" \
10                         "D:/dev/msysgit/libexec/git-core/git-http-backend.exe/$1"

I also made the Apache DocumentRoot point to my Git repos:

1 DocumentRoot "D:/dev/repo/git"
2  
3 <Directory "D:/dev/repo/git">
4     Options Indexes FollowSymLinks
5     AllowOverride All
6     Order allow,deny
7     Allow from all
8 </Directory>

The result:

Using the repo

You can create a local copy of this git repo using the clone command.

1 git clone http://localhost/Test.git

以下摘自:Tommy's Blog 

如果你的开发团队采用的是 集成管理员工作流,则需要每一个开发人员都有一个公共的代码库供其他人访问。在 linux 上设置很简单,但是如果部分开发人员必须使用 windows 环境, 可以按照下面的方式来配置。

安装 Git
~~~~~~~
从 googlecode 下载 msysgit 1.7.9,一路默认安装。如果你使用的是 eclipse 的 git 插件(如 egit),也需要安装 msysgit。安装完成后, 最好立刻配置 .gitconfig 和 .ssh,方法网上介绍很多,这里不啰嗦了。
装好 msysgit以后,进入你的 git 安装目录 C:\Program Files\Git\libexec\git-core 下找到一个 git-http-backend.exe 文件,双击运行一下,如果提示缺少一个 libiconv-2.dll 文件,需要你手工将这个文件从 C:\Program Files\Git\bin 目录复制到 C:\Program Files\Git\libexec\git-core 目录中。

安装 Apache
~~~~~~~
从 apache 的官方网站下载最新的 msi 安装包 httpd 2.2.22 no_ssl msi,然后一路默认安装。在配置服务器域属性的时候使用 localhost ,如下

Network Domain: localhost Server Name: localhost Admin Email: 你的邮件地址

访问 http://localhost, 成功的话会显示 It works!

创建一个 git 仓库目录
~~~~~~~
如果你已经通过 eclipse 的 egit 插件 clone 好了项目,那么默认会有一个 $HOME/git 目录被创建,下面的内容可以跳过。
否则你需要从装好的 git 菜单运行 Git Bash

$ mkdir git $ cd git $ git init --bare sandbox.git

也会在 $HOME 下创建一个 git 目录,并初始化了一个空的 git 项目 sandbox.git

修改 Apache 配置
~~~~~~~
这步稍微复杂一点, 从你的 apache 安装目录找到 httpd.conf 文件,默认是安装在 C:\Program Files\Apache Software Foundation\Apache2.2\conf 目录下,编辑这个文件,在文件的末尾加上下面的内容:

SetEnv GIT_PROJECT_ROOT C:/Users/Tommy/git SetEnv GIT_HTTP_EXPORT_ALL ScriptAliasMatch \         "(?x)^/git/(.*/(HEAD | \                         info/refs | \                         objects/(info/[^/]+ | \                                  [0-9a-f]{2}/[0-9a-f]{38} | \                                  pack/pack-[0-9a-f]{40}\.(pack|idx)) | \                         git-(upload|receive)-pack))$" \                         "C:/Program Files/Git/libexec/git-core/git-http-backend.exe/$1"   <Directory "C:/Program Files/Git/libexec/git-core/">   Allow From All </Directory>

第一行的 GIT_PROJECT_ROOT 告诉 git 你的仓库位置, 第二行告诉 git 在这个目录下的所有项目应该通过 http 发布 (默认情况下, git 只发布那些含有 git-daemon-export-ok 文件的仓库),剩下的 ScriptAliasMatch 配置告诉 apache , 如果开头是 git 的 url 请求交给 git-http-backend.exe 来处理。最后是设置目录的访问权限。别忘了把路径改为你自己的路径。
httpd.conf 文件默认只有管理员可以修改, 如果当前用户不是管理员, 需要修改这个文件的权限。

改好后,重起 apache,进入 Git Bash, 随便进入一个目录(但不要是 C:/Users/Tommy/git),测试一下通过 http 进行 clone

$ cd $ git clone http://localhost/git/sandbox.git

到这里,通过http协议,公共仓库可以被clone了,但不能push,只能读不能写,要实现push操作,参考以下内容:

Setting up and using Git on Windows and Ubuntu

Append the following to theApache config file to enable read/write access to the folder

Source code 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 
<Location /git> 
  AuthType Basic 
  AuthName "Git" 
  AuthUserFile /etc/apache2/passwd.git 
  Require valid-user 
</Location>

What the above do is make requests to /git only accessible to valid users and tell valid users are listed on the file /etc/apache2/passwd.git.

Make sure the file ‘/etc/apache2/passwd.git’ is accessible by Apache (user ‘www-data’).

Use the following command to create the user list at /etc/apache2/passwd.git and add the user ‘username’ with a password to it.

Source code 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 在 windows 下配置 git 公共库 - luowei505050@126 - 维唯为为_126博客 
htpasswd -c /etc/apache2/passwd.git username

If the file already exists then run httpasswd without -c option.

即在httpd.conf文件中添加:

<Location /> AuthType Basic AuthName "Git" AuthUserFile d:/apache2/passwd.git Require valid-user </Location>

并用htpasswd创建passwd.git文件:

htpasswd -c d:/apache2/passwd.git luowei

向此文件中添加有访问仓库权限的用户,如果此文件已存在省略 -c 参数即可....

到这里为止,git 的公共仓库算是配置完成了,下面简单说一下 gitweb 的配置,可配可不配,个人觉得用处不大。


gitweb 配置
~~~~~~~
msysgit 安装包里自带 perl ,但是缺少 cgi.pm 模块, 需要从 CPAN 下载
解压缩后, 将 lib 下的文件复制到 C:\Program Files\Git\lib\perl5\5.8.8 目录下。

继续修改 apache 的 httpd.conf 文件,在文件最后再增加下面的内容:

Alias /gitweb "C:/Program Files/Git/share/gitweb"   <Directory "C:/Program Files/Git/share/gitweb/">   AddHandler cgi-script .cgi   <Files ~ "\.cgi$">     Options +ExecCGI   </Files>   AllowOverride None   Order allow,deny   Allow from all   DirectoryIndex gitweb.cgi </Directory>

修改 gitweb.cgi 第一行, 使用 msysgit 中自带的 perl

#!C:/Program Files/Git/bin/perl

修改 git 命令的位置

our $GIT = "C:/Program Files/Git/bin/git";

修改我们 git 仓库的位置, 注意, 如果你的仓库在 c:\Users\Tommy\git 目录, 应该按照下面的写法

our $projectroot = "/c/Users/Tommy/git";

还需要在 C:/Program Files/Git/ 目录下创建一个 tmp 目录。
重起 apache ,开浏览器,访问 http://localhost/gitweb

不知为什么,通过 msysgit 里自带的 perl 在 apache 中运行 gitweb 速度很慢,但我直接通过下面的命令执行 gitweb.cgi 却很快

c:\Program Files\Git\bin>perl.exe "c:\Program Files\Git\share\gitweb\gitweb.cgi"

所以这里不建议配置 gitweb,只供参考。

参考文档
~~~~~~~
[1] http://www.jeremyskinner.co.uk/2010/07/31/hosting-a-git-server-under-apache-on-windows/
[2] http://blog.csdn.net/dbzhang800/article/details/6901460
[3] https://git.wiki.kernel.org/articles/g/i/t/MSysGit~GitWeb_6d0c.html#Install_GitWeb_CGI_on_Windows
[4] http://jmodalwindow.java.net/manual/dpl/html/version.control.system.html

 

Create a bare GIT repository

A small rant: git is unable to create a normal bare repository by itself. Stupid git indeed.

To be precise, it is not possible to clone empty repositories. So an empty repository is a useless repository. Indeed, you normally create an empty repository and immediately fill it:

git init git add . 

However, git add is not possible when you create a bare repository:

git --bare init git add . 

gives an error "fatal: This operation must be run in a work tree".

You can't check it out either:

Initialized empty Git repository in /home/user/myrepos/.git/ fatal: http://repository.example.org/projects/myrepos.git/info/refs not found: did you run git update-server-info on the server?  git --bare init git update-server-info # this creates the info/refs file chown -R <user>:<group> . # make sure others can update the repository 

The solution is to create another repository elsewhere, add a file in that repository and, push it to the bare repository.

mkdir temp; cd temp git init touch .gitignore git add .gitignore git commit -m "Initial commit" git push (url or path of bare repository) master cd ..; rm -rf temp
参考文章:

8 ways to share your git repository

原文地址:https://www.cnblogs.com/luowei010101/p/2815188.html