Skip to content

Instantly share code, notes, and snippets.

@itsdonnix
Created April 2, 2024 08:54
Show Gist options
  • Save itsdonnix/01c6e2dcc38ecf8acc2c08d1acae47ee to your computer and use it in GitHub Desktop.
Save itsdonnix/01c6e2dcc38ecf8acc2c08d1acae47ee to your computer and use it in GitHub Desktop.
Transfer Git Repository
#!/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";
@itsdonnix
Copy link
Author

Quick command to start

wget https://gist.githubusercontent.com/itsdonnix/01c6e2dcc38ecf8acc2c08d1acae47ee/raw/987a8e6e19aa421ec9d31ba922a5e0e284890c30/transfer_git_repository.sh;
chmod +x transfer_git_repository.sh;
./transfer_git_repository.sh <old_git_url> <target_git_url>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment