Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default comments in the git commit message?

Tags:

git

git-commit

Is it possible to modify the commented part of the default git commit message? I want to add a bit more 'context' information for my users.

# Please enter the commit message for your changes.
# (Comment lines starting with '#' will not be included)
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test.txt
#
like image 364
zedoo Avatar asked Oct 19 '10 08:10

zedoo


People also ask

Can I edit my commit message?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N . Don't amend pushed commits as it may potentially cause a lot of problems to your colleagues.

Can we change the commit message in git after push?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.


3 Answers

There is commit.template configuration variable, which according to git-config(1) manpage:

Specify a file to use as the template for new commit messages. "~/" is expanded to the value of $HOME and "~user/" to the specified user's home directory.

You can put it in per-repository (.git/config), user's (~/.gitconfig) and system (/etc/gitconfig) configuration file(s).

like image 111
Jakub Narębski Avatar answered Oct 11 '22 16:10

Jakub Narębski


You can use git hooks for that. Before the person who wants to commit the changes is shown the commit message, the prepare-commit-msg script is run.

You can find an example prepare-commit-msg script in .git/hooks.

To edit the default message create a new file called prepare-commit-msg in the .git/hooks folder. You can edit the commit message by using a script like this:

#!/bin/sh
echo "#Some more info...." >> $1

The $1 variable stores the file path to the commit message file.

like image 45
weiqure Avatar answered Oct 11 '22 18:10

weiqure


Here is a python git-hook to clean up the default message. Hook name: prepare-commit-msg.

#!/usr/bin/env python
import sys
commit_msg_file_path = sys.argv[1]
with open(commit_msg_file_path, 'a') as file:
    file.write('')

You can simply add you text in the file.write() method.

like image 32
swayamraina Avatar answered Oct 11 '22 16:10

swayamraina