Skip to content

Instantly share code, notes, and snippets.

@atombender
Created June 4, 2012 11:30
Show Gist options
  • Save atombender/2867854 to your computer and use it in GitHub Desktop.
Save atombender/2867854 to your computer and use it in GitHub Desktop.
Git script to change a repo's push/pull URLs to point to another Github owner
#!/usr/bin/env ruby
def run(s)
puts "[#{s}]"
result = `#{s}`
unless $?.exited? and $?.exitstatus == 0
abort "Failed to run: #{s}"
end
result
end
new_owner = ARGV.shift
unless new_owner
abort "Usage: #{$0} <new-owner>"
end
unless run("git remote show -n origin") =~ /Fetch URL: (.*?)$/m
abort "Could not read remote"
end
old_url = $1
unless old_url =~ %r{git@github\.com:([^/]+)/(.*)$}
abort "Remote must be Github path"
end
old_owner = $1
if old_owner == new_owner
abort "Owner is already #{new_owner}"
end
repo_name = $2
new_url = "git@github.com:#{new_owner}/#{repo_name}"
puts "Writing new origin #{new_url}"
run("git remote set-url origin #{new_url}")
run("git remote set-url --push origin #{new_url}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment