Kingalione
Kingalione

Reputation: 4275

git pre-commit hook: changed files are not in the current commit

I have a git pre-commit hook which changes some files if they are corrupt. After the hook is done the changed files are not listed in the current commit. How can I stage the changes from the hook into the current commit?

My hook looks like this:

#!/bin/sh
versionUpdater -editVersion

Which opens a windows forms where I can edit some versions from some files. After I finished editing I want that these changes are in the current commit.

Those changes from the hook are now listed in the next commit.

Upvotes: 1

Views: 791

Answers (1)

CodeWizard
CodeWizard

Reputation: 142660

There are several options. the following answer will explain in details what needs to be done and how.

Can a Git hook automatically add files to the commit?.

In the pre-commit:

  • Touch a file .processCommit or something. (be sure to add this to .gitignore)
#!/bin/sh 
echo 
touch .processCommit 
exit

In the post-commit:

if .processCommit exists you know a commit has just taken place.

#!/bin/sh
echo
if [ -a .commit ]
    then
    rm .commit
    git add yourfile
    git commit --amend -C HEAD --no-verify
fi
exit

Upvotes: 2

Related Questions