Skip to content

Instantly share code, notes, and snippets.

@Pranit-Harekar
Last active March 16, 2020 12:52
Show Gist options
  • Save Pranit-Harekar/6b9e881776ecbfc59da571250fadd06b to your computer and use it in GitHub Desktop.
Save Pranit-Harekar/6b9e881776ecbfc59da571250fadd06b to your computer and use it in GitHub Desktop.
Import fork branches to main repo
#!/usr/bin/env ruby
#################################################################################
# MUST READ: This script imports your fork's branches to main springboardretail
# repository. Please make sure you run this script in a temporary directory
# outside of your local clone directory. In this script, `origin` remote
# points to your fork while `upstream` points to main the repository. Please
# enter your name & fork_url below to get started.
#################################################################################
## Enter your name
name = 'XXXXXXXXXX'
## Enter your fork's SSH or HTTPS URL. Make sure it's your fork's URL!!
fork_url = 'XXXXXXXXXX'
system 'git init'
puts '--- Adding remote for origin & fetching all branches ---'
system "git remote add --fetch origin #{fork_url}"
puts '--- Adding remote for upstream & fetching all branches ---'
system 'git remote add --fetch upstream [email protected]:springboardretail/springboard-retail.git'
puts '--- Creating local branches & setting them to track origin remote ---'
`git branch -r`.lines.map(&:strip).reject { |b| b =~ /^upstream\// }.each do |branch|
new_branch = branch.partition('/')[2]
system "git branch #{new_branch} --track #{branch}"
end
puts '--- Deleting merged branches ---'
system 'git branch --merged upstream/staging | grep -v "\* master" | xargs -n 1 git branch -d'
puts "--- Renaming local branches with '#{name}/' prefix ---"
`git branch`.lines.map(&:strip).each do |branch|
system "git branch -m master #{name}/master" if branch == '* master'
system "git branch -m #{branch} #{name}/#{branch}"
end
puts '--- List all local branches ---'
puts `git branch`.lines.map(&:strip)
puts '--- Pushing all local branches to upstream remote ---'
system 'git push upstream --all'
@Pranit-Harekar
Copy link
Author

How to run it?

  • Make sure you have Ruby installed on your machine
  • Create a temporary directory outside of your springboardretail workspace
  • Copy following script in a file like tmp/script.rb
  • Edit name & fork_url fields in the script and save it
  • Make it executable, chmod +x ~/tmp/script.rb
  • Run it! ./tmp/script.rb

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