1

As an example, I prefer viewing my Git branches using the flag --sort=committerdate. Is there a way to edit my .gitconfig to automatically use that flag?

Is there a general way to pass flags to commands by default?

Ceasar
  • 22,185
  • 15
  • 64
  • 83
  • Yes, I looked at that, but the question and answers deal with a more specific question. – Ceasar Aug 13 '18 at 18:30
  • @merlin2011's recommendation is actually a good duplicate; I'll close (there's nothing *wrong* with duplicates!). – torek Aug 13 '18 at 18:30

1 Answers1

1

Is there a general way to pass flags to commands by default?

No. Or rather, both no and yes, but it's not what you think, and --sort for git branch is not available this way.

There are two ways to do this, one general-purpose and one rather ad hoc. The general purpose method is to use aliases, which can be either shell aliases (the precise syntax for these depends on your shell) or Git aliases (which are defined by Git and hence more predictable here). For instance, you can make an alias br for branch --sort=committerdate:

$ git config alias.br "branch --sort=committerdate"

after which git br runs git branch --sort=committerdate.

The ad hoc method is per Git command and requires consulting both the documentation for that particular command—e.g., the git log documentation for git log—and the git config documentation. Here you will find that, for instance, the log.decorate setting controls whether --decorate is the default, and the color.branch (not branch.color) setting controls whether git branch output is colored by default. The "default default" for some of these defaults is often controlled by yet another setting, so that branch coloring is determined by the first of:

  • the command line --color or --no-color option if specified, or
  • the color.branch setting if specified, or
  • the color.ui setting if specified, or
  • whether output is going to a "tty device" (as determined by the C library isatty(1) function).

As it happens, there is no --sort=<key> control knob, so you are left with aliases as the only option here. Well, that is, unless you add a branch.sort or sort.branch setting knob, and convince the Git folks to adopt it! :-)

torek
  • 448,244
  • 59
  • 642
  • 775