How to sync two folders in two separate GitHub repositories

Say you have a repo A, in which you want to duplicate some content with repo B.

How to sync two folders in two separate GitHub repositories

The Problem

I have a personal website, stored in a GitHub repo (and hosted via GitHub pages), as well as a lab website (a “company” website, if you will). Both are fairly similar, as they are built using Wowchemy’s academic theme. Importantly, there is a blog in the company websites with posts, but I have one on my personal website too. What I would like is that every time I post something on my website, it gets automatically copied over to the company website. So that I don’t have to manually maintain the content at two separate places.

The Solution

  1. The first step is to go to the settings of your GitHub account, to developers settings, and to personal access tokens. You have to generate a token, and tick the repo authorizations. Copy-paste the key.
  2. Go to the settings of the personal website repo (the source from which the content will be copied), to “Secrets”, and add a new secret called “API_TOKEN_GITHUB” (with the key you just copied).
  3. Create a new GitHub action workflow such as this one. The things to change are the source_file, destination_repo and destination_folder.

Tada 🎉 Everytime I push to my personal repo, the new content of one of the subfolder gets copied to another repo.

Notes

  • This is a one-way sync, so updates on the target repo won’t affect the source repo (but might get overridden!).
  • if you want to preserve the original commit message, set commit_message: ${{ github.event.head_commit.message }} (thanks @dobbelina)

Thanks for reading! Do not hesitate to share or tweet this post, or leave a comment below 🤗

Dominique Makowski
Dominique Makowski
Lecturer in Psychology

Trained as neuropsychologist and CBT psychotherapist, I am currently working as a lecturer at the University of Sussex, on the neuroscience of reality perception.