Last active
June 3, 2025 12:41
-
-
Save pajcho/245cd6826e3fd200c69b07e42b1e3d4c to your computer and use it in GitHub Desktop.
Migrate repo from dev to main
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 | |
set -e | |
echo "π Starting migration..." | |
# Check if inside a git repo | |
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
echo "β Not a Git repository. Run this inside your local repo folder." | |
exit 1 | |
fi | |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
# Step 1: Detach if current branch is main or dev | |
if [[ "$CURRENT_BRANCH" == "main" || "$CURRENT_BRANCH" == "dev" ]]; then | |
echo "π¦ Detaching from current branch ($CURRENT_BRANCH) to allow cleanup..." | |
git checkout --detach | |
fi | |
# Step 2: Delete old main and dev branches if they exist | |
git branch -D main 2>/dev/null || true | |
git branch -D dev 2>/dev/null || true | |
# Step 3: Update remote URL | |
CURRENT_URL=$(git remote get-url origin) | |
NEW_PATH="github.com:lotusflare/mtn-web.git" | |
NEW_PATH_HTTPS="github.com/lotusflare/mtn-web.git" | |
if [[ "$CURRENT_URL" == git@* ]]; then | |
UPDATED_URL="git@${NEW_PATH}" | |
elif [[ "$CURRENT_URL" == https://* ]]; then | |
UPDATED_URL="https://${NEW_PATH_HTTPS}" | |
else | |
echo "β Unknown remote URL format: $CURRENT_URL" | |
exit 1 | |
fi | |
echo "π Updating remote to: $UPDATED_URL" | |
git remote set-url origin "$UPDATED_URL" | |
# Step 4: Fetch new main | |
echo "π₯ Fetching from remote..." | |
git fetch origin | |
# Step 5: Check out clean main | |
git checkout -b main origin/main | |
echo "β Migration complete. Youβre now on the new 'main' branch." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment