airshare1 / git /init /init_step copy.sh
tanbushi's picture
Initial commit
75ce4df
#!/bin/bash
# 检查是否提供了所有三个参数
if [ "$#" -ne 3 ]; then
echo "用法: $0 <当前分支> <别名> <仓库地址>"
exit 1
fi
rm -fr .git
CURRENT_BRANCH="$1"
ALIAS="$2"
REPO_URL="$3"
TEMP_BRANCH="$4"
echo "当前分支: ${CURRENT_BRANCH}"
echo "别名: ${ALIAS}"
echo "仓库地址: ${REPO_URL}"
# rm -fr .git
# 检查当前目录是否为 Git 仓库
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "错误:当前目录不是 Git 仓库。初始化 Git 仓库...。"
git init -b "${CURRENT_BRANCH}"
echo "初始化 Git仓库成功!"
fi
# 获取当前 Git 分支
ACTUAL_CURRENT_BRANCH=$(git branch --show-current)
echo "当前分支: ${ACTUAL_CURRENT_BRANCH}"
if [ "${ACTUAL_CURRENT_BRANCH}" != "${CURRENT_BRANCH}" ]; then
echo "当前分支 (${ACTUAL_CURRENT_BRANCH}) 与预期分支 (${CURRENT_BRANCH}) 不匹配。正在尝试切换到 ${CURRENT_BRANCH}..."
git checkout "${CURRENT_BRANCH}"
if [ $? -ne 0 ]; then
echo "错误:无法切换到分支 ${CURRENT_BRANCH}。请确保该分支存在。"
git checkout -b "${CURRENT_BRANCH}"
fi
echo "已成功切换到分支 ${CURRENT_BRANCH}。"
else
echo "当前分支已经是 ${CURRENT_BRANCH}。"
fi
# 检查远程仓库是否已存在,如果不存在则添加
if ! git remote get-url "${ALIAS}" > /dev/null 2>&1; then
echo "远程仓库 ${ALIAS} 不存在。正在添加远程仓库..."
git remote add "${ALIAS}" "${REPO_URL}"
if [ $? -ne 0 ]; then
echo "错误:无法添加远程仓库 ${ALIAS}。"
exit 1
fi
echo "远程仓库 ${ALIAS} 已成功添加。"
else
echo "远程仓库 ${ALIAS} 已存在。"
fi
git add .
git commit -m "Initial commit"
# 将远程仓库的main分支拉取到本地临时分支:temp_branch
git pull "${ALIAS}" main:temp_branch --rebase
# 将临时分支合并到本地分支
git checkout temp_branch
rm README.md
rm .gitattributes
# git add .
# git commit -m "Merge temp_branch into ${CURRENT_BRANCH}"
git checkout "${CURRENT_BRANCH}"
git merge temp_branch
git branch -d temp_branch
git restore --staged README.md
git restore --staged .gitattributes
git add .
git commit -m "update"
# 强制推送
git push --force "${ALIAS}" "${CURRENT_BRANCH}":main # 通常 Space 默认分支为 main