CentOS7上git环境的搭建 和 仓库的创建见这两篇文章
git环境的搭建: https://www.jianshu.com/p/a73c9d9779ea
git创建仓库: https://www.jianshu.com/p/30f11666a72d
因为公司具体业务,创建了很多仓库, 在创建的过程中就发现,实际上每次创建都是一个重复性的过程,所以就考虑把这些手动的重复步骤写成一个自动化脚本,后续只需要调用脚本 传递参数就可以快速创建仓库 以下就是脚本源码, 发出来供大家参考:
```
#!/bin/bash
# ./git.sh 仓库名称 工作路径
if [ "$1" = "" ] || [ "$2" = "" ]; then
echo "参数: 仓库名称 工作路径 <command>"
echo "<command>:"
echo "-create | -c 创建并初始化仓库"
echo "-reinit | -r 重新初始化仓库"
exit 1
fi
git_name="$1.git"
if [ "$3" = "-reinit" ] || [ "$3" = "-r" ]; then
if [ ! -d $git_name ];then
echo "仓库不存在,请检查"
exit 1
fi
if [ ! -d $2 ];then
echo "工作区不存在,请检查"
exit 1
fi
git --work-tree=$2 --git-dir=/home/www/$git_name rm -r --cached .
git --work-tree=$2 --git-dir=/home/www/$git_name add .
git --work-tree=$2 --git-dir=/home/www/$git_name commit -m 'init'
echo "git重新初始化完成 git clone www@xx.xx.xx.xx:$git_name"
exit 1
fi
if [ "$3" != "" ] && [ "$3" != "-create" ] && [ "$3" != "-c" ]; then
echo "<command>参数错误"
exit 1
fi
if [ ! -d $git_name ];then
git init --bare $git_name
else
echo "文件夹已经存在,无法创建仓库"
exit 1
fi
if [ ! -d $2 ];then
mkdir $2
fi
chown www:www -R $git_name
chown www:www -R $2
# 添加忽略规则 在同级别目录创建文件 这里逐行读取并写入到exclude文件中 此步骤可省略
for line in `cat .gitignore`
do
echo $line >> "$git_name/info/exclude"
done
# 添加忽略规则结束
echo "#!/bin/sh" >> "$git_name/hooks/post-receive"
echo "git --work-tree=$2 --git-dir=/home/www/$git_name checkout -f" >> "$git_name/hooks/post-receive"
chown www:www -R $git_name
chmod 777 "/home/www/$git_name/hooks/post-receive"
chown www:www -R $2
git --work-tree=$2 --git-dir=/home/www/$git_name rm -r --cached .
git --work-tree=$2 --git-dir=/home/www/$git_name add .
git --work-tree=$2 --git-dir=/home/www/$git_name commit -m 'init'
chown www:www -R $git_name
chmod 777 "/home/www/$git_name/hooks/post-receive"
chown www:www -R $2
echo "git创建成功 git clone www@xx.xx.xx.xx:$git_name"
exit 1
```