Sometimes someone on our team does a git push and breaks the build because his local build works but he has forgotten to commit all his local modifications and untracked files to git before he pushes.
I’d like to prevent this… I poured over the docs for an hour or so today and couldn’t find anything built in.
Does anyone have any solutions?
You can use the
pre-pushhook (since git 1.8.2).Your pre-push hook could check the exit code of
git status, returning 0 (OK to push) ifgit statusreturns non-zero, otherwise returning 1 (do not allow push).The man page for
git-statussays:Any repo created with git 1.8.2 or later will have
pre-push.samplein the.git/hooksdirectory, which is a useful starting point for implementing your policy. There are more good examples of using thepre-pushhook here: http://blog.ittybittyapps.com/blog/2013/09/03/git-pre-push/Be aware that the hook does not run on the upstream repo. Each clone would need to have this hook installed, to enforce your policy. (Hooks are not cloned as part of the repository. As hooks are executed by git, this design prevents malicious hooks running on a developer’s machine. Malicious code should instead go into a Makefile or configure script, which developers run without looking.)