如何搭建自己的git服务器

git是什么?

git是目前世界上最先进的分布式版本控制系统。出自linux之父之手

SVN与Git的区别?

SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。
Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

在linux上搭建git服务器
  1. 服务端安装git
    本次案例我使用的linux系统是centos7,windows10
[root@localhost ~]# yum install git
Loaded plugins: fastestmirror
Determining fastest mirrors
base                                                                         | 3.6 kB  00:00:00     
epel                                                                         | 5.3 kB  00:00:00     
extras                                                                       | 2.9 kB  00:00:00     
updates                                                                      | 2.9 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                            | 153 kB  00:00:00     
(2/4): epel/x86_64/updateinfo                                                | 1.0 MB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                           | 2.8 MB  00:00:00     
(4/4): epel/x86_64/primary_db                                                | 6.9 MB  00:00:00     
Package git-1.8.3.1-20.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# git version
git version 1.8.3.1
  1. 客户端安装git
    windows客户端下载git
    小乌龟下载地址
    安装完毕后鼠标右键菜单中会有git Bash,git GUI 等选项,打开git Bash设置用户名邮箱
    因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识。
    每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改。如果使用了 --global 选项,那么该命令只需要运行一次,因为之后无论你在该系统上做任何事情, Git 都会使用那些信息。 当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置。
    打开git bash
    在这里插入图片描述
    如果想要检查你的配置,可以使用 git config --list 命令来列出所有 Git 当时能找到的配置。

  2. 创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码

[root@localhost ~]# useradd git
[root@localhost ~]# passwd git
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
#禁止git用户ssh登录
[root@localhost ~]# vim /etc/passwd
git:x:500:500::/home/git:/usr/bin/git-shell
  1. 将客户端公钥添加到服务器
    客户端生成ssh私钥和公钥,打开windows的git bash,输入ssh-keygen -t rsa -C “任意备注”,生成ssh私钥和公钥
    在这里插入图片描述
    此时 C:\Users\用户名.ssh 下会多出两个文件 id_rsa 和 id_rsa.pub
    id_rsa 是私钥
    id_rsa.pub 是公钥

创建 /home/git/.ssh/authorized_keys文件存放ssh公钥
在 /home/git/ 下创建目录 .ssh

[root@localhost git]# chown -R git:git .ssh
[root@localhost git]# chmod 700 .ssh
[root@localhost git]# touch .ssh/authorized_keys
[root@localhost git]#chmod 600 .ssh/authorized_keys

将客户端公钥id_rsa.pub文件的内容写到服务器端 /home/git/.ssh/authorized_keys 文件里
5. 创建版本库
随便找个文件夹

[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# pwd
/www/wwwroot/git/0242
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# git init --bare 0242.git
Initialized empty Git repository in /www/wwwroot/git/0242/0242.git/
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# ll -al
total 12
drwxr-xr-x 3 root root 4096 Nov  2 18:36 .
drwxr-xr-x 3 root root 4096 Nov  2 18:32 ..
drwxr-xr-x 7 root root 4096 Nov  2 18:36 0242.git

git init
该命令将创建一个名为 .git 的子目录,是隐藏的,这个子目录含有你初始化的 Git 仓库中所有的必须文件,这些文件是 Git 仓库的骨干。
git init --bare xxxx.git
这样初始化的仓库并没有.git目录,只有.git目录下的文件。
不使用–bare选项时,就会生成.git目录以及其下的版本历史记录文件,这些版本历史记录文件就存放在.git目录下;而使用–bare选项时,不再生成.git目录,而是只生成.git目录下面的版本历史记录文件,这些版本历史记录文件也不再存放在.git目录下面,而是直接存放在版本库的根目录下面
把生成的仓库文件所有人改为git用户

[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# chown -R git:git 0242.git
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# ll -al
total 12
drwxr-xr-x 3 root root 4096 Nov  2 18:36 .
drwxr-xr-x 3 root root 4096 Nov  2 18:32 ..
drwxr-xr-x 7 git  git  4096 Nov  2 18:36 0242.git
[root@iZwz9ifm754y5p5n4kw6zlZ 0242]# 
  1. 客户端克隆远程仓库
    windows用小乌龟
    在这里插入图片描述
    linux用命令行
    git clone git@xx.xx.xx.xxx(ip地址):/www/wwwroot/git/0242/0242.git(文件目录) [本地目录]
    克隆成功
    在这里插入图片描述
    在该目录下的文件都可以被版本库所管理
    需要提交代码的时候只需提交(git add)+添加至本地仓库(git commit) +推送至远程仓库(git push)即可
    新建test.txt文件测试是否能推送成功
    在这里插入图片描述
    为了方便管理,作者日期备注等最好填写完整,然后把作了改动的文件打钩,点击提交
    在这里插入图片描述
    提交后再点击推送(可多次提交后再一次性推送)
    在这里插入图片描述
    推送成功,若推送失败,请查询 第四步 与 仓库文件权限
  2. 到一台新的服务器查看本地提交的文件是否已成功同步到远程库
    git clone git@远程仓库IP地址:远程仓库文件目录 本地仓库目录
[root@iZj6ccn4rzv6zts66ne4btZ sl.baidu.com]# git clone git@xx.xxx.xxx.xxx:/www/wwwroot/git/0242/0242.git 0242
Cloning into '0242'...
The authenticity of host 'xx.xxx.xxx.xxx (xx.xxx.xxx.xxx)  ' can't be established.
ECDSA key fingerprint is SHA256:W9f3HJap6HEGlbH61hbdxxKR3+c7c1oQmojREZSD9lI.
ECDSA key fingerprint is MD5:69:e7:85:51:ea:b0:34:de:de:0b:57:33:48:2c:41:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xx.xxx.xxx.xxx' (ECDSA) to the list of known hosts.
git@xx.xxx.xxx.xxx's password: 
Could not chdir to home directory /home/git: Permission denied
remote: warning: unable to access '/home/git/.config/git/attributes': Permission denied
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

在这里插入图片描述
克隆成功,如果克隆失败,可能是该服务器公钥未加入git服务器authorized_keys文件

[root@iZj6ccn4rzv6zts66ne4btZ .ssh]# ssh-keygen -t rsa -C "随便填"
[root@iZj6ccn4rzv6zts66ne4btZ sl.baidu.com]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2jJXJOXoZpD0F1jlGTOrnSt22TWZCHTMeNEPdEV3rKT+THSe74yAhBN3qNlXbNAPp4iF7U3Aykbh2is332Cbf+R9NKCQ6t8/RucE348sKaQhv4Pok9IFFjeDD1vW11iM8KlsrVNtWyP0OUY63LPvDSzGHHt/9v/60/GetCTlk5EBGbohYcYe1/y8I+NNaNOyIqCbdcgEeZWypECc2Usa+XWjI8KuZApmPHpw/EKxv8+5c7JbAH+i0KVbxc2WVfqsEXP6Nc342haoqkrmmo3uNumohKx34teChL9C56Zerz0oU8lpgBQbB+iCqgcZKHUUFiFrs9+28+FmrYZRdXKC9 phpsolo@163.com

复制~/.ssh/id_rsa.pub文件里的内容加入到git服务器authorized_keys文件内
修改新服务器里的test.txt内容,并推送到远程仓库,然后本地拉取查看是否能成功同步

[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git add test.txt 
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git commit -m "测试推送2"
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@iZj6ccn4rzv6zts66ne4btZ.(none)')

出示此报错,按照提示设置其邮箱以及用户名,设置完毕再次commit,commit成功,然后push至远程仓库

[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git config --global user.email "phpsolo@163.com"
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git config --global user.name "hhs"
[root@iZj6ccn4rzv6zts66ne4btZ 0242]# git commit -m "测试推送2"
[master 2178928] 测试推送2
 1 file changed, 1 insertion(+), 1 deletion(-)
 [root@iZj6ccn4rzv6zts66ne4btZ 0242]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

git@xx.xxx.xxx.xxx's password: 
Could not chdir to home directory /home/git: Permission denied
Counting objects: 5, done.
Writing objects: 100% (3/3), 250 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@xx.xxx.xxx.xxx:/www/wwwroot/git/0242/0242.git
   9396e5c..2178928  master -> master

本地服务器拉取,test.txt内容已改变
本次测试两台服务器之间形成了一种多人协作开发的关系,这就是分布式版本管理工具的作用
在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值