Files
recommend/git_push.sh
赵杰 Jie Zhao (雄狮汽车科技) 7ab87e8f15 优化注册功能
2025-11-03 12:29:32 +08:00

85 lines
2.1 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# -*- coding: utf-8 -*-
# Git 自动上传脚本Linux/Mac
set -e # 遇到错误立即退出
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== Git 自动上传脚本 ===${NC}"
# 检查是否在 git 仓库中
if [ ! -d .git ]; then
echo -e "${RED}错误: 当前目录不是 git 仓库${NC}"
exit 1
fi
# 获取当前分支
CURRENT_BRANCH=$(git branch --show-current)
echo -e "${YELLOW}当前分支: ${CURRENT_BRANCH}${NC}"
# 检查是否有未提交的更改
if [ -z "$(git status --porcelain)" ]; then
echo -e "${YELLOW}没有需要提交的更改${NC}"
read -p "是否继续检查远程更新? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${GREEN}拉取远程更新...${NC}"
git pull origin "$CURRENT_BRANCH" || echo -e "${YELLOW}拉取失败,可能没有远程分支${NC}"
fi
exit 0
fi
# 显示当前状态
echo -e "${YELLOW}当前更改状态:${NC}"
git status --short
# 询问是否继续
read -p "是否继续提交并推送? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}操作已取消${NC}"
exit 0
fi
# 获取提交信息
if [ -z "$1" ]; then
echo -e "${YELLOW}请输入提交信息(或直接按回车使用默认信息):${NC}"
read -r COMMIT_MSG
if [ -z "$COMMIT_MSG" ]; then
COMMIT_MSG="chore: update code $(date '+%Y-%m-%d %H:%M:%S')"
fi
else
COMMIT_MSG="$1"
fi
# 添加所有更改
echo -e "${GREEN}添加文件到暂存区...${NC}"
git add -A
# 提交更改
echo -e "${GREEN}提交更改...${NC}"
if git commit -m "$COMMIT_MSG"; then
echo -e "${GREEN}提交成功!${NC}"
else
echo -e "${RED}提交失败${NC}"
exit 1
fi
# 推送到远程
echo -e "${GREEN}推送到远程仓库...${NC}"
if git push origin "$CURRENT_BRANCH"; then
echo -e "${GREEN}✓ 推送成功!${NC}"
else
echo -e "${RED}推送失败,可能需要设置远程仓库或认证${NC}"
echo -e "${YELLOW}提示: 可以手动执行 'git push origin $CURRENT_BRANCH'${NC}"
exit 1
fi
echo -e "${GREEN}=== 完成 ===${NC}"