创建局域网git裸仓库

创建局域网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

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值