Skip to content

Instantly share code, notes, and snippets.

@mislav
Created June 25, 2010 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mislav/453116 to your computer and use it in GitHub Desktop.
Save mislav/453116 to your computer and use it in GitHub Desktop.
Have some commands automatically run when you switch to or from a specific branch

Copy this script to ".git/hooks/post-checkout" in your repository and make it executable:

chmod +x .git/hooks/post-checkout

Tweak it at will. I'm refreshing the bundle and restarting the app server because the Gemfile is different on my "hosted" branch than on other branches.

#!/usr/bin/env ruby -wKU
old, head, flag = ARGV
# your branch name here
target_branch = 'hosted'
if flag == "1" # switching branches
branches = `git name-rev --name-only #{old} #{head}`.split("\n")
if branches.include?(target_branch)
puts 'checking bundle, restarting app ...'
system 'bundle check'
system 'touch tmp/restart.txt'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment