Git 自动化推送更新脚本 🚀 一键自动化推送 Git 仓库更新的高效脚本工具,支持
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}"
# 检查是否为 Git 仓库
if [ ! -d .git ]; then
echo -e "${RED}❌ 当前目录不是 Git 仓库!${NC}"
exit 1
fi
# 获取当前远程 URL
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}"
# 转换为 SSH 协议
if [[ $CURRENT_URL == https://* ]]; then
# HTTPS 转 SSH
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}"
# 检查是否为 Git 仓库
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
|
# 添加到 ~/.bashrc 或 ~/.zshrc
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
|
# 修改 git_push.sh 中的提交信息
COMMIT_MSG="你的自定义提交信息 $(date '+%Y-%m-%d %H:%M:%S')"
|
2. 🎯 选择性添加文件
1
2
3
|
# 修改添加步骤为选择性添加
echo -e "📥 添加变更文件..."
git add -p # 交互式添加
|
3. 🌿 推送到特定分支
1
2
|
# 修改推送命令指定分支
git push origin main # 推送到 main 分支
|
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 密钥配置
ssh -T git@github.com
# 或配置 HTTPS 凭据存储
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 管理大文件
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 压缩
git config --global core.compression 9
git config --global pack.depth 50
|
🔄 十、工作流集成
1. 🤖 结合 CI/CD 流程
1
2
3
4
5
6
7
|
# 在自动化脚本中使用
#!/bin/bash
set -e # 遇到错误立即退出
# 切换协议并推送
bash git_ssh_config.sh
bash git_push.sh
|
2. 🪝 预推送钩子(pre-push hook)
1
2
3
4
|
# 创建 .git/hooks/pre-push
#!/bin/bash
# 运行测试 before 推送
npm test && echo "测试通过,开始推送" || exit 1
|
3. ⏰ 定时自动推送
1
2
|
# 使用 crontab 定时推送
0 18 * * * cd /path/to/repo && /usr/local/bin/git-push
|
🎯 提示:这些脚本非常适合自动化日常推送流程、批量管理多个项目,或者集成到更复杂的 DevOps 工作流中。建议根据团队规范和个人习惯进行适当的定制。
希望这些 Git 自动化推送脚本能大大提高您的工作效率!如有任何问题或建议,欢迎反馈和改进。