Last active
June 8, 2016 06:39
-
-
Save Tsagadai/e71c89e587d25a9a5d633ecf33ca9c41 to your computer and use it in GitHub Desktop.
A simple helper file for migrating between git accounts. It isn't perfect but it works.
This file contains 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
#!/usr/bin/ruby | |
require 'clamp' | |
Clamp do | |
option ["--allbranches","-a"], :flag, "port all branches" | |
option ['-r', '--reponame'], 'reponame', 'Repository name, as a full https url' | |
option ['--forkname', '-f'], 'forkname', 'Repository name, as a full https url', default: 'dotcom' | |
option ['--shrink'], :flag, "Shrink the repo" | |
def execute | |
result = %x(git status 1> /dev/null 2>&1) | |
if $? != 0 | |
puts "Please run in a git repo" | |
fail | |
end | |
branches = allbranches? ? `git branch -a`.split(/\n/).map {|r| r.strip.gsub!(/remotes\/origin\/([a-z0-9_-]*)$/,'\1')}.uniq.reject(&:nil?) : [] | |
# back up config | |
`cp .git/config .git/config_backup` | |
`git fetch origin` | |
`git remote add #{forkname} #{reponame}` | |
branches = branches - ['master'] | |
branches.unshift('master') | |
puts "Converting the following branches: \n#{branches.join("\n * ")}" | |
branches.each do |branch| | |
`git checkout -b #{branch} origin/#{branch}` | |
`git pull` | |
if shrink? | |
`java -jar ~/bfg-1.12.12.jar --strip-blobs-bigger-than 100M .` | |
`git reflog expire --expire=now --all && git gc --prune=now --aggressive` | |
end | |
`git push --force -u #{forkname} #{branch}` | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment