fork 可以在 GitHub 中自动同步吗?

问题描述 投票:0回答:5

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

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

github git-fork
5个回答
53
投票

郑重声明,最近我遇到了同样的问题,并通过 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 '[email protected]'

          # "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 技巧来完成此操作。

我注意到有一项公共行动通过变基来解决这个问题。它看起来很有希望,但它相当没有记录,所以这里是我的片段。希望有帮助!


48
投票

您可以定义一个 webhook 来监听上游(原始存储库)的更改,并更新您的分支。

2016 年 6 月,您拥有了服务

backstroke.us
,它可以为您侦听这些事件。无需编写自己的侦听器。
请参阅1自负/仰泳

但是,正如 chriszo111 评论,到 2020 年,这将是

wei/pull

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


27
投票

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

或者,只需安装这个 Github 应用程序即可实现此功能

https://github.com/wei/pull

🤖 一个 GitHub 应用程序,可让您的存储库与上游更改保持同步。


3
投票

我有一个带有 GitHub Action 的 sync-fork Gist,可以使用 GitHub CLI 使存储库保持最新状态。这就是现在的样子(为了完整性):

name: sync-fork
on:
  schedule:
    - cron: '0 0 * * *'
  workflow_dispatch: { }
jobs:
  sync:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - run: gh repo sync $REPOSITORY -b $BRANCH_NAME
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPOSITORY: ${{ github.repository }}
          BRANCH_NAME: ${{ github.ref_name }}

0
投票

通常您不想向上游添加额外的工作流程。您可能想在另一台服务器或其他 REPO 操作上运行 crone 作业,然后使用您的 GitHub PAT

name: sync-fork
on:
  schedule:
    - cron: '0 0 * * *'
  workflow_dispatch: { }
jobs:
  sync:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - run: gh repo sync your_forked_repo -b branch_you_want_to_sync
        env:
          GITHUB_TOKEN: ${{ secrets.YOUR_PAT }}
© www.soinside.com 2019 - 2024. All rights reserved.