Skip to content

Instantly share code, notes, and snippets.

@mwlang
Last active February 12, 2020 18:44
Show Gist options
  • Save mwlang/90358b5e76051bc925bac421ff0f29e2 to your computer and use it in GitHub Desktop.
Save mwlang/90358b5e76051bc925bac421ff0f29e2 to your computer and use it in GitHub Desktop.
Easy git branch checkout by Jira ticket
  1. Save jira-co.rb to a folder on your $PATH (for example ~/bin).
  2. chmod +x jira-co.rb to make it executable.
  3. Add git alias to your ~/.gitconfig like so:
[alias]
  co-jira = !sh -c 'jira-co.rb $1' -
  1. Switch to your repo's folder and use it!
>> git co-jira ruby-2379
checking out branch: "RUBY-2379_public_pr_316_reduce_allocations_for_options_hashes"
Switched to branch 'RUBY-2379_public_pr_316_reduce_allocations_for_options_hashes'

NOTE: Only checks out an existing remote or local branch. Does not create a new one!

#!/usr/bin/env ruby
pattern = ARGV[0]
if pattern.to_s == ""
puts "Must supply a pattern for the branch. i.e. RUBY-2379"
exit 1
end
branches = `git branch -a | grep "remotes" | grep -i #{pattern}`.split("\n").map(&:strip)
if branches.size == 0
puts "No branch matched #{pattern.inspect}"
exit 1
end
def shorten_branch_name branch_name
branch_name.split("/").last
end
if branches.size == 1
branch_name = shorten_branch_name branches[0]
puts "checking out branch: #{branch_name.inspect}"
`git checkout #{branch_name}`
else
puts "Too many branches returned! Improve your pattern to match specific branch"
branches.each_with_index do |branch, index|
puts "#{index}: #{branch.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment