Last active
March 16, 2020 12:52
-
-
Save Pranit-Harekar/6b9e881776ecbfc59da571250fadd06b to your computer and use it in GitHub Desktop.
Import fork branches to main repo
This file contains hidden or 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/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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to run it?
tmp/script.rbname&fork_urlfields in the script and save itchmod +x ~/tmp/script.rb./tmp/script.rb