#!/bin/bash # 脚本用法:./git-commit-and-tag.sh "你的提交描述" "版本号" # 检查参数数量 if [ $# -ne 2 ]; then echo "错误: 脚本需要2个参数。" echo "用法: $0 \"提交描述\" \"版本号\"" echo "示例: $0 \"修复了登录问题\" \"v1.2.3\"" exit 1 fi # 分配参数 COMMIT_MESSAGE="$1" VERSION_TAG="$2" # 检查当前目录是否为Git仓库 if ! git rev-parse --git-dir > /dev/null 2>&1; then echo "错误: 当前目录不是一个Git仓库。" exit 1 fi # 检查是否有未暂存的更改 if [ -z "$(git status --porcelain)" ]; then echo "提示: 没有检测到任何更改需要提交。" exit 0 fi echo "开始提交并打版本标签..." echo "提交描述: $COMMIT_MESSAGE" echo "版本标签: $VERSION_TAG" # 添加所有更改到暂存区[citation:4][citation:6] git add . # 进行提交[citation:4][citation:10] git commit -m "$COMMIT_MESSAGE" if [ $? -ne 0 ]; then echo "错误: 提交失败。" exit 1 fi # 创建轻量标签或附注标签(这里创建轻量标签)[citation:4] git tag "$VERSION_TAG" if [ $? -ne 0 ]; then echo "错误: 创建标签失败。" exit 1 fi # 推送到远程仓库并推送标签[citation:4] echo "正在推送到远程仓库..." git push git push origin "$VERSION_TAG" echo "✅ 完成!提交已推送,版本标签 $VERSION_TAG 已创建并推送。"