Linux环境下搭建git远程私库


title: Linux环境下搭建git远程私库
date: 2020-02-05
updated: 2020-02-05
categories:

  • 搬砖笔记
  • linux

tags:

  • git
  • linux

git远程私库就是把自己的Linux下的git仓库作为服务端,客户端通过git和其建立连接。远程私库可以用于部署项目或者团队协同开发。本人主要介绍git安装和配置过程。本人涉及的linux系统版本是Ubantu18.04和Centos6.5。

搭建远程私库前确保远程服务器可以通过ssh连接成功,命令格式:ssh 用户名@ip地址。连接成功如下所示:
在这里插入图片描述

git安装

通过包管理工具安装

Ubuntu18.04环境下

#更新系统工具包
apt-get update -y
#安装Git
apt install git
#查看git
git version

Centors6.5环境下

#安装git
yum install -y git	
#查看git
git version

通过安装包安装

安装前准备

yum -y install curl curl-devel zlib-devel openssl-devel perl cpio expat-devel gettext-devel gcc cc

下载安装包

https://github.com/git/git/releases	</br>

解压

tar zxvf git-2.25.0.tar.gz

编译安装

cd git-2.25.0
make prefix=/usr/local/git-2.25.0 all
make prefix=/usr/local/git-2.25.0 install

查看git

git version

git配置

配置环境变量(安装包安装时)

编辑配置文件

vim /etc/profile

在里面加入加入export PATH=$PATH:/usr/local/git-2.25.0/bin

生效配置文件

 source /etc/profile

配置git

设置用户名 密码 邮箱

git config --global user.name "Your Name"
git config --global user.password  "Your Password "
git config --global user.email "[email protected]"
#查看配置是否生效
git config --list

配置ssh公钥

ssh-keygen -t rsa //按三下回车键 系统会提示密钥的保存位置(一般是~/.ssh目录)

创建仓库

创建仓库文件

mkdir -p /usr/local/gitspace

初始化仓库

cd gitspace
git init --bare

创建Linux用户(可选)

useradd -m liquanhong //Ubuntu中需要加-m参数,不然不会在home中创建文件夹
passwd liquanhong //设置用户密码
chown -R liquanhong:liquanhong /usr/local/gitspace //给仓库分配所属组		

ssh免密登录

方法1:把本地的git公钥文件id_rsa.pub,复制到~/.ssh文件夹并改为 authorized_keys
方法2:新建 authorized_keys文件,内容改为本地的git公钥文件id_rsa.pub的内容
设置成功后客户端不需要输入密码也可以进行拉取、提交代码等操作。
~相当于/home/liquanhong

配置hooks

编辑post-receive

vim /usr/local/gitspace/hooks/post-receive

如果没有该文件,创建一个。文件内容如下:

git --work-tree=/usr/local/blog --git-dir=/usr/local/gitspace checkout -f

其中/usr/local/blog是提交代码后git自动生成文件夹目录

赋予权限

chown -R liquanhong:liquanhong /usr/local/blog
chmod +x /usr/local/gitspace/hooks/post-receive//这个很关键必须执行

hooks配置成功后,客户端每次提交代码git会自动在/usr/local/blog目录更新提交的代码文件

如果用root作为git用户,所有chown命令不需要执行。

客户端操作

本地克隆仓库

远程私库格式:git clone linux用户名@ip地址:仓库路径
例子:git clone [email protected]:/usr/local/gitspace

仓库能克隆到本地说明远程私库没问题,正常用git commit,git push等命令就能愉快玩耍了!

发布了30 篇原创文章 · 获赞 76 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/a1275302036/article/details/104170603
今日推荐