1
0
Fork 0
80486DX2-66/sheets/adding-epoch-seconds-to-commit-messages.md

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:

  1. Open a terminal on your Linux system.
  2. Navigate to the Git repository where you want to apply this hook.
  3. Locate the .git directory inside the repository (check if it's there).
  4. You need to create or open the .git/hooks/commit-msg hook file.
    nano ./.git/hooks/commit-msg
    
  5. In the commit-msg file, you can use the following script:
    #!/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"
    
    Save the changes (Ctrl + O, then Enter). Exit Nano: Ctrl + X.
  6. 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.