10

very similar to this question: How to make git log decorate by default

I would like to make git log do git log --graph. I assumed I could add something like graph = true to the [log] section of my ~/.gitconfig file, but it did not work, nor did any of the other 28 things I tried putting into the [log] section. :(

I expect it will be suggested that I add an alias like git lg. I do not want to create an alias. I have two reasons for this:

  1. my fingers have been typing git log for over a decade and I have no interest in changing that
  2. As a result of my career, I am extremely conservative with my usage of aliases. I cannot add, nor am I always able to add, my alias to however many thousands of machines I end up interacting with as a cloud engineer. I use git on multiple machines and I want git log to be the solitary command I use to display the git log.

UPDATE: I thought of a way to do it, but I hate it. The idea is to create a bash script called git and put it somewhere in my path before /usr/bin/git. All it would do is call /usr/bin/git with whatever arguments are passed, unless it is a log in which case it will do the same but tack on a --graph. /me shudders

penchant
  • 2,263
  • 3
  • 14
  • 20
  • Note: if you want a *regular* `git log` (without the `--graph` added automatically), you can on demand add a `--no-graph` option to your `git log`, with Git 2.26 (Q1 2022). See [my edited answer below](https://stackoverflow.com/a/43555332/6309). – VonC Feb 24 '22 at 08:05

3 Answers3

1

2017: I don't know of:

  • a git configuration for the graph option: you might have to propose a similar default behavior as the one I mentioned with Git 2.13 for got log --decorate
  • a way without wrapper of some sort

For instance, you can define a bash function which would add the option

function do_git {
  cmd=$1
  shift
  extra=""
  if [ "$cmd" == "log" ]; then
    extra="--graph"
  fi
  "`which git`" "$cmd" "$extra" "$@"
}

Then add a wrapper or an alias to reference do_git.

The advantage is that the function is part of your dotfiles, that you can manage as a git repo and replicate across your machines. See for instance " Managing Dotfiles with Git"

2022: Note, a log.graph option is being discussed/implemented.


But: what happens when you type git log --graph (resulting in git log --graph --graph)?

Or what if you want a regular git log? (without the --graph added automatically)

Before Git 2.36 (Q2 2022), "git log --graph --graph"(man) used to leak a graph structure, and there was no way to countermand "--graph" that appear earlier on the command line.

A "--no-graph" option has been added and resource leakage has been plugged.

See commit 087c745, commit dccf6c1 (11 Feb 2022) by Alex Henrie (alexhenrie).
(Merged by Junio C Hamano -- gitster -- in commit 8813596, 23 Feb 2022)

log: add a --no-graph option

Signed-off-by: Alex Henrie

It's useful to be able to countermand a previous --graph option, for example if git log --graph(man) is run via an alias.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You think as i do! Also, from further reading it appears git itself doesn't allow you to set graph in gitconfig. So wrapper it is, for now. – penchant Apr 22 '17 at 04:47
  • @tr3buchet Yes, for now there is no native option allowing to define that --graph as a default. – VonC Apr 22 '17 at 04:48
  • If you're going to wrap something, why not wrap `git-log`, as it will be more specific. Also, you can create an alias in your `~/.gitconfig`, which seems like the best option. – iconoclast Aug 01 '18 at 01:17
  • @iconoclast an alias for git log? I believed it would not work: https://stackoverflow.com/q/5916565/6309, https://stackoverflow.com/a/3538791/6309. I did not try to define a `git-log` executable though. – VonC Aug 01 '18 at 07:15
1

I don't think that doing it as default would be what you want in all cases anyway. I am thinking especially of cases when you will want to process the output of git log or when you add other options that would conflict with --graph.

So I prefer to go with the idea in your UPDATE and put a little script in my $HOME/bin, called git and which comes before the installed git in my $PATH.

It looks like this:

#!/bin/bash

function run_git(){
    real_git=$(which -a git | sed '2 !d;')
    bash -c "$real_git $(printf ' %q' "$@")"
}

if [ -t 1 ]; then
    case "$*" in
    log)
        run_git log --graph
        exit $?
        ;;
    esac
fi

run_git "$@"

The function:

  • gets the actual git executable to call.
  • runs the command, preserving spaces in argument by double-quoting them (thanks to this answer

The [ -t 1 ] test checks that this outputs directly to a terminal, to avoid messing potential scripts/programs/pipes up. (thanks to this question)

Then the case statement will allow to handle other similar cases simply.

Germain Chazot
  • 430
  • 4
  • 10
  • 1
    Interesting addition to my own answer. +1. That script seems more complete than mine ;) – VonC May 17 '18 at 15:40
  • Doing it by default is exactly what I would want. For times that I don't want it (such as processing via script), I would expect one of two behaviors that other tools use. Either: a) git could detect that its output is going through a pipe and turn off default options that affect the display. Users could still manually specify these options. or better yet b) git should have a --graph as well as a --no-graph option. The latter could be used to turn off a default option. Both of these techniques are rather common in large CLI applications like VCS and package managers. – bobpaul May 31 '19 at 23:46
0

If you want a log --graphby default, add to your ~/.bashrc (or ~/bash_aliases) this function:

function git {
  cmd=$1
  shift
  extra=
  if [ "$cmd" = "log" ]; then extra=--graph; fi
  "$(which git)" "$cmd" $extra "$@"
}

Now to get a possibility to see log without graph, and add a corresponding git alias:

 git config --global alias.log-default 'log'

If you have set up a non-default pretty format, and want to get rid of it, just add --pretty=medium

 git config --global alias.log-default 'log --pretty=medium'

Alternatively to git alias, you can add this line to the function git:

  if [ "$cmd" = "log-default" ]; then cmd='log'; extra=; fi
user2622016
  • 6,060
  • 3
  • 32
  • 53