25

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?

birgersp
  • 3,909
  • 8
  • 39
  • 79
  • 1
    just do a git status and extract the lines starting with "new file/deleted/modified/renamed" and use that as commit comment – Adrian Shum Jan 26 '16 at 10:17
  • 1
    Relevant - http://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message – Andrew C Jan 26 '16 at 16:35
  • 7
    `curl http://whatthecommit.com/index.txt` – wim Apr 07 '17 at 15:18
  • Use [npm] package (https://www.npmjs.com/package/lazycommit-cli) [lazycommit](https://codeberg.org/fftcc/lazycommit-cli) – ff99cc Feb 26 '23 at 07:15

7 Answers7

12

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 <foo@bar.com>
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)

jp48
  • 1,186
  • 10
  • 17
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • 8
    You should only script against `git status` if you use `git status --porcelain`, otherwise you will break in the future – Andrew C Jan 27 '16 at 16:10
  • Yes I am aware of it. This is only aimed to be a quick and easy one line solution :P – Adrian Shum Jan 27 '16 at 23:40
  • Maybe add `LANG=c` to ensure English – rubo77 Sep 09 '16 at 22:45
  • 3
    I wouldn't recommend calling someone lazy when there could be a valid reason for automating something like this. I mean, isn't that what we're all here for? – Resist Design Sep 10 '21 at 15:08
  • 2
    I disagree about laziness, there are different situations. I have several repositories with notes and configurations, and I don't want to open each of them for commit and submit. And here is my solution to this issue. https://stackoverflow.com/a/75134440/21008370 –  Jan 21 '23 at 14:50
6

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.

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • FTR I use git for short term local development then submit to a different repository. so I'm often "this lazy" and correct commit messages are written at regular intervals. – djechlin Dec 21 '16 at 18:06
4

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.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
4

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.

Erikw
  • 799
  • 6
  • 18
0

In such rare cases, I like to use a standard one-word message, like "trivial". But others might be "uninteresting" or "addfiles". The important part is probably that it's consistent and recognizable.

But I've recently started using git leaders like "Add" and "Doc". So then these messages are a little more meaningful; e.g., "Add: files".

If you're looking for a fun way, and your team knows that ridiculous messages mean that it's a trivial commit, use http://whatthecommit.com/ to auto-generate humorous messages.

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
0

hope this helps, you can configure an alias in .bashrc , under the aliases for ls section as below

alias gitit="git commit -pm '`git status -s` Edit# `git log | grep commit | wc -l`'; git push"

what this does is

  1. Show you the changes you have made and prompt you with a y/n for staging them in the commit (-p)
  2. Commits a message (-m) consisting of the change made using git status -s and the number of changes since branch start git log | grep commit | wc -l'
  3. Pushes the change

example run below

tr@tr-work:~/Gits/devops-ansible-roles$ gitit

diff --git a/README.md b/README.md
index 0ba82f15..dd8be086 100755
--- a/README.md
+++ b/README.md
@@ -3,4 +3,7 @@
 ==============================================================

 This repository is a collection of Ansible Roles and associated artifacts for executing those roles such as scripts, templates and variable files.
-These roles are called by Jenkins Pipelines defined in the devops-jenkins-pipelines repo.
\ No newline at end of file
+These roles are called by Jenkins Pipelines defined in the devops-jenkins-pipelines repo.
+
+
+//
(1/1) Stage this hunk [y,n,q,a,d,e,?]? y

[feature/DO-389_manage_dbs c6dfacf8]  M README.md Edit# 184
 1 file changed, 4 insertions(+), 1 deletion(-)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: 
remote: Create pull request for feature/DO-389_manage_dbs:
remote:   https://bitbucket.org/retracted/devops-ansible-roles/pull-requests/new?source=feature/DO-389_manage_dbs&t=1
remote: 
To bitbucket.org:retracted/devops-ansible-roles
   4e14490e..c6dfacf8  feature/DO-389_manage_dbs -> feature/DO-389_manage_dbs
tr@tr-work:~/Gits/devops-ansible-roles$ git log
commit c6dfacf825e6c325bb579de29305d82a7b6bd07d (HEAD -> feature/DO-389_manage_dbs, origin/feature/DO-389_manage_dbs)
Author: Tanveer Roowala <tanveer.roowala@retracted.com>
Date:   Sat May 16 13:58:31 2020 +1000

     M README.md Edit# 184

0

what I do is to save the file while leaving the editor window open and then use a sed script in parallel to edit. For instance you could use sed -n '/new/ s/#//gp' commit_file and when you are satisfied switch to sed -i ending with just //g on the string. Then reread the file, if you are using nano press CTRL+R commit_file and if you want do some manual editing and exit