views:

67

answers:

1

Sometimes, it makes sense to forbid git commit when there are untracked files, because you should add them either to files to commit or gitignore. And an option like -f to force this commit is also necessary. So is there any option/plugin/etc to do that? Thank you very much.

+2  A: 

You can add a pre-commit hook that exits with a non-zero value if you find untracked files (you could examine the output of git status --porcelain -u to determine their presence--look for lines beginning with "?"). Then you can override the commit verification with git commit --no-verify if you don't care about the untracked files.

Edit: Thanks to @Jefromi; I don't have my git environment at hand to test. He also notes that this command should identify the presence of untraced files: git status -u | grep '^# Untracked files:$'.

Mark Peters
I think you want to examine the output of `git status --porcelain -u`, not `git status -u`. All the `-u` does is make sure that untracked files will be listed, which they will be anyway unless it's turned off in the config. The `--porcelain` is what makes it easy to parse with a script, with the `??` at the beginning of the line, instead of you having to read and find the "Untracked files" section. That option was added in v1.6.4.
Jefromi
Of course, if all you need to know is if there are untracked files, just do `git status -u | grep '^# Untracked files:$'`
Jefromi