Categories
Tags
API API文档 CAS CI-CD DevOps Docker ElasticSearch Everything Git GitHub GitHub Actions HTTP客户端 Java JWT Micrometer MyBatis-Plus Prometheus Python Redis RSS Spring Spring Boot Supplier Typora uni-app Vue Web Web应用 Web开发 中间件 代码生成 任务执行 任务管理 会话管理 内存模型 分布式 前端开发 协议 后端开发 图床 字符串匹配 安全认证 容器化 局域网 工具 工具类 并发容器 并发编程 开源 微服务 搜索引擎 数据库 数据科学 数据结构 数据验证 文件处理 文件服务器 日语 正则表达式 死锁 深度学习 源码分析 爬虫 版本控制 监控 算法 线程安全 线程池 缓存 脚本 自动化 自然语言处理 虚拟线程 设计模式 语言学习 部署 锁机制 面试 项目实战 高可用
420 words
2 minutes
Git自动化提交脚本指南
1. Windows 环境
在目标文件夹内,创建一个新的.bat文件然后输入以下内容:
@echo off
chcp 65001 >nul
echo Checking Git repository...
git status >nul 2>&1
if errorlevel 1 (
echo Error: Not a Git repository or Git not installed
timeout /t 3 >nul
exit /b 1
)
:input
set /p commit_message="Enter commit message (default: Auto commit): "
if "%commit_message%"=="" (
set commit_message="Auto commit at %date% %time%"
)
echo Staging changes...
git add . >nul
if errorlevel 1 (
echo Error: Failed to stage files
timeout /t 3 >nul
exit /b 1
)
echo Committing changes...
git commit -m %commit_message% >nul
if errorlevel 1 (
echo Notice: No changes to commit
goto retry
)
echo Pushing to remote...
git push >nul
if errorlevel 1 (
echo Error: Push failed
timeout /t 3 >nul
exit /b 1
)
echo Successfully committed and pushed!
timeout /t 3 >nul
exit /b 0
:retry
choice /c yn /m "Retry with empty commit? (y/n)"
if errorlevel 2 exit /b 1
git commit --allow-empty -m %commit_message%
goto :eof
2. Linux 环境
在目标文件夹内,创建一个新的git-autocommit.sh文件然后输入以下内容:
# Function to check Git status
check_git_repo() {
git status >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Not a Git repository or Git not installed" >&2
exit 1
fi
}
# Function to handle commit
perform_commit() {
git commit -m "$1" >/dev/null 2>&1
return $?
}
# Main execution
check_git_repo
# Get commit message
read -r -p "Enter commit message (default: Auto commit): " commit_message
if [ -z "$commit_message" ]; then
commit_message="Auto commit at $(date '+%Y-%m-%d %H:%M:%S')"
fi
# Stage changes
echo "Staging changes..."
git add . >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Failed to stage files" >&2
exit 1
fi
# Commit changes
if ! perform_commit "$commit_message"; then
echo "Notice: No changes to commit"
read -r -p "Retry with empty commit? (y/n): " retry_choice
case "$retry_choice" in
[yY]|[yY][eE][sS])
git commit --allow-empty -m "$commit_message" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Empty commit failed" >&2
exit 1
fi
;;
*)
exit 1
;;
esac
fi
# Push changes
echo "Pushing to remote..."
git push >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Push failed" >&2
exit 1
fi
echo "Successfully committed and pushed!"
使用方式:
chmod +x git-autocommit.sh
./git-autocommit.sh
Git自动化提交脚本指南
https://mj3622.github.io/posts/经验分享/脚本化管理git仓库/
