Skip to content

Instantly share code, notes, and snippets.

@igorcosta
Created February 22, 2023 00:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorcosta/bcdfcf45c86672d141b64ebb7fc6a537 to your computer and use it in GitHub Desktop.
Save igorcosta/bcdfcf45c86672d141b64ebb7fc6a537 to your computer and use it in GitHub Desktop.
A handy script to shift GitHub repos from one place to another
#!/bin/bash
# Make sure you change this script chmod to executable
# Set your GitHub username and access token
USERNAME={REPLACE_ME}
ACCESS_TOKEN={REPLACE_ME}
# Set the path to the directory where you want to store your repositories
LOCAL_REPO_DIR=/path/to/your/local/repo/directory
# Set the URL of the new server where you want to push the code
NEW_SERVER_URL=git@example.com:newrepo.git
# Iterate through each repository in your GitHub account
for REPO_NAME in $(curl -s -u ${USERNAME}:${ACCESS_TOKEN} https://api.github.com/user/repos\?per_page\=1000 | grep -o '"clone_url":"[^"]*' | sed 's/"clone_url":"//');
do
REPO_DIR=${LOCAL_REPO_DIR}/${REPO_NAME##*/}
# Clone the repository to your local machine
git clone $REPO_NAME $REPO_DIR
# Print a message indicating that the repository has been cloned
echo "Cloned ${REPO_NAME##*/} to $REPO_DIR"
# Change the remote origin to the new server URL
cd $REPO_DIR
git remote set-url origin $NEW_SERVER_URL
# Push the code to the new server
git push --all
git push --tags
# Print a message indicating that the code has been pushed to the new server
echo "Pushed code to $NEW_SERVER_URL"
done
@igorcosta
Copy link
Author

igorcosta commented Feb 22, 2023

About this script

Note: Use it at your own risk

Things to consider:

  • This scripts migrates from GitHub.com to Any git hosting solution.
  • It uses the GitHub public API, so tecnically you can repropuse to use on any vendor.
  • Make sure you understand the GitHub API rate Limit
  • We only shifting bytes from point A to point B ( Code + commit history and tags) If you need more than that, you can reach out to the GitHub Expert services team.

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