Can a Git hook automatically add files to the commit?

前端 未结 10 1585
礼貌的吻别
礼貌的吻别 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:24

    I had the same need and this approach worked pretty well for me:

    #!/bin/sh
    files='git diff --cached --name-only'
    re=""
    if [[ $files =~ $re ]]
    then
       echo "Creating files"
       create_my_files && git add my_files
    fi
    

    where "create_my_files" should be executable, for example if it is a python file you could execute it as "python create_my_files && git add my_files"

    and is true you don't need a pre-commit to commit again (that would create a infinite nasty loop :p)

提交回复
热议问题