Created
April 2, 2024 08:54
-
-
Save itsdonnix/01c6e2dcc38ecf8acc2c08d1acae47ee to your computer and use it in GitHub Desktop.
Transfer Git Repository
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 | |
# ====== USAGE ====== | |
# | |
# ./tranfer_git_repository.sh <old_git_url> <target_git_url> | |
# | |
# Example: | |
# ./transfer_git_repository.sh https://<access_token>@git.example.com/Organization1/repo-name https://<access_token>@git.example.com/Organization2/repo-name | |
# Script arguments | |
old_repo_url="$1" # URL of the original remote repository (e.g., https://<token>@github.com/user/repo.git) | |
new_repo_url="$2" # URL of the new remote repository (e.g., https://<token>@yourserver.com/user/repo.git) | |
repo_name="${old_repo_url##*/}" # Extract repository name from old URL | |
# Check if required arguments are provided | |
if [ -z "$old_repo_url" ] || [ -z "$new_repo_url" ]; then | |
echo "Error: Please provide both old and new repository URLs as arguments." | |
exit 1 | |
fi | |
# Clone the old repository (bare clone recommended for efficiency) | |
git clone --bare "$old_repo_url" "$repo_name" | |
# Enter the old repository | |
cd "$repo_name"; | |
# Add the new remote repository | |
git remote add new-origin "$new_repo_url" | |
# Push all branches and tags to the new remote repository | |
git push --mirror new-origin | |
# Remove the temporary local repository (if using bare clone) | |
if [ "$(git config --local --get remote.new-origin.url)" == "$new_repo_url" ]; then | |
rm -rf "$repo_name" | |
fi | |
echo "Successfully transferred repository '$repo_name' from '$old_repo_url' to '$new_repo_url'" | |
# Cleanup, remove the old repository | |
rm -rf "$repo_name"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick command to start