Git服务器搭建步骤及配置方法:
安装Git
1. 下载安装Git,可以到官网下载,也可以使用Linux自带的包管理工具安装。
2. 安装完成后,需要设置Git的用户名和邮箱:
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
搭建Git服务器
1. 需要在服务器上创建一个git用户,以及一个用于存放git仓库的文件夹:
# 创建git用户 $ sudo adduser git # 创建用于存放git仓库的文件夹 $ sudo mkdir -p /var/git/project.git
2. 需要在这个文件夹上初始化git仓库:
# 切换到git用户 $ sudo su git # 初始化git仓库 $ cd /var/git/project.git $ git init --bare
3. 需要配置一些权限,以允许用户连接和操作仓库:
# 设置文件夹的所有者 $ sudo chown -R git:git /var/git/project.git # 设置文件夹的权限 $ sudo chmod -R 755 /var/git/project.git
创建SSH Key
1. 在服务器上创建SSH Key,可以使用ssh-keygen命令:
# 切换到git用户 $ sudo su git # 创建SSH Key $ ssh-keygen -t rsa -C "your_email@example.com"
2. 创建完成后,会在用户的home目录下生成一个.ssh文件夹,里面有一个id_rsa和id_rsa.pub文件,其中id_rsa.pub文件就是SSH Key的公钥,可以将它添加到git仓库中:
# 将SSH Key的公钥添加到git仓库 $ cd /var/git/project.git $ git config --local user.name "Your Name" $ git config --local user.email "your_email@example.com" $ git config --local core.sshCommand "ssh -i /home/git/.ssh/id_rsa" $ git config --local receive.denyNonFastForwards true $ git config --local receive.denyDeletes true $ git config --local receive.denyDeleteCurrent true $ git config --local receive.denyCurrentBranch ignore
配置Git服务器
1. 在服务器上配置Git服务器,可以使用git-daemon命令:
# 启动Git服务器 $ git daemon --base-path=/var/git --export-all --enable=receive-pack
2. 启动Git服务器后,就可以使用git clone命令将git仓库克隆到本地:
# 克隆git仓库 $ git clone git@server_ip:/var/git/project.git
3. 克隆完成后,就可以在本地进行操作,比如提交代码、查看提交记录等:
# 查看提交记录 $ git log # 提交代码 $ git add . $ git commit -m "commit message" $ git push origin master