Created
May 22, 2024 16:31
-
-
Save iraklisg/88837f58404f01ed9308cef80fe328d1 to your computer and use it in GitHub Desktop.
Script for transferring repositories from Bitbucket to GitHub
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 | |
# Check if the correct number of arguments is provided | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 /path/to/your/bitbucket/repo https://github.com/yourusername/yournewrepo.git" | |
exit 1 | |
fi | |
# Variables | |
BITBUCKET_REPO_PATH=$1 | |
GITHUB_URL=$2 | |
GITHUB_REMOTE="github" | |
BITBUCKET_REMOTE="bitbucket" | |
# Ensure you are in the correct repository directory | |
cd "$BITBUCKET_REPO_PATH" || { echo "Directory $BITBUCKET_REPO_PATH not found"; exit 1; } | |
# Backup the existing .git folder | |
echo "Creating a backup of the existing .git/config file..." | |
cp -r .git/config .git/config.bak | |
echo "Backup git config file created at .git/config.bak." | |
echo "▶️ Cleaning up deleted remote branches and respective local branches..." | |
# Fetch updates from the remote and prune branches that no longer exist on the remote | |
git fetch --prune | |
git remote prune origin | |
# List local branches that are tracking deleted remote branches | |
branches_to_delete=$(git branch -vv | grep ': gone]' | awk '{print $1}') | |
# Delete each local branch that is tracking a deleted remote branch | |
for branch in $branches_to_delete; do | |
echo "" | |
echo "Deleting local branch: $branch" | |
git branch -d "$branch" || git branch -D "$branch" | |
done | |
echo "✓ Cleanup complete." | |
# Fetch all branches from Bitbucket | |
git fetch --all | |
# Add GitHub remote | |
echo "▶️ Pull the latest changes from Bitbucket and push them to GitHub..." | |
git remote add $GITHUB_REMOTE "$GITHUB_URL" | |
# Pull the latest changes for all branches and push them to GitHub | |
for branch in $(git branch -r | grep -v '\->' | sed 's/origin\///g'); do | |
# Ensure the working directory is clean before checking out | |
echo " ⦿ Resetting the working directory for branch $branch..." | |
git reset --hard | |
git clean -fd | |
# Force checkout the branch locally, creating it if it doesn't exist | |
if git show-ref --verify --quiet refs/heads/"$branch"; then | |
echo "Branch '$branch' already exists locally. Force checking out and updating..." | |
git checkout -f "$branch" | |
else | |
echo "Branch '$branch' does not exist locally. Creating and checking out..." | |
git checkout --track origin/"$branch" | |
fi | |
# Pull the latest changes from Bitbucket | |
git pull origin "$branch" | |
# Push the branch to GitHub | |
git push -u "$GITHUB_REMOTE" "$branch" | |
echo "" | |
# Add a small delay to ensure operations complete | |
sleep 2 | |
done | |
echo "✓ All branches have been pushed to GitHub." | |
# Push all tags to GitHub | |
echo "▶️ Pushing tags to GitHub..." | |
git push "$GITHUB_REMOTE" --tags | |
echo "✓ All tags have been pushed to GitHub." | |
echo "▶️ Checking out the default branch $DEFAULT_BRANCH..." | |
# Detect the current default branch name from Bitbucket remote | |
DEFAULT_BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}') | |
# Ensure the working directory is clean before checking out the default branch | |
git reset --hard | |
git clean -fd | |
# Set local default branch as the default branch | |
git checkout -f "$DEFAULT_BRANCH" | |
echo "✓ All branches and tags have been pushed to GitHub. The default branch is $DEFAULT_BRANCH." | |
echo "▶️ Renaming remotes..." | |
# Rename remotes | |
git remote rename origin $BITBUCKET_REMOTE | |
git remote rename $GITHUB_REMOTE origin | |
echo "✓ The remote names have been changed" | |
# Verify the remote URLs | |
git remote -v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment