Skip to content

Instantly share code, notes, and snippets.

@sevaldes
Last active October 24, 2017 13:40
Show Gist options
  • Select an option

  • Save sevaldes/cc5c141b9d757c643d358c2cbb0511b7 to your computer and use it in GitHub Desktop.

Select an option

Save sevaldes/cc5c141b9d757c643d358c2cbb0511b7 to your computer and use it in GitHub Desktop.
Ruby script to delete local branches not founded on remote.
#!/usr/bin/env ruby
if Gem::Specification.find_all_by_name('git').empty?
puts 'gem "git" not founded'
puts 'Run "gem install git" command to install required gem'
exit
end
require 'git'
require 'logger'
require 'optparse'
options = {
git_binary_path: '/usr/bin/git',
git_project_folder: Dir.pwd
}
parser = OptionParser.new do |opts|
# opts.banner = "Usage: example.rb [options]"
opts.on('-d folder', '--directory') do |d|
options[:git_project_folder] = d
end
opts.on('-b binary', '--git_binary_path') do |d|
options[:git_project_folder] = d
end
end.parse!
Git.configure do |config|
config.binary_path = options['git_project_folder']
end
git = Git.open(options[:git_project_folder])
if git.nil?
puts "The directoy #{options[:git_project_folder]} is not a Git project"
end
# git = Git.open(Dir.pwd, log: Logger.new(STDOUT))
remote_branches = git.branches.remote.collect(&:name)
local_branches = git.branches.local
local_branches.each do |b|
branch_name = b.name
unless remote_branches.include?(branch_name)
b.delete
puts "DELETED \t #{branch_name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment