Linux Python Go

Git 多仓库推送配置指南

AuthorsAdrain
Published on

在 Git 中配置 post-commit 钩子,可以实现代码提交后自动推送到多个远程仓库(例如 GitHub、GitLab、Gitea)实现代码的简易多重备份,毕竟 github 会封号。

1:配置多个远程仓库

在Git 仓库目录,查看或添加远程仓库:

cd /path/to/your/repository

# 查看现有的远程仓库
git remote -v

# 添加 GitHub 仓库(如果还没有)
git remote add github  [email protected]/你的用户名/仓库名.git

# 添加 GitLab 仓库
git remote add gitlab [email protected]/你的用户名/仓库名.git

# 添加 Gitea 仓库
git remote add gitee [email protected]/你的用户名/仓库名.git

# 验证配置
git remote -v

注意: 推荐使用 SSH,地址格式是 [email protected]:用户名/仓库名.git。

2:创建 post-commit 钩子文件

在 .git/hooks 目录下创建 post-commit 钩子文件:

# 创建钩子文件
nano .git/hooks/post-commit


或者使用其他编辑器:

vim .git/hooks/post-commit
# 或
code .git/hooks/post-commit

3:复制以下脚本内容到文件中

将以下脚本复制并粘贴到 .git/hooks/post-commit 文件中:

#!/bin/bash

# 在后台异步推送,不阻塞提交过程
{
    sleep 2  # 等待一下,避免冲突

    current_branch=$(git symbolic-ref --short HEAD)
    log_file=".git/push.log"

    echo "========================================" >> "$log_file"
    echo "$(date '+%Y-%m-%d %H:%M:%S'): 开始自动推送到多个仓库..." >> "$log_file"
    echo "当前分支: $current_branch" >> "$log_file"
    echo "========================================" >> "$log_file"

    # 推送到 GitHub
    echo "正在推送到 GitHub..." >> "$log_file"
    if git push github "$current_branch" >> "$log_file" 2>&1; then
        echo "✓ GitHub 推送成功" >> "$log_file"
    else
        echo "✗ GitHub 推送失败" >> "$log_file"
    fi

    # 推送到 GitLab
    echo "正在推送到 GitLab..." >> "$log_file"
    if git push gitlab "$current_branch" >> "$log_file" 2>&1; then
        echo "✓ GitLab 推送成功" >> "$log_file"
    else
        echo "✗ GitLab 推送失败" >> "$log_file"
    fi

    # 推送到 Gitee
    echo "正在推送到 Gitee..." >> "$log_file"
    if git push gitee "$current_branch" >> "$log_file" 2>&1; then
        echo "✓ Gitee 推送成功" >> "$log_file"
    else
        echo "✗ Gitee 推送失败" >> "$log_file"
    fi

    echo "$(date '+%Y-%m-%d %H:%M:%S'): 所有推送任务完成" >> "$log_file"
    echo "" >> "$log_file"

} &

echo "✓ 提交成功!后台正在自动推送到远程仓库..."
echo "可以查看 .git/push.log 文件了解推送状态"

保存文件(vim 编辑器中按 i 插入,粘贴后 esc输入 :wq Enter保存退出; nano 编辑器中按 Ctrl+X,然后按 Y,再按 Enter )。

4:添加执行权限

为钩子文件添加执行权限:

chmod +x .git/hooks/post-commit

5:测试钩子

注意 如果是第一次推送到某个远程仓库,可能需要手动推送一次建立跟踪关系

git push -u github main
git push -u gitlab main
git push -u gitee main

创建一个测试提交:

# 修改一个文件或创建测试文件
echo "test" >> test.txt

# 添加到暂存区
git add test.txt

# 提交(这会触发 post-commit 钩子)
git commit -m "测试 post-commit 钩子"

提交后,应该会看到如下输出:

✓ 提交成功!后台正在自动推送到远程仓库... 可以查看 .git/push.log 文件了解推送状态

6:查看推送日志

查看推送日志文件,以确认推送结果:

# 查看推送日志
cat .git/push.log

# 或者实时监控日志
tail -f .git/push.log

日志示例:

========================================
2025-10-10 14:30:45: 开始自动推送到多个仓库...
当前分支: main
========================================
正在推送到 GitHub...
✓ GitHub 推送成功
正在推送到 GitLab...
✓ GitLab 推送成功
正在推送到 Gitee...
✓ Gitee 推送成功
2025-10-10 14:30:52: 所有推送任务完成

常见问题处理

问题 1:如果你只想推送到其中某几个仓库,修改脚本,注释掉不需要的部分:

# 推送到 GitHub
echo "正在推送到 GitHub..." >> "$log_file"
if git push github "$current_branch" >> "$log_file" 2>&1; then
    echo "✓ GitHub 推送成功" >> "$log_file"
fi

# 如果不需要 GitLab,注释掉这部分
# echo "正在推送到 GitLab..." >> "$log_file"
# if git push gitlab "$current_branch" >> "$log_file" 2>&1; then
#     echo "✓ GitLab 推送成功" >> "$log_file"
# fi

问题 2:如果需要添加更多远程仓库,在脚本中添加新的推送块:

# 推送到新的仓库
echo "正在推送到 Bitbucket..." >> "$log_file"
if git push bitbucket "$current_branch" >> "$log_file" 2>&1; then
    echo "✓ Bitbucket 推送成功" >> "$log_file"
else
    echo "✗ Bitbucket 推送失败" >> "$log_file"
fi

问题 3:删除钩子,如果不需要多仓库推送了,就删除或禁用钩子:

# 删除钩子文件
rm .git/hooks/post-commit

# 或者重命名禁用
mv .git/hooks/post-commit .git/hooks/post-commit.disabled

问题 4:为了防止日志文件过大,可以参考下面的命令清空推送日志:

# 清空日志
> .git/push.log

# 或删除日志文件
rm .git/push.log

注意事项

认证问题:确保你已经配置好各个平台的认证(SSH 密钥或 HTTPS 凭据)。

分支名称:脚本会自动检测当前分支,支持 main、master 或其他分支名。

日志文件:日志会不断累积,可以定期清理。