Skip to content

Instantly share code, notes, and snippets.

@shostakovich
Last active December 14, 2015 15:49
Show Gist options
  • Save shostakovich/5110413 to your computer and use it in GitHub Desktop.
Save shostakovich/5110413 to your computer and use it in GitHub Desktop.
Pre-push hook that rejects force pushes to master. Requires Git 1.8.2 and above.
[push]
default = current
[alias]
pfush = "push --force"
#!/usr/bin/env ruby
# Pre-push hook that rejects force pushes to master.
# Requires Git > 1.8.2
class PushRejecter
def run
if pushing_to?(:master) and forced_push?
reject
else
allow
end
end
def pushing_to?(branch)
`git branch | grep "*" | sed "s/* //"`.match(branch.to_s)
end
def forced_push?
`ps -ocommand= -p #{Process.ppid}`.match(/force|pfush/)
end
def reject
puts "Force push to master rejected."
exit(1)
end
def allow
exit(0)
end
end
PushRejecter.new.run
@pixelhandler
Copy link

Thanks for the pre-push script very helpful, I wanted to use bash instead of ruby so based on this created https://gist.github.com/pixelhandler/5718585

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment