Git 自动化推送更新脚本 🚀

一键自动化推送 Git 仓库更新的高效脚本工具,支持 SSH 和 HTTPS 协议,单个项目和批量操作
📋 目录导航
✨ 脚本功能概述
本指南提供完整的 Git 自动化推送解决方案:
- 🔄 协议切换 - 在 SSH 和 HTTPS 协议间灵活切换
- 📤 单个项目推送 - 推送当前 Git 项目的更新
- 📦 批量项目推送 - 递归推送指定目录下所有 Git 仓库的更新
🔄 一、协议切换配置脚本
🔐 切换到 SSH 协议(推荐)
1
| bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_ssh_config.sh)
|
🌐 切换到 HTTPS 协议
1
| bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_https_config.sh)
|


协议切换脚本功能:
- 🔄 自动检测当前远程 URL
- 🌐 在 SSH 和 HTTPS 协议间智能切换
- ✅ 验证新配置是否有效
- 📝 显示详细的切换结果
📤 二、单个项目推送脚本
🚀 完整推送流程(SSH 协议)
1 2
| bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_ssh_config.sh) && \ bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_push.sh)
|
🌐 完整推送流程(HTTPS 协议)
1 2
| bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_https_config.sh) && \ bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_push.sh)
|

单个项目推送脚本功能:
- 🔍 自动检测当前 Git 仓库状态
- 📦 添加所有变更文件到暂存区
- 💬 生成智能提交信息(可自定义)
- 🚀 推送到远程仓库
- 🎨 彩色输出显示每个步骤状态
- ⚡ 错误处理和回滚机制
📦 三、批量项目推送脚本
📂 在 FnOS 终端执行批量推送
1 2
| cd /vol1/1000/home && \ bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_push_all.sh)
|
💻 从 Windows Git Bash 远程执行批量推送
1
| ssh fnos "cd /vol1/1000/home && bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/git/git_push_all.sh)"
|

批量推送脚本功能:
- 🔄 递归搜索指定目录下的所有 Git 仓库
- 📊 显示每个仓库的推送状态汇总
- ⏱️ 显示每个仓库的操作耗时
- 🎯 智能跳过无需推送的仓库
- 📝 生成详细的推送报告摘要
🛠️ 四、脚本源码参考
🔐 协议切换脚本 (git_ssh_config.sh)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #!/bin/bash
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m'
echo -e "${BLUE}🔧 切换到 SSH 协议...${NC}"
if [ ! -d .git ]; then echo -e "${RED}❌ 当前目录不是 Git 仓库!${NC}" exit 1 fi
CURRENT_URL=$(git remote get-url origin 2>/dev/null)
if [ -z "$CURRENT_URL" ]; then echo -e "${RED}❌ 未配置远程仓库!${NC}" exit 1 fi
echo -e "📡 当前远程: ${YELLOW}$CURRENT_URL${NC}"
if [[ $CURRENT_URL == https://* ]]; then SSH_URL=$(echo $CURRENT_URL | sed 's|https://|git@|' | sed 's|/|:|') git remote set-url origin "$SSH_URL" echo -e "${GREEN}✅ 已切换到 SSH 协议: $SSH_URL${NC}" elif [[ $CURRENT_URL == git@* ]]; then echo -e "${YELLOW}⚠️ 已经是 SSH 协议,无需切换${NC}" else echo -e "${RED}❌ 不支持的协议格式${NC}" exit 1 fi
|
🚀 单个项目推送脚本 (git_push.sh)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #!/bin/bash
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m'
echo -e "${CYAN}🚀 开始 Git 推送流程...${NC}"
if [ ! -d .git ]; then echo -e "${RED}❌ 当前目录不是 Git 仓库!${NC}" exit 1 fi
REPO_NAME=$(basename -s .git $(git config --get remote.origin.url)) CURRENT_BRANCH=$(git branch --show-current) REMOTE_URL=$(git remote get-url origin)
echo -e "📦 仓库: ${GREEN}$REPO_NAME${NC}" echo -e "🌿 分支: ${YELLOW}$CURRENT_BRANCH${NC}" echo -e "🌐 远程: ${BLUE}$REMOTE_URL${NC}" echo -e "${BLUE}========================================${NC}"
if [ -z "$(git status --porcelain)" ]; then echo -e "${YELLOW}⚠️ 没有需要提交的变更${NC}" exit 0 fi
echo -e "📋 变更文件:" git status -s
echo -e "${BLUE}========================================${NC}" echo -e "📥 添加变更文件..." git add .
COMMIT_MSG="Update: $(date '+%Y-%m-%d %H:%M:%S')" echo -e "💬 提交信息: ${YELLOW}$COMMIT_MSG${NC}" if git commit -m "$COMMIT_MSG"; then echo -e "${GREEN}✅ 提交成功${NC}" else echo -e "${RED}❌ 提交失败${NC}" exit 1 fi
echo -e "${BLUE}========================================${NC}" echo -e "🚀 推送到远程仓库..." if git push origin $CURRENT_BRANCH; then echo -e "${GREEN}✅ 推送成功完成!${NC}" else echo -e "${RED}❌ 推送失败!${NC}" exit 1 fi
echo -e "${GREEN}🎉 所有操作完成!${NC}"
|
⚙️ 五、安装和自定义配置
1. 📥 本地安装脚本
1 2 3 4 5 6 7 8 9 10
| curl -o ~/git_ssh_config.sh https://gitee.com/meimolihan/script/raw/master/sh/git/git_ssh_config.sh curl -o ~/git_https_config.sh https://gitee.com/meimolihan/script/raw/master/sh/git/git_https_config.sh
curl -o ~/git_push.sh https://gitee.com/meimolihan/script/raw/master/sh/git/git_push.sh curl -o ~/git_push_all.sh https://gitee.com/meimolihan/script/raw/master/sh/git/git_push_all.sh
chmod +x ~/git_*.sh
|
2. 🔤 创建便捷命令别名
1 2 3 4
| alias git-push-ssh='bash ~/git_ssh_config.sh && bash ~/git_push.sh' alias git-push-https='bash ~/git_https_config.sh && bash ~/git_push.sh' alias git-push-all='bash ~/git_push_all.sh'
|
3. 📁 全局安装到系统
1 2 3 4 5 6 7 8
| sudo mv ~/git_ssh_config.sh /usr/local/bin/git-ssh-config sudo mv ~/git_https_config.sh /usr/local/bin/git-https-config sudo mv ~/git_push.sh /usr/local/bin/git-push sudo mv ~/git_push_all.sh /usr/local/bin/git-push-all
git-ssh-config && git-push
|
🔧 六、高级用法和自定义
1. 💬 自定义提交信息
1 2
| COMMIT_MSG="你的自定义提交信息 $(date '+%Y-%m-%d %H:%M:%S')"
|
2. 🎯 选择性添加文件
1 2 3
| echo -e "📥 添加变更文件..." git add -p
|
3. 🌿 推送到特定分支
4. ⚡ 强制推送(谨慎使用)
1 2
| git push --force-with-lease origin $CURRENT_BRANCH
|
🚨 七、安全注意事项
- 🔐 权限验证:确保有推送权限,特别是强制推送
- 🔍 代码审查:推送前检查变更内容
- 💾 备份重要代码:重要变更建议先创建备份
- 🛡️ 分支保护:避免直接推送到受保护的分支
- 🔒 敏感信息:确保不推送敏感信息和配置文件
🔍 安全检查命令
1 2 3 4 5
| git log --oneline origin/main..HEAD
git diff --staged
|
💡 八、常见问题解答
❓ 推送权限被拒绝
解决方案:
1 2 3 4 5
| ssh -T git@github.com
git config --global credential.helper store
|
❓ 冲突无法推送
解决方案:
1 2 3 4 5
| git pull --rebase origin main
git push origin main
|
❓ 远程仓库不存在
解决方案:
1 2 3 4 5
| git remote add origin git@github.com:username/repo.git
git push -u origin main
|
❓ 大文件推送失败
解决方案:
1 2 3 4 5 6
| git lfs track "*.psd" git add .gitattributes git add file.psd git commit -m "Add large file" git push origin main
|
📊 九、性能优化建议
- ⚡ 分批推送:大量变更时分多次推送
- 🔐 使用 SSH:SSH 协议通常比 HTTPS 更快
- 🗜️ 压缩数据:启用 Git 压缩功能
- 📉 浅层推送:对于大型仓库考虑浅层操作
- 🔀 并行操作:批量脚本可优化为并行执行
🗜️ 启用压缩配置
1 2 3
| git config --global core.compression 9 git config --global pack.depth 50
|
🔄 十、工作流集成
1. 🤖 结合 CI/CD 流程
1 2 3 4 5 6 7
|
set -e
bash git_ssh_config.sh bash git_push.sh
|
2. 🪝 预推送钩子(pre-push hook)
1 2 3 4
|
npm test && echo "测试通过,开始推送" || exit 1
|
3. ⏰ 定时自动推送
1 2
| 0 18 * * * cd /path/to/repo && /usr/local/bin/git-push
|
🎯 提示:这些脚本非常适合自动化日常推送流程、批量管理多个项目,或者集成到更复杂的 DevOps 工作流中。建议根据团队规范和个人习惯进行适当的定制。
希望这些 Git 自动化推送脚本能大大提高您的工作效率!如有任何问题或建议,欢迎反馈和改进。