Git协议 服务器的搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yonggeit/article/details/72190717

Git 服务器的搭建
远程仓库通常只是一个纯仓库(bare repository)—一个没有当前工作目录的仓库。因为该仓库只是一个合作媒介,所以不需要从一个处于已从硬盘上检出状态的快照;仓库里仅仅是git的数据。更简单的说,纯仓库是你的项目里的.git内容。
开始架设git服务器的时候,需要把一个现存的仓库导出为新的纯仓库—不包含当前工作目录的仓库。方法很简单。把一个仓库克隆为纯仓库,可以使用clone命令的–bare选项。纯仓库的目录名以.git 结尾。
Git服务器搭建根据自己的需求选择不同的协议
Git支持http:// git:// ssh:// https:// file:// (本地)
ssh://[user@]host.xz[:port]/path/to/repo.git/
git://host.xz[:port]/path/to/repo.git/
http[s]://host.xz[:port]/path/to/repo.git/
ftp[s]://host.xz[:port]/path/to/repo.git/
rsync://host.xz/path/to/repo.git/

1.1 Git协议的服务器搭建

1.1.1 安装git

安装git软件,使用yum安装的方式

yum install git-* -y 安装git所有的包
git-all
git-cvs
git-daemon
git-email
git-gui
git-svn

因为要搭建git协议的服务器,所以git-daemon是必须要安装的,git-daemon支持两种启动方式,一种是git-daemon 的直接启动方式,一种是利用centos下的xinetd来加载git进程。

1.2.1 创建目录及版本库

[root@yongge~]# mkdir /app/git/ -p

 # 创建一个git版本库
[root@yongge-git] cd /app/git
[root@yongge-git]# git init
Initialized empty Git repository in /project/git/.git/
[root@yongge-git]mkdir /app/project
# 创建项目目录
[root@yongge-git]cd /app/project
[root@yongge-project]# git clone –bare /project/git/.git/ my-project.git
Initialized empty Git repository in /project/my-project.git/
warning: You appear to have cloned an empty repository.
[root@yongge/]# tree /project/my-project.git/
/project/my-project.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-commit.sample
│   ├── post-receive.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
├── heads
└── tags
9 directories, 14 files
可以看到这些文件其实和svn的配置文件差不多。
1.2.3 Git 服务器启动
根据官网提示我使用git daemon --reuseaddr --base-path=/project/ /project/ 运行结果失败了,其实git守护进程结合系统运行模式有三种,一种是守护进行运行,后两种是xinetd,sysinit
我选择了centos 结合xined,所以在centos下可以结合xinetd来加载git服务,其实在安装git-daemon的时候也会安装上xined这个包。
Git守护进程的端口是9418
[root@yongge/]# grep 9418 /etc/services
git 9418/tcp # git pack transfer service
git 9418/udp # git pack transfer service
来看看默认xined加载的git服务的配置文件,具体可以去man xined 去了解一下xined
[root@yongge/]# cat /etc/xinetd.d/git
# default: off
# description: The git d?mon allows git repositories to be exported using \
# the git:// protocol.
service git
{
disable = yes
socket_type = stream
wait = no
user = nobody
server = /usr/libexec/git-core/git-daemon
server_args = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
log_on_failure += USERID
}
修完的配置文件
[root@yongge/]# vim /etc/xinetd.d/git
# default: off
# description: The git d?mon allows git repositories to be exported using \
# the git:// protocol.
service git
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/libexec/git-core/git-daemon
server_args = --base-path=/project/my-project.git --export-all --user-path=root --syslog --inetd --verbose
log_on_failure += USERID
}
~
[root@yongge/]# /etc/init.d/xinetd restart
Stopping xinetd: [FAILED]
Starting xinetd: [ OK ]
[root@yongge/]# netstat -anlt|grep 9418
tcp 0 0 :::9418 :::* LISTEN
1.2.4 客户端测试
可以看到xined 结合git走的是tcp协议
客户端测试,客户端测试的时候只需要安装git就行
[root@yongge-a /]# git clone git://20.0.0.89/my-project.git sadoc
Initialized empty Git repository in /sadoc/.git/
warning: You appear to have cloned an empty repository.
将服务器的my-project.git 克隆到本地的sadoc, 不过在客户端测试的时候我发现,git clone 在那个目录下你克隆的服务器版本库就会是你当前执行git clone的目录
在服务器端提交一些文件,在客户端进行下载一下
[root@wx-a /]# cd sadoc/
[root@wx-a sadoc]# git remote -v
origin git://20.0.0.89/my-project.git (fetch)
origin git://20.0.0.89/my-project.git (push)
[root@wx-a sadoc]# git remote
origin
[root@wx-a sadoc]# ls -a
. .. .git
[root@wx-a sadoc]# git remote -v
origin git://20.0.0.89/my-project.git (fetch)
origin git://20.0.0.89/my-project.git (push)
[root@wx-a sadoc]# touch aa
[root@wx-a sadoc]# git add aa
[root@wx-a sadoc]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: aa
#
[root@yongge-a sadoc]# git commit -m "aa" #提交的时候提示我需要配置用户名和邮箱
[master (root-commit) 90291de] aa
Committer: root <root@wx-a.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <[email protected]>'
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 aa

猜你喜欢

转载自blog.csdn.net/yonggeit/article/details/72190717