问题描述
我在 Gitea 下有一个存储库,在 GitHub 下有另一个存储库。 我需要以一种方式连接这两个:在两个存储库中创建一个专用分支并连接这些分支。例如:
每次当我在 Gitea 中推送提交时,提交都会自动触发一个事件,在 GitHub 分支中推送相同的代码。
如何实现?
解决方法
您可以根据需要更改远程 URL。您可以查看此答案以了解更多信息:How to change the URI (URL) for a remote Git repository?
因此,一种方法是创建一个脚本,在每次推送时执行以下操作:
- 推送到 Gitea 仓库
- 将远程 url 切换到 GitHub 存储库
- 推送到 GitHub 存储库
- 将远程 url 切换回 Gitearepo
请注意,当多人使用该存储库时,这会导致问题。基本上,您必须复制所有 git 操作才能使用两个存储库。
其他方法可能是将 GitHub 用作“备份”存储库来创建重复性作业,该作业将拉取表单存储库 A 并强制推送到存储库 B
,如果我理解正确的话,您的目标是将每个提交从 send-to-github
复制(挑选)到 receive-from-gitea
分支。
我认为有更优雅的方法来实现这一点,但这里有一种快速而肮脏的方式来获得(我假设)你想要的东西。
需要满足一些先决条件,例如 - 已经定义了 2 个远程 URL(例如 origin
和 github
)。
像这样创建自己的脚本:
#!/bin/bash
# First push to gitea
git push origin send-to-github
# Get the currently pushed commit that you just created and pushed
CURR_COMMIT_SHA=$(git rev-parse HEAD)
# Go to your other branch that must go to github
git checkout receive-from-gitea
# Apply the commit so that it is "copied" onto the github branch
git cherry-pick $CURR_COMMIT_SHA
# Push to your github remote repository
git push gitub receive-from-gitea
使其可执行并将其作为别名添加到您的 git 配置中:
[alias]
double-push = "!c() { bash /path/to/script.sh ; }; c"
然后,一旦您准备好提交并推送到您的 Gitea 存储库,而不是使用 git push
推送,请使用您新创建的别名 git double-push
,它应该可以按预期工作。