tags:

views:

157

answers:

5

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?

A: 

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.

Michael Aaron Safyan
A: 

To automatically add changes, you might want to look into using the -a flag. From the git-commit man page:

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told git about are not affected.

There doesn't seem to be any flags for git commit that adds untracked files. Remembering to do a git add . before the commit is the best solution I can think of.

Brian McKenna
Remember though that "git add ." only adds files that are under or on the same level that the command is run
Gonçalo Queirós
+4  A: 

There's not a git hook for this.

You could write a shell script to replace push that checks for local modifications and refuses to push if they exist, and then tell your team to use that. You could even go so far as to rename git-push so they don't call it by accident.

I agree with @Clint's comment though, generally you should solve issues like that with policy and training. Continuous integration will also help. People who are sloppy with commits will always find a way to be sloppy, and accidents will always happen.

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.
Matt Curtis
A: 

Maybe put an alias, either in your shell or git config, that replaces the default push command with a custom script that does git status first and checks for "working directory clean"? I don't know if overwriting push is possible, or if it would make it so that you couldn't then call the real push. Just an idea off the top of my head, so I don't have any idea whether it really works.

Tesserex
You can't use an alias to override a built-in command. A shell alias can't contain a space, so you're pretty much back to writing your own command.
Jefromi
A: 

I ended up adding an ant target to our local build before pushing to our main repo... here's the target... in case someone else was looking for a step in the right direction.

Thanks to all who answered.

<target name="git-status-check">
    <echo>Performing git working directory check for local modifications or untracked files.</echo>
    <exec executable="git" failifexecutionfails="true"
        outputproperty="git.check">
        <arg value="status"/>
    </exec>
    <echo>${git.check}</echo>
    <propertyregex property="dirty.working.dir" input="${git.check}" regexp="working directory clean" 
        select="\1" casesensitive="false" />
    <fail message="Git status reports that you have local modifications or untracked changes in your working dir... did you forget to commit these changes? ${line.separator} ">
        <condition>
            <not>
                <isset property="dirty.working.dir" />
            </not>
        </condition>
    </fail>
    <echo>Git status reported a clean working dir continuing build...</echo>
</target>
Clint Modien
@Clint: Did my answer helped you/answer your question?
Matt Curtis