0

I use git status with the branch and short configuration flags set to true at ~/.gitconfig and, after reading the git config and git commit documentation pages, I couldn't find a way that replicates this behaviour to git commit without the usage of aliases. The only obvious configuration option related to status output is related to the message that is presented at vim (or whatever is the default editor) while editing the commit message. Is there a way to achieve this that I am missing? If so, links to relevant documentation sections would be appreciated!

The expectation here is not to make the git commit command to perform an implicit --dry-run by default (through the usage of --short), it is solely related to the command output.

Sample Expected Output:

$ git status
## master
 M foo
?? bar

$ git commit
## master
 M foo
?? bar

Sample Actual Output:

$ git status
## master
 M foo
?? bar

$ git commit
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   foo
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    bar
no changes added to commit (use "git add" and/or "git commit -a")
dandev486
  • 4,143
  • 2
  • 14
  • 22
  • The short answer is no, it can't be configured (at least today). If you were to submit patches that made `git commit` obey the short-status option when there's nothing to commit, they might be accepted, since that does seem ... right. :-) – torek Jul 21 '21 at 13:59

1 Answers1

0

If it cannot be configured you could go with aliases. Unfortunately you cannot override an existing command (e.g. you cannot change the behaviour of git commit itself, but you'd need to use a different name)

For instance you could

$ git config alias.cmt "commit --short --branch"
$ git cmt
## master
 M foo
?? bar

May not be as nice as calling git commit but it would work fine and avoid some weird corner cases of overriding the commit command (until the option will be configurable like it is for git status).

EDIT

As the original question is explicitly requiring not to use aliases, I'll add some workaround.

Calling git commit with the --short option is implying a --dry-run (e.g. no commit, just like git status). Surprisingly this dry run is not following the configuration from git status (at the time of writing the --short needs to be passed explicitly). I couldn't find any way to override this behaviour, but there's a way to have at least such output in commit messages (instead of a normal git status without the short format).

Edit the file .git/hooks/prepare-commit-msg with this content, then make it executable (chmod +x .git/hooks/prepare-commit-msg)

#!/usr/bin/sh

COMMIT_MSG_FILE=$1

echo > $COMMIT_MSG_FILE #a single new line, can change that
git status --short --branch | sed 's/^[^#]/# &/' >> $COMMIT_MSG_FILE

This file will be called before preparing the commit default file, giving a result like this:


## master
#  M foo
# ?? bar

To have the behaviour global (and not per repository), follow here

This is unfortunately just half a solution, if you call git commit with no file stashed it will still show the long output (get used to call git status instead, for now)

glemco
  • 36
  • 4
  • Thank you for your answer, but my question is about whether it is possible or not doing this without using aliases. The desired behaviour is achievable for the `git status` command and it seems like I am missing something (since the output presented by `git commit` without any arguments is essentially the default output of `git status`). – dandev486 Jul 20 '21 at 11:10
  • I use aliases for repetitive commands that represent something on my workflow and are not part of git itself, for instance an alias that performs "checkout a new feature branch foo" that creates and checkout a branch named `feature/foo`. On the other hand, the `git commit` command has the `--branch` and `--short` flags, as the `git status` command also does, but the "status output" is not the same in the cases presented. – dandev486 Jul 20 '21 at 11:16
  • I couldn't find any evidence that it can be done via configs, not sure if there's another workaround then.. Just for curiosity, why would you use the `git commit`'s dry run instead of simply `git status`? Is there any difference in their output? – glemco Jul 20 '21 at 11:50
  • No problem, I couldn't find that either. As for your question, the usage scenario that triggered this question is not intentionally running a dry run but, instead, running `git commit` prior to running a `git add `. When one forgets it, git gives a helping hand and displays that status, that could be displayed in the same format as configured for `git status` itself. – dandev486 Jul 20 '21 at 12:52
  • The intent was to standardize the output of `git status`, `git commit` when one forgets to perform a `git add` and, last but not least, the status added as a comment when editing the commit message through vim or whatever is the configured default editor. – dandev486 Jul 20 '21 at 12:54
  • 1
    Well, never though about that.. I'm so used to run git status every time. However from what I can understand from the documentation (didn't dig inside git's code), the short option implies a dry-run, which basically is like calling git-status. The fact that it doesn't inherit the same configuration is quite weird, but trying to have `--short` as default behaviour is fundamentally wrong (you'd never be able to really commit as it would always dry-run). Still couldn't find any interesting hints to solve the problems.. – glemco Jul 20 '21 at 13:08
  • I didn't notice that `--short` implies `--dry-run` and it would really be wrong as you stated. Maybe that is the reason why it seems not to be a configuration option, even though it would be nice to have the output formatted in a uniform manner. I will update the question to consider that, since the desired effect is the output, not the dry run and I will try to investigate it further during this week. – dandev486 Jul 20 '21 at 13:30