Overview
This story is about how to utilize git hooks in order to add branch name to each commit. I have used the script from this article and modified it to my needs to minimize the restrictions.
Details
Follow the instruction given below to enable it:
- Move inside your repository’s home directory.
- Rename the .git/hooks/prepare-commit-msg.example to .git/hooks/prepare-commit-msg
- Change file permissions:
sudo chmod 755 .git/hooks/prepare-commit-msg
4. Paste the script given below in that file
5. After these changes, the branch name will be added to your every commit and it will look like this
6. It will not allow you to commit directly to the following branch
1. master
2. production
3. staging
4. main
You can change these restrictions based on your requirements.
Final Thoughts
I hope you would love this story and let me know if anything can be improved.