Skip to content

Instantly share code, notes, and snippets.

@Swop
Last active June 4, 2019 11:19
Show Gist options
  • Save Swop/6025459 to your computer and use it in GitHub Desktop.
Save Swop/6025459 to your computer and use it in GitHub Desktop.
Push an entire local Git repository to a new remote repository
#!/bin/bash
# The command must be called inside the Git repository to send to the new repository
# Usage: push-to-new-repo.sh TARGET_REPO_URL
remote=origin;
target_repo=$1;
nb_branches=`git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",$1); print $1}' | wc -l | sed 's/ //g'`;
echo "${nb_branches} branch(es) to push";
cpt=0;
for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",$1); print $1}'`; do
cpt=$(($cpt + 1));
echo "";
echo "----> Branch ${brname} (${cpt}/${nb_branches})..";
git branch --set-upstream $brname $remote/$brname;
done;
echo "";
echo "Add new remote: ${target_repo}";
git remote add target ${target_repo};
echo "Push branches to new remote...";
git push --all target;
echo "Push tags to new remote...";
git push --tags target;
echo "Done!"
echo "The new remote was created under the 'target' name. Feel free to remove the old remote and rename 'target' to 'master'."
# inline shell version (replace the target_repo variable before pasting in your terminal)
remote=origin; \
target_repo="INSERT HERE YOUR TARGET REPO ADDRESS"; \
nb_branches=`git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",$1); print $1}' | wc -l | sed 's/ //g'`; \
echo "${nb_branches} branch(es) to push"; \
cpt=0; \
for brname in `git branch -r | grep $remote | grep -v master | grep -v HEAD | awk '{gsub(/[^\/]+\//,"",$1); print $1}'`; do \
cpt=$(($cpt + 1)); \
echo ""; \
echo "----> Branch ${brname} (${cpt}/${nb_branches}).."; \
git branch --set-upstream $brname $remote/$brname;\
done; \
echo ""; \
echo "Add new remote: ${target_repo}"; \
git remote add target ${target_repo}; \
echo "Push branches to new remote..."; \
git push --all target; \
echo "Push tags to new remote..."; \
git push --tags target; \
echo "Done"\
echo "The new remote was created under the 'target' name. Feel free to remove the old remote and rename 'target' to 'master'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment