Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent git push if local modifications are detected (including untracked files)?

Tags:

git

git-push

push

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?

like image 583
Clintm Avatar asked May 14 '10 00:05

Clintm


People also ask

Can you git push with untracked files?

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.


2 Answers

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.)

like image 123
Matt Curtis Avatar answered Oct 31 '22 16:10

Matt Curtis


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.

like image 37
Michael Aaron Safyan Avatar answered Oct 31 '22 17:10

Michael Aaron Safyan