61

如果选中,Stash 将启用自动分叉同步: https
://confluence.atlassian.com/display/STASH/Keeping+forks+synchronized它将更新您未修改的分叉中的任何分支。

我一直无法在 GitHub 中找到类似的自动功能;所有的谷歌搜索都在寻找通过本地缓存同步分叉的手动方式。

4

3 回答 3

42

你可以定义一个webhook来监听上游(原始 repo)的变化,并更新你的 fork。

在 2016 年 6 月,您拥有了backstroke.us为您侦听这些事件的服务。无需编写自己的监听器。
1egoman/仰泳

但是,正如chriszo111所评论的那样,在 2020 年,这将是wei/pull

使用probot构建的 GitHub 应用程序,可通过自动拉取请求使您的分叉与上游保持同步。

于 2016-07-03T14:54:27.440 回答
41

作为记录,最近我遇到了同样的问题,并通过 Github Actions 解决了这个问题。解决方案相当简单:计划的操作获取上游存储库并将其合并到我的存储库中。

# .github/workflows/example.yml

name: Merge upstream branches
on:
  schedule:
     # actually, ~5 minutes is the highest
     # effective frequency you will get
    - cron:  '* * * * *'
jobs:
  merge:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Merge upstream
        run: |
          git config --global user.name 'your-name'
          git config --global user.email 'your-username@users.noreply.github.com'

          # "git checkout master" is unnecessary, already here by default
          git pull --unshallow  # this option is very important, you would get
                                # complains about unrelated histories without it.
                                # (but actions/checkout@v2 can also be instructed
                                # to fetch all git depth right from the start)

          git remote add upstream https://github.com/example/test.git
          git fetch upstream

          # Neither forget the -b opt,
          # the feature/x ref is ambiguous at this stage
          git checkout -b feature/x origin/feature/x
          git merge --no-edit upstream/feature/x
          git push origin feature/x

          git checkout master
          git merge --no-edit upstream/master
          git push origin master

          # etc

我每周日运行它,这对我来说绰绰有余。只需将其安排到适合您的任何地方。

此外,同步不同作业中的每个分支可能更明智,因为它们将并行运行,并且如果发生冲突,它们可以独立成功或失败。

如果您需要合并任意数量的分支,您可以参考如何获取所有 Git 分支等问题来找到执行此操作的 shell 技巧。

我注意到存在一个公共行动来通过重新定位来解决这个问题。它看起来很有希望,但它没有记录,所以这里是我的片段。希望能帮助到你!

于 2020-05-03T12:17:22.383 回答
20

您可以创建一个使用 Github API 定期检查上游 repo 的 Github 应用程序。找到更新后,使用 Github API 创建拉取请求,然后调用 updateRef 更新您的分支以匹配 master。

或者,只需安装此 Github App 即可

https://github.com/wei/pull

一个 GitHub 应用程序,可通过上游更改使您的存储库保持最新。

于 2019-02-15T23:50:36.327 回答