Can forks be synced automatically in GitHub?

GithubGit Fork

Github Problem Overview


Stash enables automatic fork syncing if selected: https://confluence.atlassian.com/display/STASH/Keeping+forks+synchronized
It will update any branches in your fork that you haven't modified.

I've been unable to find similar automatic functionality in GitHub; all google searches are turning up manual ways to sync forks via your local cache.

Github Solutions


Solution 1 - Github

For the record, recently I ran into this same problem and addressed it with Github Actions. The solution is rather easy: an scheduled action fetches the upstream repository and merges it into mine.

# .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

I run it every Sunday which is more than enough for me. Just schedule this to whatever is fine for you.

Also, it is probably wiser to sync every branch in a different job since they will run in parallel and can independently succeed or fail if conflicts occur.

If you need to merge an arbitrary number of branches, you can refer to questions like https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches to find shell tricks to do it.

I noticed a public action exists to address this by rebasing. It looked promising but it was fairly undocumented so here is my snippet instead. Hope it helps!

Solution 2 - Github

You could define a webhook to listen to upstream (the original repo) changes, and update your fork.

In June 2016, you had the service backstroke.us which listens to those events for you. No need to write your own listener.
See 1egoman/backstroke

But, as commented by chriszo111, in 2020, that would be wei/pull

> a GitHub App built with probot that keeps your forks up-to-date with upstream via automated pull requests.

Solution 3 - Github

You can create a Github App that use Github API to check the upstream repo periodically. Once an update is found, use Github API to create a pull request then call updateRef to update your branch to match master.

Or, just install this Github App that does exactly that

https://github.com/wei/pull

烙 a GitHub App that keeps your repository up-to-date with upstream changes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMalinaView Question on Stackoverflow
Solution 1 - GithubN1nguView Answer on Stackoverflow
Solution 2 - GithubVonCView Answer on Stackoverflow
Solution 3 - GithubddhhzView Answer on Stackoverflow