0

I have the following pre-push hook:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn generatestrings

generatestrings generates 2 .json files under project/src/assets/locales/ pre-push but the generated files are not added and committed, which in turn doesn't push them. This is why I added this next:

 #!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn generatestrings
git add .
git commit -m "Generated translations"

Which works fine but I don't want to add everything, just the JSON files located in the locales folder. This is why I tried this:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn generatestrings
git add ./src/assets/locales/
git commit -m "Generated translations"

But this gives me an error:

Your branch is ahead of 'origin/test' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Anyone has any idea what am I doing wrong?

Onyx
  • 5,186
  • 8
  • 39
  • 86

1 Answers1

0

This is the wrong way to use Git hooks. Don't use a pre-push hook for this purpose. Just run a command that you write that:

  1. builds what you want;
  2. adds and commits it; then
  3. runs git push.

This will work much better. The purpose of a pre-push hook is to verify that you are pushing what you intended and to abort the operation if it's not what is intended. It should never be used to change the operation.

This is much like the purpose of a pre-commit hook, which should only check what is to be committed and not alter what is to be committed. (If you do alter what it to be committed from a pre-commit hook, you should also reject the commit.)

(Note that "should" here is advice rather than requirement. In some hooks, altering files or adding new commits works. In other hooks, it doesn't. When and where and why it works is not entirely predictable and, more importantly, not guaranteed to remain the same in future Git versions. So don't depend on it!)

torek
  • 448,244
  • 59
  • 642
  • 775