Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i make git log --graph the default

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

like image 381
penchant Avatar asked Apr 22 '17 04:04

penchant


People also ask

Is git log --graph in chronological order?

Note that git log --graph does not show the commits in chronological order. The git help man pages call it --topo-order, topological ordering. “Show no parents before all of its children are shown, and avoid showing commits on multiple lines of history intermixed.”

What does git log -P <path> do?

Without this flag, git log -p <path>... shows commits that touch the specified paths, and diffs about the same specified paths. With this, the full diff is shown for commits that touch the specified paths; this means that "<path>…​" limits only commits, and doesn’t limit diff for those commits.

Is there a modifiable field for git log actions?

All Git actions (blue buttons) work like that. Even the main git log action itself is a modifiable field: By default it holds log --graph --oneline --pretty=VSCode --author-date-order -n 15000 --skip=0 --all $ (git reflog show --format='%h' stash)

Does gitgit log show merges when it contains merges?

git log can display surprisingly confusing results when the history contains merges. In this post, I’ll walk you through a few parameters that can help clear things up. I’ll start by showing how the default “chronological” order of git log can mislead you when the history contains merge commits.


2 Answers

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.

like image 86
VonC Avatar answered Oct 12 '22 13:10

VonC


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.

like image 25
Germain Chazot Avatar answered Oct 12 '22 15:10

Germain Chazot