#!/bin/bash # 定义1个变量,变量是git裸仓库名字,例如/www/git/demo.git <<<<<<< HEAD GIT_REPO_NAME="/www/git/wx.sstbc.com.git" ======= GIT_REPO_NAME="/data/git/wx.sstbc.com.git" >>>>>>> 8508a4aca7d83d334aa2ec18b4291c6d5750a769 #检测系统里是否存在用户名是git的用户,如果不存在,就创建一个,并且设置密码为git@2023 if ! id -u git > /dev/null 2>&1; then useradd git echo "已创建用户 git" echo git:git@2023 | chpasswd echo "密码已设置为 Git@2018" else echo "git用户已存在" fi # 检测系统里是否存在git用户组,如果不存在,就创建一个,并把git用户加入git用户组 if grep "^git:" /etc/group >/dev/null 2>&1; then echo "git用户组已存在" else echo "创建git用户组..." groupadd git usermod -aG git git fi # 根据定义的裸仓库路径,在指定路径下创建裸仓库 echo "创建裸仓库..." mkdir -p $GIT_REPO_NAME && cd $GIT_REPO_NAME && git init --bare # 给上一步创建的裸仓库给予git用户组里git用户读写执行权限 echo "给裸仓库设置权限..." chown git:git * -R cd - chown git:git "$GIT_REPO_NAME" # 切换到当前脚本所在的目录,初始化git仓库,并且关联之前创建的本地裸仓库地址。 cd "$(dirname "$0")" if [ -d ".git" ]; then echo "该目录是一个Git仓库" else echo "该目录不是一个Git仓库" echo "初始化git仓库..." git init fi echo "关联仓库" git remote add origin $GIT_REPO_NAME # 输出这个裸仓库的本地链接地址和远程连接地址 echo "远程仓库关联:git remote add production ssh://git@ip:$GIT_REPO_NAME"