How to automatically generate commit message

14,872

Solution 1

If you are really that lazy you may just use the following. In brief, it does a git status, extract lines for new files, deleted, renamed and modified, and pass it to git commit

# LANG=C.UTF-8 or any UTF-8 English locale supported by your OS may be used
LANG=C git -c color.status=false status \
| sed -n -r -e '1,/Changes to be committed:/ d' \
            -e '1,1 d' \
            -e '/^Untracked files:/,$ d' \
            -e 's/^\s*//' \
            -e '/./p' \
| git commit -F -

Tweak the sed part to customize how you want the message to be generated base on result of git status

Alias it to something short, or save it as a script (e.g. git-qcommit) so that you can use it as git qcommit

A sample message from git log

adrianshum:~/workspace/foo-git (master) $ git log
commit 78dfe945e8ad6421b4be74cbb8a00deb21477437
Author: adrianshum <[email protected]>
Date:   Wed Jan 27 01:53:45 2016 +0000

renamed:    bar.txt -> bar2.txt
modified:   foo.txt

Edited: Changed original grep to sed to make the commit message generation logic more generic by including lines between Changes to be committed and Untracked files, and produce a slightly better looking commit message)

Solution 2

If you don't provide a message (using the -m flag), an auto-generated message will be opened and asks you to modify it (if you with). It looks like:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch <branch>
# Changes to be committed:
#       deleted:    <deleted files>
#       modified:   <modified files>
#
# Untracked files:
#       <untracked files>

Now you just have to remove the # from the lines you want to insert (say the modified ones).

I really discourage you from doing that, commit messages are very important.

Related question with different (maybe better) approach.

Solution 3

You can use the commit -m command to pass any message as your commit message.


Another solution is to use the template configuration option to define default template.

commit.template

There is commit.template configuration variable.

commit.template

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.

Solution 4

Sometimes I just want to commit with an automatically generated message. This script grabs the output of git status --porcelain and uses it as the commit message.

#!/usr/bin/env node
var childProcess = require('child_process');
var spawn = childProcess.spawn;

function getStatusMessage() {
  var bash = spawn('bash');
  bash.stdin.end('git status --porcelain');

  return new Promise(function (resolve) {
    bash.stdout.on('data',function (data) {
      resolve(data.toString());
    });
  });
}

getStatusMessage().then(function (statusMessage) {
  var bash = spawn('bash');
  bash.stdin.end('git commit -m "'+statusMessage+'"');
  bash.stdout.on('data',function (data) {
    console.log(data.toString());
  });
  bash.stderr.on('data',function (data) {
    console.log(data.toString());
  });
});

Here's a gist.

Solution 5

Here's a variation of @adrian-shum's commands that uses git status --porcelain to be more robust, formatted as a copy-and-paste:able git alias below:

Git alias maintained in this gist:
https://gist.github.com/erikw/654386d35ecfdb0354cd2b71763f19ae

This will produce a commit with a message (last line) e.g.:

$ git status --porcelain
A file1.py
A file2.py
A file3.py
M file4.py
M file5.py
D README.md
R test.txt-> test2.txt
$ git commit-status
$ git log --no-decorate -n 1
bee4f8e Added: file1.py file2.py file3.py Modified: file4.py file5.py Deleted: README.md Renamed: test.txt-> test2.txt

Why so complicated substitutions? Because the modifiers can be grouped e.g. "RM a -> b" would mean that the file was both renamed and modified.

Share:
14,872
birgersp
Author by

birgersp

Updated on June 05, 2022

Comments

  • birgersp
    birgersp about 2 years

    In some (very) rare occasions, I make some changes in my repository that are so self-explanatory that a commit message describing my intentions is somewhat useless. In these cases, I would like the commit message to basically just list what files I've added/removed/edited. For instance:

    Added 'dog.h', 'cat.h'

    A manual commit message would look like

    Added header files

    In situations like this it would be nice to not have to actually write the commit message, but rather have it automatically generated.

    I'm aware that this is very bad practice, but I would only use this for non-professional repositories used for private projects. I know it's lazy, but I'm curious as to how it can be done. Unix shell scripts are preferred, but any solution is welcome.

    Q: Is there a way to automatically generate a git commit message, listing what files that has been changed?