📌 介绍

在软件开发过程中,我们常常需要将本地的 Git 仓库自动提交并推送到远程仓库,以保持代码或内容的最新状态。本文将介绍如何编写一个适用于 Windows (Git Bash)、macOS 和 Linux 的自动 push 脚本,并详细解析其中的关键点。


📑 目录

  1. 🎯 目标
  2. 跨平台日期格式适配
  3. 🖥 Windows 专用 PowerShell 版本
  4. 🐧 Linux/macOS 专用 Bash 版本
  5. 🔄 Git 推送常见错误及解决方案
  6. 🏆 最佳实践总结

🎯 1. 目标

每天每次执行 自动提交本地修改
✅ 兼容 Windows (Git Bash)macOSLinux
Hugo 站点自动部署,确保 public 目录被正确推送
Git 自动 commit,提交信息为当前日期
✅ 解决 public/ 目录被 .gitignore 忽略 的问题


⚡ 2. 跨平台日期格式适配

由于 Windows Git BashLinux/macOSdate 命令上的实现方式不同,我们需要统一日期格式,确保 Git 提交信息格式正确 (YYYY.MM.DD)。

🔹 解决方案:

# 获取当前日期(跨平台适配)
if [[ "$OSTYPE" == "darwin"* ]] || [[ "$OSTYPE" == "linux-gnu"* ]]; then
    now=$(date +"%Y.%m.%d")   # macOS/Linux 格式:2025.02.06
else
    now=$(date +"%Y.%m.%d")   # Windows(Git Bash 兼容 Linux `date`)
fi

echo "当前日期:$now"

✅ 这样 now 变量在所有平台上都能正确输出

当前日期:2025.02.06

🖥 3. Windows 专用 PowerShell 版本

📌 push.ps1(适用于 Windows PowerShell)

# 🚀 Git 自动 Push 脚本(Windows 专用)
# 允许 PowerShell 运行脚本(首次执行需要管理员权限)
# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# 确保 PowerShell 运行在脚本所在目录
Set-Location -Path (Split-Path -Parent $MyInvocation.MyCommand.Definition)

# 运行 Hugo 生成网站
hugo -F --cleanDestinationDir

# 进入 public 目录
Set-Location -Path ".\public"

# 添加所有文件到 Git
git add -A

# 获取当前日期(格式 YYYY.MM.DD)
$now = Get-Date -Format "yyyy.MM.dd"

# 生成提交信息
$commit_msg = "$now"

# 提交更改
git commit -m "$commit_msg"

# 确保远程分支存在
$branch = "public"
$remote_branches = git branch -r | Out-String
if (-not ($remote_branches -match "origin/$branch")) {
    Write-Host "⚠️ 远程没有 $branch 分支,改用 baseline"
    $branch = "baseline"
}

# 推送到远程仓库
git push -u origin $branch

Write-Host "✅ Commit message: $commit_msg"

📌 运行 PowerShell 版本

.\push.ps1

📌 确保脚本可以执行(仅需执行一次)

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

⚠️ (管理员权限执行,否则 PowerShell 可能禁止运行 .ps1 脚本)


🐧 4. Linux/macOS 专用 Bash 版本

📌 push.sh(适用于 Linux/macOS/Git Bash)

#!/bin/bash

# 🚀 Git 自动 Push 脚本(Linux/macOS/Git Bash 专用)

# 确保脚本在当前目录执行
cd "$(dirname "$0")"

# 运行 Hugo 生成网站
hugo -F --cleanDestinationDir

# 进入 public 目录
cd public || exit 1

# 添加所有文件
git add -A

# 获取当前日期(跨平台适配)
if [[ "$OSTYPE" == "darwin"* ]] || [[ "$OSTYPE" == "linux-gnu"* ]]; then
    now=$(date +"%Y.%m.%d")   # macOS/Linux 格式
else
    now=$(date +"%Y.%m.%d")   # Windows(Git Bash 兼容 Linux `date`)
fi

# 组合提交信息
commit_msg="$now"
git commit -m "$commit_msg"

# 确保远程分支存在
branch="public"
remote_branches=$(git branch -r)
if [[ ! "$remote_branches" =~ "origin/$branch" ]]; then
    echo "⚠️ 远程没有 $branch 分支,改用 baseline"
    branch="baseline"
fi

# 推送到远程仓库
git push -u origin "$branch"

# 显示提交信息
echo "✅ Commit message: $commit_msg"

📌 运行 Bash 版本

chmod +x push.sh  # 仅需执行一次,赋予执行权限
./push.sh

🔄 5. Git 推送常见错误及解决方案

错误信息 原因 解决方案
error: src refspec public does not match any 本地 public 分支不存在或无提交 git checkout -b public && git commit --allow-empty -m "init" && git push -u origin public
fatal: refusing to merge unrelated histories 本地仓库和远程仓库是两个不同的历史 git pull origin main --allow-unrelated-histories
Your local changes would be overwritten by merge 本地文件有未提交的修改 git stash && git pull && git stash pop
public/ is ignored by one of your .gitignore files public/ 目录被 .gitignore 忽略 git add -f publicsed -i '/public/d' .gitignore

🏆 6. 最佳实践总结

Windows 用户 使用 push.ps1macOS/Linux 用户 使用 push.sh
✅ 确保 .gitignore 不会忽略 public/
Git commit 仅包含当前日期,避免过长的提交信息
自动检测远程分支,避免 src refspec does not match any 错误
PowerShell 用户 需执行 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 以允许脚本运行


🎉 现在,你可以一键 push Git 了!

💡 Windows 用户运行:

.\push.ps1

🐧 macOS/Linux/Git Bash 用户运行:

./push.sh

你的 Git 自动提交 & 推送脚本 终于完成了!🚀 🎯