bashgithookshusky

Husky: git commit --amend and push inside a pre-push hook


In order to make a continuous improvement script, inside my pre-push hook, I am trying to add a jest.config.js when it is changed, git commit --amend it, and push it:

git add ${PROJECT_ROOT}/jest.config.js
git commit --amend -C HEAD
git pull
git push --no-verify

However, because this code is in a pre-push hook, that means that the push that should happen afterwards will fail because:

remote: error: cannot lock ref 'my/branch': is at 'updatedHash' but expected 'formerHash'

Is there a recommended way to push inside a pre-push hook?


Solution

  • I was facing the same issue, after playing with it i figured out that we don't need to "re-push" we can just commit with --amend and it will be part of the last commit and will get pushed.

    For example:

    git add ${PROJECT_ROOT}/jest.config.js
    git commit --amend --no-edit
    

    Note that i changed your -C (which is --reuse-message) to --no-edit but i guess you can also use -C in this case, though i'm not sure about the implications.