Created
July 19, 2013 17:24
-
-
Save kvangork/6040875 to your computer and use it in GitHub Desktop.
Script to move a git repo from one remote URL to another. Based on my fork of https://gist.github.com/markrickert/2919901
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Takes two parameters: origin and destination remote git repository URLs. | |
# | |
# This is the stuff this script does: | |
# | |
# 1. Clones the origin repository | |
# 2. Fetches all remote branches | |
# 3. Backs everything up to a tarball | |
# 4. Pushes all branches to the destination repository | |
# 5. Removes all files in the main branch from the origin repository | |
# 6. Writes a readme file with the new repo URL and pushes to the origin | |
# 7. Deletes the cloned working folder. | |
# | |
# Your origin remote repository's history is left untouched by this script. | |
if [ "$1" = "" ] | |
then | |
echo "Usage: $0 <origin repository clone URL> <destination repository clone URL>" 1>&2 | |
exit 1 | |
fi | |
# Variable definitions | |
ORIGINURL=$1 | |
DESTURL=$2 | |
ORIGINNAME=${ORIGINURL##*/} | |
FOLDERNAME=${ORIGINNAME%.git} | |
TARNAME="$FOLDERNAME.gitarchive.$(date +%Y%m%d).tgz" | |
# Clone the repos and go into the folder | |
git clone --recursive $ORIGINURL $FOLDERNAME | |
cd $FOLDERNAME | |
BRANCHNAME="$(git symbolic-ref --short -q HEAD)" | |
# Pull all branches | |
git branch -r | grep -v HEAD | grep -v $BRANCHNAME | while read branch; do | |
git branch --track ${branch##*/} $branch | |
done | |
# Pull all remote data and tags | |
git fetch --all | |
git fetch --tags | |
git pull --all | |
git gc # Cleanup unnecessary files and optimize the local repository | |
# Create an archive of the directory | |
cd ../ | |
tar cfz "$TARNAME" "$FOLDERNAME/" | |
# now we can safely mess with it | |
cd $FOLDERNAME | |
# push everything to destination url | |
git remote add destination $DESTURL | |
git push destination --all | |
git push destination --tags | |
git remote rm destination | |
# replace all branches with destination URL README files | |
git branch -r | grep -v HEAD | while read branch; do | |
git checkout ${branch##*/} | |
# remove normal files. I wish these could be just one command. | |
git rm -r * | |
# remove dotfiles | |
git rm -r *.* | |
echo "Moved to $DESTURL" > README | |
git add -A | |
git commit -m "Replace README with new repo URL" | |
done | |
git push origin --all | |
# Remove the git clone | |
cd ../ | |
rm -rf "./$FOLDERNAME" | |
echo "Done!" | |
echo "The repository at $ORIGINURL has been moved to $DESTURL" | |
echo "Your archived git repository is named $TARNAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment