Is it possible to automatically split a commit for

2019-09-03 20:21发布

问题:

At our company code reviews are done in Github compare view by adding comments. Of course you can use difftool or someting. But I'd like to know if there is a way to automatically warn / split a commit when it exceeds the Github limits?

回答1:

You can use a pre-commit hook to prevent large commits. E.g. to check the diff's number of lines, save the following as [REPO PATH]/.git/hooks/pre-commit and make it executable (e.g. chmod +x on linux):

#!/usr/bin/env bash
[[ $(git diff --cached | wc -l) > 300 ]] && { echo "Commit too long"; exit 1; }

or check file size:

tmp=$(mktemp /tmp/git_XXXXX)
git diff --cached > "$tmp"
[[ $(ls -l "$tmp" | awk '{print $5}') > 10000 ]] && { echo "Commit too large"; exit 1; }