在bash上使用Git钩子来检查前缀

时间:2017-07-23 15:55:19

标签: git bash github git-bash githooks

我需要一些bash脚本帮助自动在commit -m消息中添加前缀,它不是服务器端,只是repo,我需要添加消息" User:...&# 34; ,如果用户输入提交消息名称,例如" Jhon" ,它将是用户:Jhon。也许谁可以帮忙为它编写脚本?

1 个答案:

答案 0 :(得分:0)

这就是我要做的事。

  1. 编写一个如下所示的脚本(例如prefix_commit.sh):

    #!/bin/bash
    git commit -m User:"$1"
    

    注意:

    • 使用git commit -m "User:$1"也可以在此处使用。
    1. 使脚本可执行(您只需要执行一次):

      $ chmod +x ./prefix_commit.sh
      
    2. 从命令行调用脚本并传递提交消息,如下所示:

      $ ./prefix_commit.sh 'Sample commit message'
      

      注意:

      • 如果您打算使用多个单词撰写邮件,请务必在提交邮件周围使用单引号
    3. 将参数传递给bash脚本

      • 可以在bash脚本或函数中接收参数:

        $1 # first argument
        $2 # second argument
        $3 # third argument
        
        ### $4, $5, $6, $7 ... etc
        
      • 因此,假设我有一个名为echo_my_args.sh的脚本,它回应了三个参数:

        #!/bin/bash
        echo $1
        echo $2
        echo $3
        
      • 我可以向脚本传递三个参数,然后看到它们回显:

        $ ./echo_my_args.sh 'my first arg' 'my second arg' 'my third arg'
        my first arg
        my second arg
        my third arg
        
      • 再次注意,如果参数必须使用单引号,如果它有多个单词。如果传递一个单词参数,则不需要单引号:

        $ ./echo_my_args.sh first second third
        first
        second
        third
        
      • 以下是有关如何pass arguments to a bash script

      • 的更多信息