git 自动保持二级仓库与主仓库同步?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3669636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:04:23  来源:igfitidea点击:

Automatically keep a secondary repo in sync with a primary repo?

gitsynchronizationrepository

提问by George Hawkins

We have a two tier setup.

我们有两层设置。

We have a primary repository (called 'primary' below).

我们有一个主要存储库(下面称为“主要”)。

And a secondary repository (called 'secondary' below) that was created like so:

以及一个像这样创建的辅助存储库(下面称为“辅助”):

$ git clone --bare --shared $REPO_A/primary secondary.git

People working on the secondary repository view the branches which originated from the primary repository as read only but base their own branches off these branches.

在辅助存储库上工作的人们将源自主存储库的分支视为只读,但将他们自己的分支建立在这些分支之上。

We want to sync up the secondary repository with the primary repository once a day.

我们希望每天将辅助存储库与主存储库同步一次。

I.e. we want commits and new branches that were pushed to the primary to become visible to people working off the secondary repository (next time they do a pull).

即,我们希望推送到主存储库的提交和新分支对从辅助存储库工作的人可见(下次他们进行拉取时)。

We do not want this to be symmetric, i.e. activity against the secondary repository will not become visible to those working off the primary repository.

我们不希望这是对称的,即针对辅助存储库的活动不会对主存储库的工作人员可见。

Ideally I'd like to run a cron job that runs on the machine with the bare secondary repository that somehow fetches new data from the primary and automatically includes it into the secondary.

理想情况下,我想运行一个 cron 作业,该作业在带有裸辅助存储库的机器上运行,该作业以某种方式从主存储库获取新数据并自动将其包含到辅助存储库中。

I was hoping there might be a simple way to do this (and I'm hoping someone here will tell me there is).

我希望可能有一种简单的方法来做到这一点(我希望这里有人会告诉我有)。

If I were to write a script to do it, it would do:

如果我要编写一个脚本来执行此操作,它将执行以下操作:

  • Create a fresh clone of the secondary.

    $ git clone $REPO_B/secondary
    $ cd secondary
    
  • Get all its branches.

    $ git branch -r | sed 's?.*origin/??'
    
  • Get all branches in the primary repo.

    $ git ls-remote --heads $REPO_A/primary | sed 's?.*refs/heads/??'
    
  • For each primary branch for which I don't already have a corresponding secondary branch:

    $ git fetch $REPO_A/primary $BRANCHNAME:$BRANCHNAME
    $ git push origin $BRANCHNAME:refs/heads/$BRANCHNAME
    
  • For each primary branch for which I already have a corresponding secondary branch:

    $ git checkout -b $BRANCHNAME --track origin/$BRANCHNAME
    $ git pull $REPO_A/primary $BRANCHNAME
    $ git push
    
  • 创建辅助副本的新克隆。

    $ git clone $REPO_B/secondary
    $ cd secondary
    
  • 获取其所有分支。

    $ git branch -r | sed 's?.*origin/??'
    
  • 获取主仓库中的所有分支。

    $ git ls-remote --heads $REPO_A/primary | sed 's?.*refs/heads/??'
    
  • 对于我还没有相应的辅助分支的每个主要分支:

    $ git fetch $REPO_A/primary $BRANCHNAME:$BRANCHNAME
    $ git push origin $BRANCHNAME:refs/heads/$BRANCHNAME
    
  • 对于我已经有相应的二级分支的每个主要分支:

    $ git checkout -b $BRANCHNAME --track origin/$BRANCHNAME
    $ git pull $REPO_A/primary $BRANCHNAME
    $ git push
    

As I'm new to git I wouldn't be surprised if I've failed to consider certain fundamental issues?

由于我是 git 新手,如果我没有考虑某些基本问题,我不会感到惊讶吗?

And like I said I'm hoping there's a simpler way of doing this, i.e. someone goes "oh, don't do all that, just do...".

就像我说的,我希望有一种更简单的方法来做到这一点,即有人会说“哦,不要做所有这些,做...”。

采纳答案by VonC

Oh, don't do all that, just do:

哦,不要那样做,只要做:

git --bare fetch

;)

;)

(See this old thread for instance)
If you have added the relevant remote originsto your bare repo, you can fetch in turn each of those origins.

(例如,请参阅此旧线程
如果您已将相关的远程源添加到您的裸仓库,您可以依次获取这些源中的每一个。

回答by Dustin

You can just do a git clone --bare --mirrorand periodically do a git fetchto make this happen.

你可以做一个git clone --bare --mirror并定期做一个git fetch来实现这一点。

I do it realtimish using a tool called gitmirrorI wrote in node.js that I run on a machine at home to receive webhooks from github as well as ad-hoc hooks to sync up commits.

我使用我在 node.js 中编写的名为gitmirror的工具来实现它,该工具在家里的机器上运行以从 github 接收 webhooks 以及同步提交的临时钩子。

For a non-github example, I have a repo that's used for a couchdb backup that has a commit about once an hour. The cron job basically comes down to this:

对于非 github 示例,我有一个用于 couchdb 备份的存储库,该备份大约每小时提交一次。cron 工作基本上归结为:

# do some backup stuff
git commit -qam "Backup `date`" >> dump.log 2>&1

From there, I have a post-commit hook (.git/hooks/post-commit) that looks like this:

从那里,我有一个.git/hooks/post-commit像这样的提交后钩子 ( ):

#!/bin/sh
curl -sS http://my.home.machine/gitmirror/bak/repo-name.git

You can accomplish the same thing by pushing from the receiving side. This has the advantage of firing-and-forgetting the payload in the normal case.

你可以通过从接收端推动来完成同样的事情。这具有在正常情况下触发并忘记有效载荷的优点。