- Edited
I learned a new trick.
These instructions are for Linux. Modifications may be needed on Windows or Mac.
Add this post-commit script to the .git/hooks subfolder of a Godot project:
#!/bin/sh
# Generate a GDScript file containing the information from the last git commit.
# The file path is relative to the git repository root.
FILE="post_commit.gd"
echo "# Auto-generated by git hook post-commit." > $FILE
echo "class_name GitLastCommit" >> $FILE
# last commit author date as UNIX timestamp
echo "const SECS_SINCE_EPOCH: int = $(git log --max-count=1 --pretty=format:"%at" HEAD)" >> $FILE
# last commit abbreviated hash
echo "const SHORT_HASH: String = '$(git log --max-count=1 --pretty=format:"%h" HEAD)'" >> $FILE
The value for FILE can be changed if desired. If it's in a subfolder, that subfolder must exist.
If you want to change the information provided by git, the documentation is here:
https://git-scm.com/docs
You can run the script manually to initialize the file, without actually performing a commit:
$ .git/hooks/post-commit
This GDScript code example prints the last git commit info, but it could also be displayed in a node, such as a Label or RichTextLabel:
# Get git commit info.
# Use offset from UTC to display date/time as local time.
var bias_secs: int = Time.get_time_zone_from_system()['bias'] * 60
var last_commit_datetime: String =\
Time.get_datetime_string_from_unix_time(GitLastCommit.SECS_SINCE_EPOCH + bias_secs)
var last_commit: String = "%s [%s]" % [last_commit_datetime, GitLastCommit.SHORT_HASH]
print_debug("Last commit: %s" % last_commit)
Sample output:
Last commit: 2025-03-01T19:16:19 [fa93c1d]
The file created by the post-commit hook (post_commit.gd in the above example) should be added to .gitignore, since it's generated automatically after each commit.
Add this line to .gitignore to prevent git from tracking both post_commit.gd and post_commit.gd.uid:
post_commit.gd*
(.uid files were added in Godot 4.4)
I've observed that when a commit is made inside the Godot editor using the godot-git-plugin, that commit may not be shown by the code in the above example. I don't understand why, but it can be resolved by using the following command outside of Godot to edit the last-commit message. It's not necessary to make any actual changes to the commit message.
git commit --amend
If it's an empty commit with no file changes, add the allow-empty parameter:
git commit --amend --allow-empty