Skip to content

Instantly share code, notes, and snippets.

@slucero
Created December 8, 2015 19:06
Show Gist options
  • Save slucero/32e8c98478d87b555fe4 to your computer and use it in GitHub Desktop.
Save slucero/32e8c98478d87b555fe4 to your computer and use it in GitHub Desktop.
Git command script to locally track all remote branches
#!/usr/bin/env ruby
##############################
# Create local tracking branches for all remote branches
# that don't have local branches with a matching name.
#
# TODO: Add remote name as an agument
# TODO: Add option for pass-through grep arguments to filter branches
##############################
# TODO: Accept this as an argument
REMOTE = 'origin'
# Filter only branches not already existing locally
local_branches = %x(git branch | sed 's/^*/ /' | sort).split
remote_branches = %x(git branch -r | sed 's&#{REMOTE}/&&' | sort).split
untracked_branches = remote_branches - local_branches
# Create a tracking branch for each untracked branch
untracked_branches.each { |branch|
remote_branch = "#{REMOTE}/#{branch}"
puts "Creating a tracking branch for #{branch}:#{remote_branch}"
%x(git branch --track #{branch} #{remote_branch})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment