问题
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.
回答1:
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:$'
.
来源:https://stackoverflow.com/questions/3533470/how-to-forbid-git-commit-if-there-are-untracked-files