Search code examples
gitgithookspre-commit-hookpre-commit

Staging again files during pre-commit hook?


I have installed mvn java-formatter:format task in pre-commit hook. This task will format the java source codes.

pushd src/ > /dev/null

mvn java-formatter:format
RETVAL=$?
if (($RETVAL == 1)); then
        exit 1
fi

popd > /dev/null

I need to append newly formatted source files to commit. How can I do this?


Solution

  • The pre-commit hook should be able to include a git add, to modify the index about to be committed.

    See for example "git pre-commit hook, add file into index".

    Try a git add -A :/. (see "Add as if from the root folder of the repository")

    If you need to add only the files which were already staged, then you need: