A universal Python tool to import any Git repository to GitHub with support for custom configuration including tokens, organizations, branches, and repository names.
- Import from any Git repository (GitHub, GitLab, Bitbucket, custom servers, etc.)
- Support for both user accounts and organizations
- Custom branch selection as default branch
- Custom repository naming
- Automatic dependency checking
- Token validation before import
- Progress feedback and error handling
- Interactive and command-line modes
- Mirror clone (preserves all branches, tags, and history)
- Python 3.6 or higher
- Git
- curl
- GitHub Personal Access Token with repo permissions
- Download the script:
wget https://raw.githubusercontent.com/N1709/git-repo-importer.py
# or
curl -O https://raw.githubusercontent.com/N1709/git-repo-importer.py- Make it executable:
chmod +x git-repo-importer.py- Go to GitHub Settings > Developer settings > Personal access tokens
- Click "Generate new token (classic)"
- Select scopes:
repo(Full control of private repositories)admin:org(if importing to organization)
- Generate and copy the token
Run the script without arguments for interactive mode:
python3 git-repo-importer.py --interactiveYou will be prompted to enter:
- GitHub token
- Target username or organization
- Source repository URL
- Custom default branch (optional)
- Custom repository name (optional)
python3 git-repo-importer.py \
--token YOUR_GITHUB_TOKEN \
--user target-username \
--source https://source-repo.gitpython3 git-repo-importer.py \
--token YOUR_GITHUB_TOKEN \
--user organization-name \
--source https://android.googlesource.com/kernel/common.gitpython3 git-repo-importer.py \
--token YOUR_GITHUB_TOKEN \
--user username \
--source https://source-repo.git \
--branch android-mainlinepython3 git-repo-importer.py \
--token YOUR_GITHUB_TOKEN \
--user username \
--source https://source-repo.git \
--name my-custom-repo-namepython3 git-repo-importer.py \
--token ghp_xxxxxxxxxxxxxxxxxxxx \
--user my-organization \
--source https://android.googlesource.com/kernel/common.git \
--branch android-mainline \
--name android-kernel-mainline| Option | Short | Description | Required |
|---|---|---|---|
--token |
-t |
GitHub personal access token | Yes |
--user |
-u |
Target GitHub username or organization | Yes |
--source |
-s |
Source repository URL | Yes |
--branch |
-b |
Custom default branch | No |
--name |
-n |
Custom repository name | No |
--interactive |
-i |
Run in interactive mode | No |
python3 git-repo-importer.py \
-t ghp_your_token_here \
-u your-username \
-s https://android.googlesource.com/kernel/common.git \
-b android-mainlinepython3 git-repo-importer.py \
-t ghp_your_token_here \
-u sm6125-devs \
-s https://github.com/sm6125-mainline/linux.git \
-b masterpython3 git-repo-importer.py \
-t ghp_your_token_here \
-u your-username \
-s https://gitlab.com/some-project.git \
-n my-forked-projectpython3 git-repo-importer.py \
-t ghp_your_token_here \
-u your-username \
-s https://username:password@private-git-server.com/repo.git- Dependency Check: Verifies Git and curl are installed
- Token Validation: Validates GitHub token with API
- Repository Check: Checks if target repository already exists
- Repository Creation: Creates new repository on GitHub
- Clone Source: Mirror clones the source repository
- Push to GitHub: Pushes all branches and tags to GitHub
- Branch Setup: Sets custom default branch if specified
- Cleanup: Removes temporary files
The tool handles various error scenarios:
- Missing dependencies
- Invalid GitHub token
- Repository already exists (prompts for confirmation)
- Clone failures
- Push failures
- Network errors
- GitHub (public and private)
- GitLab (public and private)
- Bitbucket
- Gitea
- Gogs
- Android AOSP (googlesource.com)
- Custom Git servers
- Any Git repository accessible via HTTP/HTTPS
- Never commit your GitHub token to version control
- Use environment variables for tokens in automation:
export GITHUB_TOKEN=ghp_your_token_here python3 git-repo-importer.py -t $GITHUB_TOKEN -u username -s https://repo.git
- Tokens are only used for GitHub API authentication
- Source repository credentials should be in the URL if needed
Error: Invalid GitHub token. Status code: 401
Solution: Check your token has repo scope and is not expired
Warning: Repository username/repo-name already exists
Do you want to overwrite it? (yes/no):
Solution: Type yes to overwrite or no to cancel
Error: Failed to clone source repository
Solution:
- Check source URL is correct
- Verify you have access to source repository
- Check network connection
Error executing command: git push --mirror
Solution:
- Verify GitHub token has write permissions
- Check repository size limits
- Verify network connection
Create a script file batch-import.sh:
#!/bin/bash
TOKEN="ghp_your_token_here"
USER="your-username"
# Array of repositories to import
repos=(
"https://repo1.git"
"https://repo2.git"
"https://repo3.git"
)
for repo in "${repos[@]}"; do
python3 git-repo-importer.py -t $TOKEN -u $USER -s $repo
doneFor importing only specific branches, modify the push command in the script or use:
# After cloning, before pushing
cd /tmp/git-import-*
git remote add github https://TOKEN@github.com/user/repo.git
git push github refs/heads/main:refs/heads/main
git push github refs/heads/develop:refs/heads/developContributions are welcome. Please ensure:
- Code follows PEP 8 style guide
- All functions have docstrings
- Error handling is comprehensive
- Changes are tested
MIT License - Feel free to use and modify
- Initial release
- Support for user and organization imports
- Custom branch selection
- Custom repository naming
- Interactive and CLI modes
- Comprehensive error handling