Auto Add Git Commit Message
Add to One Commit
If you want to append the actual date to your commits you can use below script:
$ git commit -m "modified <branch> files on `date`"
Your commit message will be like:
modified <branch> files on Tue Apr 29 17:23:02 +03 2020
You can modify the date format as well as you like:
$ git commit -m "generated files on `date +'%Y-%m-%d %H:%M:%S'`"
which will give:
modified <branch> files on 2020-04-29 17:23:02
Add to All Commits
If you want to append the actual date to all your commits of a specific repository it is better to use Git Hooks:
Git Hooks
Git hooks are scripts that Git executes before or after events like:
commit, push, rebase etc.
They are a built-in feature.
Git hooks run locally.
Every Git repository has a .git/hooks repo and .samples you can use:
.git/
├── COMMIT_EDITMSG
├── config
├── description
├── FETCH_HEAD
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ └── update.sample
Using Hooks
The hook we are going to use for our purpose is prepare-commit-msg.
Make a copy of the file:
$ cp .git/hooks/prepare-commit-msg.sample ./git/hooks/prepare-commit-messageDelete the template content and add the script:
#!/bin/bash # # Append current `date` to commit messages echo "`date +'%Y-%m-%d %H:%M:%S'`" >> $1where
$1is the commit message file.Make it executable:
$ chmod +x prepare-commit-message
After every git commit run the date is going to be added automatically:
Update post "auto-add-git-commit-message"
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch <feature>
# Your branch is ahead of 'origin/<feature>' by 2 commits.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# new file: content/post/2020-04/auto-add-git-commit-message.md
#
# Changes not staged for commit:
# modified: content/post/2020-04/auto-add-git-commit-message.md
#
2020-04-29 17:23:02
Further
Adding date to a commit would be unnecessary since Git already logging
date and you can see them with git log, however sometimes it would be useful.
After all of that you can use this post as an inspiration for your workflow automation.
All done!