420 字
2 分钟
Git自动化提交脚本指南
1. Windows 环境
在目标文件夹内,创建一个新的.bat文件然后输入以下内容:
@echo offchcp 65001 >nulecho Checking Git repository...git status >nul 2>&1if errorlevel 1 ( echo Error: Not a Git repository or Git not installed timeout /t 3 >nul exit /b 1)
:inputset /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 . >nulif errorlevel 1 ( echo Error: Failed to stage files timeout /t 3 >nul exit /b 1)
echo Committing changes...git commit -m %commit_message% >nulif errorlevel 1 ( echo Notice: No changes to commit goto retry)
echo Pushing to remote...git push >nulif errorlevel 1 ( echo Error: Push failed timeout /t 3 >nul exit /b 1)
echo Successfully committed and pushed!timeout /t 3 >nulexit /b 0
:retrychoice /c yn /m "Retry with empty commit? (y/n)"if errorlevel 2 exit /b 1git commit --allow-empty -m %commit_message%goto :eof2. Linux 环境
在目标文件夹内,创建一个新的git-autocommit.sh文件然后输入以下内容:
# Function to check Git statuscheck_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 commitperform_commit() { git commit -m "$1" >/dev/null 2>&1 return $?}
# Main executioncheck_git_repo
# Get commit messageread -r -p "Enter commit message (default: Auto commit): " commit_messageif [ -z "$commit_message" ]; then commit_message="Auto commit at $(date '+%Y-%m-%d %H:%M:%S')"fi
# Stage changesecho "Staging changes..."git add . >/dev/null 2>&1if [ $? -ne 0 ]; then echo "Error: Failed to stage files" >&2 exit 1fi
# Commit changesif ! 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 ;; esacfi
# Push changesecho "Pushing to remote..."git push >/dev/null 2>&1if [ $? -ne 0 ]; then echo "Error: Push failed" >&2 exit 1fi
echo "Successfully committed and pushed!"使用方式:
chmod +x git-autocommit.sh
./git-autocommit.sh