Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 217
Milad Khajavi Avatar asked Nov 28 '25 23:11

Milad Khajavi


1 Answers

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:

  • either to add files per pattern
  • or filter only the staged files, as in git add $(git diff --name-only --cached).
like image 111
VonC Avatar answered Dec 01 '25 12:12

VonC