Skip to content

Instantly share code, notes, and snippets.

@jlis
Last active May 3, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlis/1be9be73d40633593dc94394106b8ece to your computer and use it in GitHub Desktop.
Save jlis/1be9be73d40633593dc94394106b8ece to your computer and use it in GitHub Desktop.
Push git branches to a new remote
#!/usr/bin/env bash
if [ "$1" == "" ]; then
echo "Usage: ./push_to_new_remote.sh <git folder> <name of the new git remote>"
exit 1
fi
if [ "$2" == "" ]; then
echo "Usage: ./push_to_new_remote.sh <git folder> <name of the new git remote>"
exit 1
fi
cd $1
remote="$2"
for branch in $(git for-each-ref --format='%(refname)' refs/heads/); do
branch="${branch//refs\/heads\//}"
echo "Checking branch '$branch'..."
if git show-branch remotes/$remote/$branch > /dev/null 2>&1; then
echo "Branch $branch already exists on $remote"
echo "Skipping..."
else
while true; do
read -p "Do you wish to push $branch to $remote? (y/n) " yn
case $yn in
[Yy]* )
echo "Pushing branch to remote..."
git push $remote $branch
echo "Done!"
break;;
[Nn]* )
echo "Skipping..."
break;;
* )
echo "Please answer y for yes or n for no.";;
esac
done
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment