1.6 KiB
1.6 KiB
Original response by ChatGPT
Edited by Larry Holst, for use by a certain someone!
Licensed under Creative Commons Zero 1.0 Universal
To automatically change the commit message using a pre-hook in Git on Linux, you can follow these steps:
- Open a terminal on your Linux system.
- Navigate to the Git repository where you want to apply this hook.
- Locate the
.git
directory inside the repository (check if it's there). - You need to create or open the
.git/hooks/commit-msg
hook file.nano ./.git/hooks/commit-msg
- In the
commit-msg
file, you can use the following script:
Save the changes (#!/bin/sh # Get the commit message file name commit_msg_file=$1 # Capture the original commit message original_commit_msg=$(cat $commit_msg_file) # Format the new commit message new_commit_msg="$(date +'%s'): $original_commit_msg" # Override the commit message file with the new message echo "$new_commit_msg" > "$commit_msg_file"
Ctrl + O
, thenEnter
). Exit Nano:Ctrl + X
. - Make the
commit-msg
file executable by running the following command:chmod +x ./.git/hooks/commit-msg
Now, the pre-hook is set up and will execute every time you try to commit a change in that repository. The commit message will be automatically modified by prefixing the current Unix timestamp in seconds followed by a colon and the original first line of the commit message.
Note that this hook will only affect local commits made on the Linux system where you set it up. Remote commits will not be affected.