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?
Both the git stash save and push commands work with the --include-untracked and -all switches. However, the git stash save command is deprecated, which means that any new development should only use push.
You can use the pre-push
hook (since git 1.8.2).
Your pre-push hook could check the exit code of git status
, returning 0 (OK to push) if git status
returns non-zero, otherwise returning 1 (do not allow push).
The man page for git-status
says:
If there is no path that is different between the index file and the current HEAD commit (i.e., there is nothing to commit by running git commit), the command exits with non-zero status.
Any repo created with git 1.8.2 or later will have pre-push.sample
in the .git/hooks
directory, which is a useful starting point for implementing your policy. There are more good examples of using the pre-push
hook 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.)
You can use the various hooks (pre-receive, I believe) to determine if the push will break the build and reject it. In addition to that, you should tell your developers to run git status
before any commit or push operation, which is an incredibly sensible rule to have in place and would deter such problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With