Skip to content

Instantly share code, notes, and snippets.

@n0nuser
Last active May 22, 2024 09:43
Show Gist options
  • Save n0nuser/3c287939a8548548032e8550d4b8d7a2 to your computer and use it in GitHub Desktop.
Save n0nuser/3c287939a8548548032e8550d4b8d7a2 to your computer and use it in GitHub Desktop.
Bash Script to Push to a New Remote
#!/bin/bash
# SET YOUR VARIABLES HERE
YOUR_HOST="github.com"
YOUR_NEW_HOST="github.com"
YOUR_ORG="github"
YOUR_NEW_ORG="github"
MAIN_BRANCH="main"
# Get the current repository name from the folder name
REPOSITORY=$(basename `pwd`)
# Define the old and new remote URLs
OLD_REMOTE="git@$YOUR_HOST:$YOUR_ORG/$REPOSITORY.git"
NEW_REMOTE="git@$YOUR_NEW_HOST:$YOUR_NEW_ORG/$REPOSITORY.git"
# List the branches in the old remote
branches=$(git branch -r | grep origin/ | sed 's/origin\///')
# Function to change the remote URL
change_remote() {
local remote_url=$1
git remote set-url --push origin $remote_url
}
# Function to push all branches to the new remote
push_branches() {
for branch in $branches; do
# Checkout the branch
git checkout $branch
# Push the branch to the new remote
git push -u origin $branch
done
}
# Save the current remote URL
current_remote=$(git remote get-url origin)
# Change to the new remote URL
change_remote $NEW_REMOTE
# Push all branches to the new remote
push_branches
# Change back to the old remote URL
change_remote $OLD_REMOTE
# Checkout the main branch
git checkout $MAIN_BRANCH
echo "All branches have been pushed to the new remote and the remote URL has been reverted to the old one."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment