Can a Git hook automatically add files to the commit?

前端 未结 10 1581
礼貌的吻别
礼貌的吻别 2020-11-28 05:08

I\'d like to add an automatically generated file to the same commit using a pre- or post-commit hook in Git, dependent on the files that were modified in that commit. How w

10条回答
  •  無奈伤痛
    2020-11-28 05:26

    You could use a combination of a pre and post commit script.

    In the pre-commit:

    • Touch a file .commit or something. (be sure to add this to .gitignore)

    In the post-commit:

    if .commit exists you know a commit has just taken place but a post-commit hasn't run yet. So, you can do your code generation here. Additionally, test for .commit and if it exists:

    • add the files
    • commit --ammend -C HEAD --no-verify (avoid looping)
    • delete .commit file

    This is roughly the process I use to store a .metadata file in the repository generated from metastore.

    If anyone knows a better way I'm all ears but it seems to work for now.

提交回复
热议问题