Created
July 18, 2016 01:53
-
-
Save SyllaJay/a3d14f5e0a2920be34932f82b1e485c9 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# This helps you migrate to new repo from origin | |
USAGE_INFO="Usage: `basename $0` URL-New-Remote" | |
USAGE_EXAMPLE="Example: `basename $0` git@github.com:user/repo.git" | |
# Check in usage | |
case $# in | |
0) | |
echo -e "${USAGE_INFO}" | |
echo -e "${USAGE_EXAMPLE}" | |
exit 1 | |
;; | |
esac | |
if [ $# -gt 1 ] | |
then \ | |
echo "Fatal: wrong parameters!" | |
echo -e "${USAGE_INFO}" | |
echo -e "${USAGE_EXAMPLE}" | |
exit 1 | |
fi | |
# Define remote | |
URL_NEW_ORIGIN=$1 | |
ORIGIN=origin | |
echo -e "Migrate to repo ${URL_NEW_ORIGIN} from ${ORIGIN}...\n" | |
### Step 1. Make sure you have a local copy of all "old repo" | |
### branches and tags. | |
# Fetch all of the remote branches and tags: | |
echo -e "git fetch -p ${ORIGIN}" | |
git fetch -p ${ORIGIN} | |
# View all "old repo" local and remote branches: | |
echo -e "git branch -a" | |
git branch -a | |
# If some of the remotes/ branches doesn't have a local copy, | |
# checkout to create a local copy of the missing ones: | |
# git checkout -b <branch> origin/<branch> | |
# 1. grep env, make sure that branches belong to only one remote! | |
# for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done | |
for remote in `git branch -r | grep -v /HEAD`;do | |
local=${remote#"${ORIGIN}/"} | |
echo "git branch --track ${local} ${remote}" | |
git branch --track ${local} ${remote} | |
done | |
# 2. no grep env | |
# for remote in `git branch -r`; do git branch --track $remote; done | |
#for remote in `git branch -r`;do | |
# if [[ ${remote} == *"${ORIGIN}"* ]] && [[ ${remote} != *"HEAD"* ]] && [[ ${remote} != "->" ]]; then | |
# local=${remote#"${ORIGIN}/"} | |
# echo "git branch --track ${local} ${remote}" | |
# git branch --track ${local} ${remote} | |
# fi | |
#done | |
# Update the branches, assuming there are no changes on your local tracking branches: | |
# for remote in `git branch -r`;do git checkout ${remote} ; git pull;done | |
# Now we have to have all remote branches locally. | |
## Step 2. Add a "new repo" as a new remote origin: | |
NEW_ORIGIN=new-origin | |
echo -e "git remote add ${NEW_ORIGIN} ${URL_NEW_ORIGIN}" | |
git remote add ${NEW_ORIGIN} ${URL_NEW_ORIGIN} | |
### Step 3. Push all local branches and tags to a "new repo". | |
# Push all local branches (note we're pushing to new-origin): | |
echo -e "git push --all ${NEW_ORIGIN}" | |
git push --all ${NEW_ORIGIN} | |
# Push all tags: | |
echo -e "git push --tags ${NEW_ORIGIN}" | |
git push --tags ${NEW_ORIGIN} | |
# Check command status | |
if [ $? -ne 0 ]; then | |
echo -e "\n" | |
echo -e "exit!" | |
exit 1 | |
fi | |
### Step 4. Remove "old repo" origin and its dependencies. | |
# View existing remotes | |
echo -e "\nRename remote..." | |
echo -e "git remote -v" | |
git remote -v | |
# Remove "old repo" remote: | |
echo -e "git remote rm ${ORIGIN}" | |
git remote rm ${ORIGIN} | |
# Rename "new repo" remote into 'origin': | |
echo -e "git remote rename ${NEW_ORIGIN} ${ORIGIN}" | |
git remote rename ${NEW_ORIGIN} ${ORIGIN} | |
# Show remote status | |
echo -e "\nMigrating completed!" | |
git remote -v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment