Add Git branch name to each commit

Irtiza
2 min readOct 15, 2021
https://medium.com/weekly-webtips/how-to-create-and-run-git-hooks-d395ec60d0d

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:

  1. Move inside your repository’s home directory.
  2. Rename the .git/hooks/prepare-commit-msg.example to .git/hooks/prepare-commit-msg
  3. 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.

https://static.thenounproject.com/png/854669-200.png

--

--