1. 程式人生 > >git commit 時檢查comment訊息格式

git commit 時檢查comment訊息格式


http://blog.csdn.net/crazyzhang1990/article/details/41865179

之前大家普遍遇到在本地commit 時,由於comment訊息格式寫錯,導致無法push的情況。

有一個策略,可以避免這種困難: 就是我們在commit時,就立刻檢查comment訊息格式,如果不符合,就無法commit。相當於提前檢查訊息格式。

所以需要大家在本地做如下操作:

  1. 進入到本地 ios-yidao-user 目錄
  2. cd .git/hooks
  3. cp commit-msg{.sample,}
  4. 編輯 commit-msg, 把裡面的內容全部刪掉,替換成如下內容:
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

$regex = /^refs\s#[0-9]+,.*\S+.*/

if !$regex.match(message)
  puts "Error: -----------------------------------------" 
  puts " Your commit message is not formatted correctly." 
  puts " It must contain an issue number." 
  puts " Example:" 
  puts "      refs #99999, Added some new feature" 
  puts "------------------------------------------------" 
  exit 1
end