Skip to content

Instantly share code, notes, and snippets.

@kparkov
Last active November 16, 2018 19:08
Show Gist options
  • Save kparkov/b3ac121e4be825d5ca3ffb65485253ca to your computer and use it in GitHub Desktop.
Save kparkov/b3ac121e4be825d5ca3ffb65485253ca to your computer and use it in GitHub Desktop.
Copy a list of git repositories from one remote to another
#!/bin/bash
# This pulls a list of repositories from repositories.txt, and pushes each of them to their new location, including all
# branches and tags.
#
# Please note the following:
# - unless the remote is configured to allow a repository to be created when pushing,
# the destination repository should be created and empty.
# - repositories.txt should have a newline at end of file, otherwise it will not process the last
# entry.
# - any source without a specified destination will be ignored during read.
# - this is idempotent. No harm will be done by running it more than once. In fact, any new changes in source will
# be added to the destination.
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
TEMPDIR="$TMP/gitlabrepo"
while IFS=' ' read -r gfrom gto
do
GITSOURCE="${gfrom}"
GITTARGET="${gto}"
if [ -z "$GITTARGET" ]; then
continue
fi
rm -rf $TEMPDIR 2> /dev/null
echo "Cloning $GITSOURCE"
echo "Pushing to $GITTARGET"
git clone $GITSOURCE $TEMPDIR
cd $TEMPDIR
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch --track ${branch#remotes/origin/} $branch
done
git remote add destination $GITTARGET
git pull destination master
git push destination --all || exit 1
git push destination --tags
done <repositories.txt
[email protected]:customer/first.git https://github.com/org/first.git
[email protected]:customer/second.git https://github.com/org/second.git
[email protected]:customer/third.git https://github.com/org/third.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment