Running a GIT Commit Command via bash

Table of Contents

Bash Shell Script

#!/bin/bash
cd /d/xampp/htdocs/racblog/wp-content/themes/twentyfifteen-child
git add functions.php
gitCmd=("git commit -m 'TX001: INitial COmmit'")

function gitCommit(){
	CommitComment=""
	CommentCount=$(($#-3))
	CommitCmd=$1" "$2" "$3  # the GIT Commit command 
	
  # All parameters strating with 4.th paramter build up our COMMIT Comment 
  for partialComment in "${@:4}"
	do
    CommitComment=$CommitComment" "$partialComment
		#echo $partialComment
	done
  
	echo "Paramenter Count:"  $# "Comment Count: "  $CommentCount
  echo "Commit Comment:" $CommitComment

  echo '$--->' $CommitCmd "$CommitComment"
  
  # Execute the GIT command 
  $CommitCmd "$CommitComment"
}

# Passing a GIT Command Array to our gitCommit() Function
# Note that we get 5 Parameters:  $1:git $2:commit $3:-m $4:'INitial $5:COmmit'
# In  the gitCommit Function we need to reconstruct the Commit Message to a single Parameter
  
 gitCommit ${gitCmd[@]}
 
 #Verify the Commit 
 git log -v

Script Output

helmut@myPC MINGW64 /d/xampp/htdocs/racblog/wp-content/themes/twentyfifteen-child (master)
$ /d/GIT/t.sh
Paramenter Count: 6 Comment Count:  3
Commit Comment: 'TX001: INitial COmmit'
$---> git commit -m  'TX001: INitial COmmit'
[master (root-commit) 095ce57]  'TX001: INitial COmmit'
 1 file changed, 39 insertions(+)
 create mode 100644 functions.php
commit 095ce57d1055879d0e133e7643294753a40fdd91 (HEAD -> master)
Author: Helmut 
Date:   Thu Oct 19 11:50:56 2017 +0200