搭建本地git服务器(搭建本地git服务器怎么用)

怎么clone自己搭建的git服务器

首先需要装好CentOS系统,作为测试,你可以选择装在虚拟机上,这样比较方便。这步默认你会,就不讲了。

有了CentOS,那么如何搭建Git服务器呢?

1、首先需要安装Git,可以使用yum源在线安装:

[root@localhost Desktop]# yum install -y git

2、创建一个git用户,用来运行git服务

# adduser git

3、初始化git仓库:这里我们选择/data/git/learngit.git来作为我们的git仓库

[root@localhost git]# git init --bare learngit.git

Initialized empty Git repository in /data/git/learngit.git/

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

[root@localhost git]# chown git:git learngit.git

4、在这里,Git服务器就已经搭得差不多了。下面我们在客户端clone一下远程仓库

Zhu@XXX /E/testgit/8.34

$ git clone git@192.168.8.34:/data/git/learngit.git

Cloning into \'learngit\'...

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

RSA key fingerprint is 2b:55:45:e7:4c:29:cc:05:33:78:03:bd:a8:cd:08:9d.

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

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

git@192.168.8.34\'s password:

这里两点需要注意:第一,当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告:

The authenticity of host \'github.com (xx.xx.xx.xx)\' can\'t be established.

RSA key fingerprint is xx.xx.xx.xx.xx.

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

这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:

Warning: Permanently added \'github.com\' (RSA) to the list of known hosts.

这个警告只会出现一次,后面的操作就不会有任何警告了。

如果你实在担心有人冒充GitHub服务器,输入yes前可以对照GitHub的RSA Key的指纹信息是否与SSH连接给出的一致。

第二,这里提示你输入密码才能clone,当然如果你知道密码,可以键入密码来进行clone,但是更为常见的方式,是利用SSH的公钥来完成验证。

5、创建SSH Key

首先在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

$ ssh-keygen -t rsa -C \"youremail@example.com\"

你需要把邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,由于这个Key也不是用于军事目的,所以也无需设置密码。

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

6、Git服务器打开RSA认证

然后就可以去Git服务器上添加你的公钥用来验证你的信息了。在Git服务器上首先需要将/etc/ssh/sshd_config中将RSA认证打开,即:

1.RSAAuthentication yes

2.PubkeyAuthentication yes

3.AuthorizedKeysFile .ssh/authorized_keys

这里我们可以看到公钥存放在.ssh/authorized_keys文件中。所以我们在/home/git下创建.ssh目录,然后创建authorized_keys文件,并将刚生成的公钥导入进去。

然后再次clone的时候,或者是之后push的时候,就不需要再输入密码了:

Zhu@XXX/E/testgit/8.34

$ git clone git@192.168.8.34:/data/git/learngit.git

Cloning into \'learngit\'...

warning: You appear to have cloned an empty repository.

Checking connectivity... done.

7、禁用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服务器的简单方法

当资源有限,但是项目同时需要几个人协同开发,我们就需要配置一个简单的局域网内的git服务器,方便协同开发。

首先我们新建远端的git目录,目录名和本地仓库名一致,并且在目录下运行:

git init --bare

一个空的git仓库就建立好了。然后我们需要把本地的仓库和远端的关联起来。具体做法是,在本地git仓库的目录下执行:

git remote add origin ssh://用户名@ip/仓库路径

比如:git remote add origin ssh://android@192.168.31.72/home/android/projects/gitserver/demoproject/。完成后,本地的提交,就可以push到远端啦。比如:

git push origin master

就可以把本地的master推送到远端。协同开发的同事可以通过如下命令获取远端的仓库

git clone ssh://android@192.168.31.72/home/android/projects/gitserver/demoproject/

是不是很简单呢

ps:实际使用过程中发现了一个问题,即本机的ip地址不是静态的。如何解决这个问题呢?可以在每次ip改变以后,重置仓库的origin url:

git remote set-url origin {url}

使用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指的是实用经典案例的意思,是对一些普遍性问题的解决方案的总结和整理。

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

windows git服务器怎么创建

安装步骤此处略去。

END

安装CopSSH

安装步骤此处略去。

END

修改配置

修改CopSSH配置文件C:\\Program Files\\ICW\\etc\\sshd_config,确保如下行为非注释行,且设置为“no”:

END

生成用户帐号

1

在服务器上生成Windows用户,取消用户下次登录时须更改密码,设置密码永不过期:

2

将该用户隶属于GitUser组(如尚未生成改组,则先生成改组):

END

激活用户

1

在Windows启动程序组中,运行如下程序(C:\\Program Files\\ICW\\bin\\ copsshcp.exe):

2

进入COPSSH Control Panel应用对话框,正常情况下服务应该为正在运行(图标为绿色,如为红色,则可尝试点选该按钮,启动该服务):

3

选择Users页面:

4

点选Add按钮,出现如下导航对话框:

5

选择Forward按钮,出现如下页面,选择欲激活的用户对应的域名及用户名:

6

选择Forward按钮,进入如下页面,选择Linux shell and Sftp,所有选项选中:

7

选择Forward按钮,进入确认页面,选择Apply:

8

回到如下页面,选择Apply后,关闭。

END

公钥文件上传

1

从用户处取得对应帐户的公钥文件authorized_keys,复制到C:\\Program Files\\ICW\\home\\ricky\\.ssh目录下(\\ricky\\是对应的用户名);

2

Windows启动程序组中运行Start a Unix BASH Shell(或Windows运行中运行命令:\"C:\\Program Files\\ICW\\bin\\bash.exe\" --login -i):

3

运行如下unix命令行,如运行未出错,则完毕。

END

生成Git库容器目录

1

在服务器硬盘上生成一个目录,用来容纳Git库,比如生成E:\\ GITTestRepo\\目录;

2

鼠标右键点击该目录,选择共享和安全…;

3

在弹出的该目录属性对话框的安全页中加入用户对应的组GitUser;

4

确认该用户组权限设置了允许修改、读取和运行、列入文件夹目录、读取、写入,选择确定后完毕。

END

确认防火墙开放了22端口

确认服务器的防火墙没有禁止22端口的TCP/IP通信。

END

修改用户登录运行脚本

1

修改C:\\Program Files\\ICW\\home\\ricky\\.bashrc文件(\\ricky\\是对应的用户名),在文件最后加入

fatal: protocol error: bad line length character: This

昨晚尝试搭建一个Git服务器,在搭建好服务器后,在服务器创建了一个空项目,我在本地使用git clone 拉取项目时,报了fatal: protocol error: bad line length character: This的错误。这个问题是出在Git服务器端,不是Git客户端的问题,所以Git客户端不需要修改任何东西。

我们可以查看如果我们是通过git用户拉去项目的,我们可以在服务器端通过cat /etc/passwd来查看git的登录shell,应该可以看到类似下面一行:

我们可以通过vim把它改为

或者通过命令修改也可以

这样Git客户端就可以通过git账户拉取项目啦。

未经允许不得转载:便宜VPS网 » 搭建本地git服务器(搭建本地git服务器怎么用)