Martin Skarzynski
Martin Skarzynski

Reputation: 31

Put list of modified files in git commit message

I am trying to add an alias to my .gitconfig that will automatically add all modified files in the current repo to the commit message.

I've tried to work with the output of git status --porcelain using cut and awk.

I got awk to work but I am sure there is a better solution that will yield a nicer commit message.

# in my .gitconfig
g = !git commit --all --message "edited:$(git status --porcelain | awk '{printf \"%s%s\",(NR>1?\",\":\"\"),$2} END{print \"\"}')"
# on the command-line
git commit --all --message "edited:$(git status --porcelain | awk '{printf \"%s%s\",(NR>1?\",\":\"\"),$2} END{print \"\"}')"

From git log, I see commit messages like edited:dir1/file1,dir2/file2.

I would like to clean up the code and/or improve the commit message.

Upvotes: 2

Views: 1685

Answers (3)

Martin Skarzynski
Martin Skarzynski

Reputation: 31

I came up with a solution that does exactly what I want, but it is pretty ghastly.

Here is what it looks like in my .gitconfig:

g = "!f() { git commit -am \"$(echo $@)\" && git push; }; f" "$([ -z \"$(git status --porcelain | grep '^\\s\\?A\\s')\" ] || git status --porcelain | grep '^\\s\\?A\\s' | awk 'BEGIN{print \"Added:\"} {printf \"%s%s\",(NR>1?\", \":\"\"), $2} END{print \"\"}')" "$([ -z \"$(git status --porcelain | grep '^\\s\\?C\\s')\" ] || git status --porcelain | grep '^\\s\\?C\\s' | awk 'BEGIN{print \"Copied:\"} {printf \"%s%s\",(NR>1?\", \":\"\"), $2} END{print \"\"}')" "$([ -z \"$(git status --porcelain | grep '^\\s\\?D\\s')\" ] || git status --porcelain | grep '^\\s\\?D\\s' | awk 'BEGIN{print \"Deleted:\"} {printf \"%s%s\",(NR>1?\", \":\"\"), $2} END{print \"\"}')" "$([ -z \"$(git status --porcelain | grep '^\\s\\?M\\s')\" ] || git status --porcelain | grep '^\\s\\?M\\s' | awk 'BEGIN{print \"Modified:\"} {printf \"%s%s\",(NR>1?\", \":\"\"), $2} END{print \"\"}')" "$([ -z \"$(git status --porcelain | grep '^\\s\\?R\\s')\" ] || git status --porcelain | grep '^\\s\\?R\\s' | awk 'BEGIN{print \"Renamed:\"} {printf \"%s%s\",(NR>1?\", \":\"\"), $2} END{print \"\"}')";

Sorry if my question was not clear.

The idea is that I can just type git g to

  1. add changes in tracked files,
  2. commit (with a half-decent message), and
  3. push.

Example commit message:

Added: file1, file2 Deleted: trash, rubbish Modified: work_in_progress

Upvotes: 1

Roland Smith
Roland Smith

Reputation: 43533

That information is already available in your commit. You just have to run git log --stat to see it.

I use this so often that I defined an alias for it in my .gitconfig:

[alias]
    ls = log --stat

So whenever I use git ls I get the stat output.

Upvotes: 4

Aron Hoogeveen
Aron Hoogeveen

Reputation: 522

If using the command line interface for git commit (default behaviour) you just need to uncomment the relevant lines that are already generated by Git. Search for the line with Changes to be commited and under that line there should be lines indicating the files modified/added/removed/moved.

Upvotes: 0

Related Questions