Tianyi Cui
Tianyi Cui

Reputation: 4073

How to forbid git commit if there are untracked files?

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.

Upvotes: 4

Views: 545

Answers (1)

Mark Peters
Mark Peters

Reputation: 81084

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:$'.

Upvotes: 4

Related Questions