创建局域网git裸仓库
不说废话,直接上脚本
#!/bin/bash
# 获取脚本所在的目录
script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ $# -eq 0 ]; then
echo "请提供仓库的名称作为参数。"
exit 1
fi
repo_name=$1
repo_path="$script_path/$repo_name.git"
ip_address=$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | grep -v 'inet6' | awk '{print $2}' | cut -d'/' -f1 | head -n 1)
url="ssh://root@$ip_address$script_path/$repo_name.git"
# 检查是否已经存在同名目录或文件
if [ -e $repo_path ]; then
echo "目录或文件 '$repo_name' 已经存在。请使用其他名称。"
exit 1
fi
# 切换到脚本所在目录
cd $script_path
# 创建裸仓库
git init --bare $repo_path
echo "------------------------------------------------------------------------"
echo ""
echo "裸仓库 '$url' 创建成功。"
echo ""
echo "------------------------------------------------------------------------"
cat << EOF
# 使用教程:
## Git 全局设置:
git config --global user.name "DerekLiu"
git config --global user.email "yjkhtddx@sina.com"
## 创建 git 仓库:
mkdir $repo_name
cd $repo_name
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin $url
git push -u origin "master"
## 已有仓库?
cd existing_git_repo
git remote add origin $url
git push -u origin "master"
EOF
echo "------------------------------------------------------------------------"
发现每次手工拼地址有些麻烦,写个查询脚本。
#!/bin/bash
# 获取脚本所在的目录
script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 获得局域网那个IP地址
private_ip=$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | grep -v 'inet6' | awk '{print $2}' | cut -d'/' -f1 | head -n 1)
# 获得公网IP地址
response=$(curl -s ipinfo.io)
# public_ip=$(echo "$response" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
public_ip=$(echo "$response" | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}')
for dir in $(find $script_path -maxdepth 1 -type d -not -path "$script_path"); do
repo_name=$(basename "$dir")
repo_path=$script_path/$repo_name
private_url="ssh://root@$private_ip$repo_path"
public_url="ssh://root@$public_ip$repo_path"
echo "[$repo_name]"
echo -e "path:\t\t$repo_path"
echo -e "private_url: \t$private_url"
echo -e "public_url: \t$public_url"
done