One-way sync 2 git remote repositories

By | April 3, 2020

One day, your project manager decides to move all team project repositories to other Git server and this task is assigned to you. Officially, there is no way for you to transfer your code to the new remote easily. What can you do?

My simple solution is to write my own bash script to handle this job.

#!/bin/bash

function writeHR {
  echo '---------------------'
}

function sync {
  while read line
  do
    writeHR;
    echo "Checking out $line";
    git checkout $line;
    git pull $src $line;
    git pull $dest $line;
    git push $dest $line;
  done;
}

cd $1;
echo "You are currently on realpath ."

src="$2";
dest="$3";

echo "Fetching remote: $src";
git fetch $src;
echo "Fetching remote: $dest";
git fetch $dest;

git branch -r | grep -v '\->' | grep $src/ | sed "s/$src\///g" | sync
  • Save the script above as a file sync.sh, make it executable by chmod-ing +x.
  • Add both source remote and destination remote to your local repositories. Make sure your local repository’s working tree is clean.
  • Run the script like below:
  • ./sync.sh path/to/root/directory/of/repository source-remote-name destination-remote-name
  • Well done! 😀

Note: remember that this is one-way sync 🙂

Category: Git

Leave a Reply