Adding “bug-fix” or any custom Message To your git commit Automatically
Use of the Prepare-commit-msg git hook
In This Scenario will create a local repository and set up the git hook.
Let’s start by setting up the repo locally
#!/bin/bash
mkdir ~/my-repo
cd ~/my-repo
git init
git status
touch test.txt
git add test.txt
git commit -m "First commit"
git branch
git checkout -b feature-1 #Optionally
git checkout master #Optionally to switch back to the master branch
use your preferred editor and add the above command, save it as my-local-repo-initialization.sh and then run it
sh my-local-repo-initialization.sh
Okay after we have successfully created and initiated our local repo, we can know use the prepare-commit-msg hook bash script below to add our custom commit message.
add the below commands to the ~/my-repo/.git/hooks/prepare-commit-msg
#!/bin/bashFILE=$1COMMIT_MESSAGE=$(cat $FILE)CUSTOME_MESSAGE="bug-fix" ### you can change the bug-fix to your customised message
echo "$CUSTOME_MESSAGE $COMMIT_MESSAGE" > $FILE
after you have added the above now we need to make it as a executable file so that git run it whenever there are new commits.
chmod +x ~/my-repo/.git/hooks/prepare-commit-msg
Okay. now we are good to test the prepare-commit-msg hook
git log
git status
echo "first line" > test.txt
git status
git add test.txt
git commit -m "Adding First Line"
git log