Skip to content

Instantly share code, notes, and snippets.

@yorammi
Last active January 11, 2024 07:25
Show Gist options
  • Save yorammi/6e79d2fc276c59aba0ef to your computer and use it in GitHub Desktop.
Save yorammi/6e79d2fc276c59aba0ef to your computer and use it in GitHub Desktop.
Sync 2 remote repositories script - Just define the 3 variables (use export command for that!)
#!/bin/bash
# REPO_NAME=<repo>.git
# ORIGIN_URL=git@<host>:<project>/$REPO_NAME
# REPO1_URL=git@<host>:<project>/$REPO_NAME
rm -rf $REPO_NAME
git clone --bare $ORIGIN_URL
if [ "$?" != "0" ]; then
echo "ERROR: failed clone of $ORIGIN_URL"
exit 1
fi
cd $REPO_NAME
git remote add --mirror=fetch repo1 $REPO1_URL
if [ "$?" != "0" ]; then
echo "ERROR: failed add remote of $REPO1_URL"
exit 1
fi
git fetch origin --tags
if [ "$?" != "0" ]; then
echo "ERROR: failed fetch from $ORIGIN_URL"
exit 1
fi
git fetch repo1 --tags
if [ "$?" != "0" ]; then
echo "ERROR: failed fetch from $REPO1_URL"
exit 1
fi
git push origin --all
if [ "$?" != "0" ]; then
echo "ERROR: failed push to $ORIGIN_URL"
exit 1
fi
git push origin --tags
if [ "$?" != "0" ]; then
echo "ERROR: failed push tags to $ORIGIN_URL"
exit 1
fi
git push repo1 --all
if [ "$?" != "0" ]; then
echo "ERROR: failed push to $REPO1_URL"
exit 1
fi
git push repo1 --tags
if [ "$?" != "0" ]; then
echo "ERROR: failed push tags to $REPO1_URL"
exit 1
fi
@MutexMorgan
Copy link

Perfect

@dereklew
Copy link

Hi

i have tried your solution, however, I am getting the following errors

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I am added new files in both repositories.

@saikrishnareddykarri
Copy link

same issue

@yorammi
Copy link
Author

yorammi commented Apr 28, 2020

The starting point of this script is that the sync is one-directional. If it is not from the start, it won't work.
Seems to me you have changes on the cloned repository that are not in the source repository. This won't work.
I've written this script 5 years ago where forks were not that good in all the Git-hosting services such as GitHub, BitBucket, GitHub and the rest. I suggest that you'll use forking or other more modern sync tools instead of this script which is limited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment