2022
我们一起努力

linux搭建git服务器的简单介绍

本文目录:

  • 1、搭建Git服务器及备份服务器
  • 2、如何搭建linux git服务器
  • 3、linux如何搭建git

搭建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的,中间有一部分是怎么去搭建,你可以看下

linux如何搭建git

1、环境准备

服务器:CentOS 7.3 + git (1.8.3.1)

客户端:win10 + git (2.17.0.windows.1)

2、服务器安装git

yum install -y git 

3、创建git用户,管理 git服务

[root@localhost home]# useradd git

[root@localhost home]# passwd git

4、服务器创建git 仓库

设置/home/git/repository-git 为git 服务器仓库,然后把 git 仓库的 owner 修改为 git 用户。

复制代码

[root@localhost git]# mkdir repository-git

[root@localhost git]# git init --bare repository-git/

Initialized empty Git repository in /home/git/repository-gt/

[root@localhost git]# chown -R git:git repository-git/

5、客户端安装git

下载 Git for Windows,地址:

安装完之后,可以使用 Git Bash 作为命令行客户端。

5.1、选择一个目录 F:\project\sell 作为本地仓库,右键进入Git Bash 命令行模式

初始化本地仓库:git init

5.2、尝试克隆一个服务器的空仓库到本地仓库

git clone git@192.168.116.129:/home/git/repository-gt

第一次连接到目标 Git 服务器时会得到一个提示:

The authenticity of host '192.168.116.129(192.168.116.129)' can't be established.

RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.

Are you sure you want to continue connecting (yes/no)?

选择 yes:

Warning: Permanently added '192.168.116.129' (RSA) to the list of known hosts.

此时 C:\Users\用户名\.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。

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

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

评论 抢沙发

评论前必须登录!