tgonzalez89
tgonzalez89

Reputation: 731

How to keep all branches and tags in sync in a fork or mirror repo?

If I have a repo which is a mirror or a fork of another repo, what's the easiest way to sync all the branches and tags with the original repo?

I want to keep any extra branches that I've created in my private repo.

Upvotes: 2

Views: 1260

Answers (1)

tgonzalez89
tgonzalez89

Reputation: 731

These commands can be used for that purpose. It will override all branches and tags in the repo, so any changes in branches that also exist in the original repo will be lost. However, it won't touch the branches that don't exist in the original repo, so those are safe.

SOURCE_REPO=https://github.com/<user|org_1>/<repo_1>.git
TARGET_REPO=https://github.com/<user|org_2>/<repo_2>.git

git clone $TARGET_REPO
cd <repo_2>

# Sync branches
git remote add src_remote $SOURCE_REPO
git fetch src_remote --quiet
git push origin "refs/remotes/src_remote/*:refs/heads/*" --force

# Sync tags
git tag -d $(git tag -l)
git fetch src_remote --tags --quiet
git push origin --tags --force

This GitHub action can also be used for this purpose: https://github.com/marketplace/actions/github-repo-sync. It's useful if you don't want to do it manually and want to do it on a regular basis. Under the hood, the core commands that do the syncing are the same as the ones above.

Upvotes: 5

Related Questions