If you want to master Git, you must first master the COMMIT-EDITMSG . This article will explore what it is, why it exists, how to customize it, and how to leverage its hidden features to become a more effective developer. At its core, COMMIT-EDITMSG is a temporary text file that Git creates in your local .git directory. Specifically, you can find it at .git/COMMIT_EDITMSG . When you run a command like git commit (without the -m flag), Git opens your default text editor and loads the contents of this file.
echo "Hotfix: Resolve null pointer exception" > .git/COMMIT_EDITMSG git commit --no-edit The --no-edit flag tells Git, "Don’t open the editor; just use the existing COMMIT_EDITMSG file." This is how tools like git rebase --continue work behind the scenes. COMMIT-EDITMSG
#!/bin/bash # .git/hooks/commit-msg COMMIT_MSG_FILE=$1 read -r subject < "$COMMIT_MSG_FILE" Regex pattern for a JIRA ticket pattern="^[A-Z]+-[0-9]+: .+" If you want to master Git, you must
: If you abort a commit (e.g., by exiting your editor with an empty message), Git deletes the file. Do not manually edit COMMIT_EDITMSG while a commit is not in progress. Always let Git create and manage the file. Common Workflows Enhanced by COMMIT-EDITMSG Fixing a Typo in the Last Commit You commit with a typo. Instead of git commit --amend -m "new message" , use: Specifically, you can find it at
Now, every time you save your COMMIT_EDITMSG , this script runs. If you forget the ticket number, Git aborts the commit. The file remains in .git/COMMIT_EDITMSG so you can edit it and retry. Perhaps you want every commit to include a Co-authored-by: trailer. Your commit-msg hook could append it automatically:
The COMMIT-EDITMSG represents the final step before a commit object is permanently written to your repository’s object database. You are not just "typing a message"; you are editing the metadata that will be cryptographically hashed and stored forever (or until a rebase).
Now, every time you run git commit , your COMMIT_EDITMSG is pre-populated with this scaffolding. You just fill in the blanks. This dramatically improves commit quality across a team. Because COMMIT_EDITMSG is just a text file, you can technically edit it directly without using git commit . For example: