2022
我们一起努力

搭建git服务器(搭建git服务器 windows)

本文目录:

  • 1、如何创建自己的私有git服务器
  • 2、linux怎么搭建git服务器
  • 3、搭建Git服务器及备份服务器
  • 4、如何搭建linux git服务器
  • 5、使用Gitolite搭建Git服务器

如何创建自己的私有git服务器

通过以下的步骤,你将在服务器上搭建并使用你自己的Git服务,例如myhost.example.com。其中的一些步骤,像email通知,限制用户的权限,特定分组的接入管理,都是依据你的要求和现实状况的可选项。还有很多命令,你需要root权限才能执行,所以别忘了用sudo,或者(最好不要)直接切换到root来执行。

为有读写权限的用户建立一个分组。根据你的操作系统,你可以用groupadd命令来实现,用vigr来编辑分组文件,或者直接编辑/etc/group文件。在最后,你会在/etc/group文件中看到如下一行

repogroup:*:10005:marry,john,violet

其中,repogroup是准许接入这个仓库的组的名字。10005是一个独一无二的分组识别数字,marry,john,violet则是获准接入这个仓库的用户。

决定Git仓库的路径。它既可以放在你的home路径下(e.g.

/home/yourname/gitroot),也可以放在一个专用的路径下(e.g.

/var/gitroot).

配置权限,让Git用户可以访问这个目录

chmod

g+rx

/path-to/gitroot

chown

:grouprepo

/path-to/gitroot

建立新的Git仓库,叫做newrepo

cd

/path-to/gitroot

git

init

–bare

newrepo.git

建立路径认证,以允许用户组访问,同时有针对性的设置Git

cd

newrepo.git

chown

-R

:grouprepo

.

git

config

core.sharedRepository

group

find

.

-type

d

-print0

|

xargs

-0

chmod

2770

find

.

-type

f

-print0

|

xargs

-0

chmod

g=u

设置提交(commit)的email通知(commit是一条命令),这样当有新的修改提交到仓库的时候,开发者们将会收到一封关于修改内容一览的电子邮件。

echo

‘One-line

project

description’

description

git

config

–local

hooks.mailinglist

email-a@example.com,email-b@example.com,…

git

config

–local

hooks.emailprefix

‘[DI-PR]

git

config

–local

hooks.showrev

“git

show

-C

%s;

echo”

git

config

–local

hooks.emailmaxlines

100

通过设置一个称为钩子(hook)的东东,来创建这些email通知。

cd

hooks

cp

post-receive.sample

post-receive

chmod

+x

post-receive

从post-receive的脚本中移除掉最后一行注释的#字号,最后语句应该是这样的

.

/path-to-hooks/post-receive-email

在你的库中先放入一个文件(比如README)。为了避免其他用户在第一次提交时遇到奇怪的错误信息时感到困惑,这是有必要的。

cd

to-your-personal-working-directory

git

clone

myhost.example.com:/path-to/gitroot/newrepo.git

echo

“Short

project

description”

README.txt

git

add

README.txt

git

commit

-a

-m

“Add

README

file”

git

push

origin

master

#

第一次仅仅需要T”origin

master”

这个参数

为仓库的其他用户建立账户。依据你系统的不同,你可以通过useradd

或者adduser来实现。

设置用户可以通过公/私钥配对来访问。这包括以下几步:

1)已经有公钥的用户,只需要把公钥发给你就好。

2)没有公钥的用户,必须用ssh-keygen命令来生成一个,然后把.ssh/id_rsa.pub发给你就可以了。

3)之后你必须在他们对应的账户下面建立这种公/私钥配对。复制他的公钥,然后顺序执行下面的命令。

sudo

su

username

mkdir

-p

.ssh

cat

.ssh/authorized_keys

\EOF

paste-key-as-one-line

EOF

exit

改变用户的账户让他们使用受限的shell。如果你想让你添加进来的用户仅仅使用Git,而不是Unix的所有东西,那么就设置他们的账户,让他们只能使用Git。Git提供了这种专为这种需求设计的受限shell。它通常被安装在

/usr/libexec/git-core/git-shell

or

/usr/local/libexec/git-core/git-shell。因此,对所有你想限制权限的用户,你可以执行以下命令。

sudo

chsh

-s

/usr/libexec/git-core/git-shell

username

告诉你的用户,用下面的语句来克隆仓库到本地。

git

clone

myhost.example.com:/path-to/gitroot/newrepo.git

到了这里,恭喜你,你成功了!

linux怎么搭建git服务器

GitHub就是一个免费托管开源代码的远程仓库。但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用。

搭建Git服务器需要准备一台运行Linux的机器,强烈推荐用Ubuntu或Debian,这样,通过几条简单的apt命令就可以完成安装。

假设你已经有sudo权限的用户账号,下面,正式开始安装。

第一步,安装git:

$ sudo apt-get install git

第二步,创建一个git用户,用来运行git服务:

$ sudo adduser git

第三步,创建证书登录:

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

第四步,初始化Git仓库:

先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令:

$ sudo git init –bare sample.git

Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git:

$ sudo chown -R git:git sample.git

第五步,禁用shell登录:

出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/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 clone命令克隆远程仓库了,在各自的电脑上运行:

$ git clone git@server:/srv/sample.git

Cloning into ‘sample’…

warning: You appear to have cloned an empty repository.

剩下的推送就简单了。

搭建Git服务器及备份服务器

OS:CentOS 7.2

Git:1.8.3.1

备份模式:

以下步骤以双机备份为例,单机备份同理,只是在镜像git项目的时候把git url换成单机中的git项目目录即可。

首先,查看系统软件库中是否有git和git的版本:

CentOS 7.2环境下的输出如下:

CentOS 7.x版本的仓库中已经附带了1.8.3.1版本的git,可以直接安装。

CentOS 6.x中的git是1.7.x版本,自带库中git版本低的linux发行版可以添加git1.8.3.1的源来安装git,但这个操作要求本机能访问互联网

其他版本的git理论上也可以,请自行测试

结果最后显示“安装完成”或“Complete”表示安装成功。可以直接使用git命令测试一下:

输出如下表示git安装成功并可用。

创建一个git用户,用来提供给外部用户以git url的方式访问git库。

在用户目录或其他对外目录中创建 /git/repos 目录,用来存放git库。

注意: 尽量使用git用户来创建该目录,方便以后git操作该目录,如果是使用其他用户创建的,记得使用以下命令将该目录的owner改为git:

为了方便访问,可以在根目录下创建一个软链连接到该目录:

这样设置后,假如以后有一个库叫 test.git ,那么就可以通过以下url来访问git库了:

创建一个测试库用来测试备份及连通性。

同主GIT服务器的《安装git》章节。

同主GIT服务器的《创建git库目录》章节。

目录结构尽量与主git服务器上的结构一致,如果不一致,使用软链的形式将git url配置为与主git服务器一致,这样保证在切换服务器时,客户端不用做修改操作。

以上个步骤中创建的 test.git 为例:

这样,主git服务器中的 test.git 就镜像到备份机中了。

如果主服务器的git项目发生了变更,可以将变更同步到备份机。

将以上同步命令写成脚本,添加一个定时任务来定时同步即可。

(待补充)

备份同步时每次都需要输入主git服务器的密码,比较麻烦,也不利于定时同步的脚本操作,可以使用ssh免密登录的方式,在主服务器上配置备份服务器的公钥。

一路Enter(回车键),完成后会打印出密钥的生成位置,通常在用户主目录的 .ssh 目录中。默认情况下会生成以下两个文件:

一定要注意上述命令中的第三步,权限要设置对,否则认证不过去不能免密登录。

如果在实际运行中,主git服务器挂了一段时间,在这段时间里一直是备份git服务器在工作,那么在修复好主服务器后需要将这一段时间的变更同步回主服务器。

操作完成后备份库自上一次同步后的更改都推送到了主库。

如何搭建linux git服务器

首先我们分别在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-1.8.3.1-4.el7.x86_64 already installed and latest version

Nothing to do

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

[root@linuxprobe ~]# mkdir linuxprobe.git

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

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

初始化Git版本仓库:

[root@linuxprobe ~]# cd linuxprobe.git/

[root@linuxprobe linuxprobe.git]# git –bare init

Initialized empty Git repository in /root/linuxprobe.git/

其实此时你的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_rsa.pub.

The key fingerprint is:

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

The key’s randomart image is:

+–[ RSA 2048]—-+

| .o+oo.o. |

| .oo *.+. |

| ..+ E * o |

| o = + = . |

| S o o |

| |

| |

| |

| |

+—————–+

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

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

root@192.168.10.10’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘192.168.10.10’”

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

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

[root@linuxprobe ~]# git clone root@192.168.10.10:/root/linuxprobe.git

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 user.name “Liu Chuan”

[root@linuxprobe ~]# git config –global user.email “root@linuxprobe.com”

[root@linuxprobe ~]# git config –global core.editor vim

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

[root@linuxprobe linuxprobe]# echo “I successfully cloned the Git repository” readme.txt

[root@linuxprobe linuxprobe]# git add readme.txt

[root@linuxprobe linuxprobe]# git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use “git rm –cached …” to unstage)

#

# new file: readme.txt

#

[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 readme.txt

[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@192.168.10.10:/root/linuxprobe.git

将文件提交到远程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@192.168.10.10:/root/linuxprobe.git

* [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@192.168.10.10:/root/linuxprobe.git

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 readme.txt

I successfully cloned the Git repository

这篇是详细介绍Git的,中间有一部分是怎么去搭建,你可以看下

使用Gitolite搭建Git服务器

Git服务的管理工具,主要有如下几种。

Gitolite 使用perl语言编写,维护和更新比较积极,下面测试使用Gitolite搭建Git服务器。

一般新建用户 ~/.ssh/ 目录是不存在的。

生成路径会在ssh-kengen执行后给出,也可修改。windows下生成路径默认位于 C:/user/用户名/.ssh 下。

此时, gitolite 会初始化两个仓库,同时创建 authorized_keys 文件

管理库中有两个目录, conf/ 和 keydir/ 。

仓库的创建通过编辑 gitolite-admin/conf/gitolite.conf 即可,然后将配置后的文件上传服务器。

若本地已有仓库repo2,将其添加到服务器

gitolite可以通过用户组的方式进行管理

如上提示,需要输入密码。

需要安装 openssh ,并将 gitolite 用户添加在 sshusers 组中,有的服务器可能是 ssh 组。

计算机领域的Cookbook指的是实用经典案例的意思,是对一些普遍性问题的解决方案的总结和整理。

【搭建git服务器】的内容来源于互联网,如引用不当,请联系我们修改。

赞(0)
文章名称:《搭建git服务器(搭建git服务器 windows)》
文章链接:https://www.fzvps.com/26914.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

相关推荐

  • 暂无文章

评论 抢沙发

评论前必须登录!