A Ruby utility to clone a GitHub repository and push it to a new organization using the octokit gem.
- Clone a repository from a source organization or user
- Create a new private repository in the target organization
- Push all branches and tags to the new repository
- Set the default branch (e.g.,
main)
- Ruby (3.x recommended)
- octokit gem
- Git installed and available in your PATH
- A GitHub personal access token with
repoandadmin:orgpermissions
Install dependencies:
gem install octokitOr use Bundler:
bundle install-
Clone the source repository:
git clone https://github.com/source-org/source-repo.git cd source-repo -
Create a new repository in the target organization:
require 'octokit' client = Octokit::Client.new(access_token: ENV['GITHUB_TOKEN']) repo = client.create_repository( 'new-repo-name', organization: 'target-org', private: true ) puts "Created: #{repo.full_name}"
-
Change the remote and push all branches and tags:
git remote add upstream https://github.com/target-org/new-repo-name.git git push --all upstream git push --tags upstream
-
Rename
mastertomainas the default branch:client.rename_branch('target-org/new-repo-name', 'master', 'main')
Run RSpec tests:
rspec spec/git_org_transfer_spec.rb- Ensure your
GITHUB_TOKENenvironment variable is set before running the Ruby scripts. - The script assumes the
mainbranch exists in your repository. - You must have admin rights in the target organization to create repositories.