96

I want to know if there is a way to set a flag by default for git command. Specifically, I want to set the --abbrev-commit flag so that when executing git log, I want to execute git log --abbrev-commit.

Unlike the question "is there any way to set a flag by default for a git command?", there is apparently not a configuration flag for adding --abbrev-commit to git log. Furthermore, the git manual states that I cannot create an alias: "To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored"

My third option is to invent a new alias like glog=log --abbrev-commit in my .gitconfig file. But I'd rather not invent my own DSL with new commands.

Is there another way to achieve it so that the abbrev-commit flag is set by default??

jmlarson
  • 837
  • 8
  • 31
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • 1
    As of git 1.7.6 there is a flag to control this behavior. See the answer by @underrun below. – slacy Jan 18 '13 at 19:38
  • As of ~2022-March/Git 2.35.1, there *still* doesn't appear to be a way to set default flags for common Git commands (outside of shell wrapper around `git` [[1](https://stackoverflow.com/a/10510960/7650275)] [[2](https://stackoverflow.com/a/8204925/7650275)]). I would like to see Git offer an option within .gitconfig to point commands to aliases. Alternatively, a new config category/group "prepend", where could define something like `prepend.log = "--stat --find-renames --stat-count=5"; prepend.diff = "blahblahblah"`. – PotatoFarmer Mar 25 '22 at 01:13

8 Answers8

69

Since git version 1.7.6, git config has gained a log.abbrevCommit option which can be set to true. Thus the answer is upgrade to at least 1.7.6 (current as of this writing is 1.7.11.4) and use:

git config --global log.abbrevCommit true
underrun
  • 6,713
  • 2
  • 41
  • 53
  • 6
    Don't suppose there's an option for oneline? – Zaz Mar 31 '13 at 15:56
  • 1
    I am accepting this answer. Nice to see Git added this configuration since I wrote the question almost four years ago. – Jesper Rønn-Jensen Mar 05 '14 at 19:44
  • 3
    Where is the source of all these mystical options? – xaxxon Nov 19 '15 at 00:25
  • Anyone know the default flag for graph? I can't find it in the page @xaxxon linked.. – myol Feb 03 '16 at 10:52
  • @myol, you should use `git config --help` or `git help config` to get the documentation **matching your installation**. – doak Dec 20 '18 at 20:19
  • Be forewarned. This setting will affect all displays of hashes in the log, including those for other built-in formats like `--pretty` and `--format=medium` unless you add `--no-abbrev-commit` – davenpcj Apr 25 '19 at 15:48
  • 1
    @Zaz for oneline use `git config --global format.pretty oneline` [https://stackoverflow.com/a/50875528/37899](https://stackoverflow.com/a/50875528/37899) – Kirill Osenkov Jan 25 '21 at 21:40
52

You can use a custom format to have git log mimic --abbrev-commit by default:

git config format.pretty "format:%h %s"
dahlbyk
  • 75,175
  • 8
  • 100
  • 122
  • Great, this is easily the best answer. – gnkdl_gansklgna May 29 '12 at 17:40
  • You can also use colors while formatting: "%C(yellow)%h%Creset %s" will present like git log --oneline with colors. – Avner Jul 01 '12 at 06:53
  • 1
    Now a quick question: if you found this useful and this answers exactly the problem, why don't you mark this as correct? – igorsantos07 Dec 01 '12 at 02:48
  • 1
    This is better if you don't want to remember obscure or hard to remember aliases and prefer to just use "git log" all the time. – Shyam Habarakada Feb 27 '14 at 20:03
  • @JesperRønn-Jensen I think this should be the correct answer, not what had been accepted above – Shyam Habarakada Feb 27 '14 at 20:05
  • @ShyamHabarakada Looking upon it, the answer from underrun is actually most correct for the versions of Git mentioned, so I change my accept to that question. – Jesper Rønn-Jensen Mar 05 '14 at 19:43
  • @JesperRønn-Jensen I didn't notice the version of git in the questions, but I think you mean the version of git you have (had?) doesn't support the format.pretty config? – Shyam Habarakada Mar 07 '14 at 02:01
  • 1
    This was the best answer for me. The `log.abbrevCommit` setting affected other more verbose formats, which I didn't like. Also, use `%C(auto)%h %s` to get the normal oneline format with automatic coloring. I used `git config --global format.pretty "%C(auto)%h %d %s"` because I wanted decorated oneline as my "default" – davenpcj Apr 25 '19 at 15:44
40

There is no generic mechanism in git to set default arguments for commands.

You can use git aliases to define a new command with the required arguments:

git config alias.lg "log --oneline"

Then you can run git lg.

Some commands also have configuration settings to change their behavior.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 14
    I want to avoid creating my own syntax so I'd prefer a solution where I don't use `git lg` but `git log` – Jesper Rønn-Jensen Mar 24 '10 at 09:20
  • why? what difference would it make? – hasen Apr 02 '10 at 23:27
  • 51
    The very important difference is that I don't want to introduce "Jespers git syntax" on my system. I want me (and others using my machine) to use the generic commands. Also, this will make my work faster on other machines: I don't worry about accidentally typing "git lg" and getting a "not found" error – Jesper Rønn-Jensen Apr 21 '10 at 09:22
  • 1
    I am accepting this answer as a valid solution. Even though this is what I originally described as my best option in the question, it seems it is the best solution at the moment. – Jesper Rønn-Jensen Dec 06 '10 at 08:40
  • 22
    If someone else was using your machine and typed git log your intended solution would mean that they would be getting results that they didn't expect. – Abizern Dec 06 '10 at 09:18
  • 2
    Redefining standard aliases can break scripts. – justintime Jan 31 '14 at 12:32
  • Scripts should not assume what options might be in effect, and be more explicit about specifying what they want. – Shyam Habarakada Feb 27 '14 at 20:36
  • 1
    @Abizern yes but you still have global configuration that does exactly that, so you're essentially saying _"don't configure your git globally because changing its behavior is bad."_ – fregante Sep 05 '16 at 09:54
16

I like the git log --oneline format. To get it as default, use

git config --global format.pretty oneline

Credit: https://willi.am/blog/2015/02/19/customize-your-git-log-format/

Ariel Gabizon
  • 2,851
  • 2
  • 17
  • 22
10

VonC has already hinted at a shell wrapper in his answer; here is my Bash implementation of such a wrapper. If you put this e.g. into your .bashrc, your interactive shell will support overriding of Git built-in commands as well as uppercase aliases.

# Git supports aliases defined in .gitconfig, but you cannot override Git
# builtins (e.g. "git log") by putting an executable "git-log" somewhere in the
# PATH. Also, git aliases are case-insensitive, but case can be useful to create
# a negated command (gf = grep --files-with-matches; gF = grep
# --files-without-match). As a workaround, translate "X" to "-x".
git()
{
    typeset gitAlias="git-$1"
    if type ${BASH_VERSION:+-t} "$gitAlias" >/dev/null 2>&1; then
        shift
        eval $gitAlias '"$@"'
    elif [ "$1" = "${1#-}" ] && expr "$1" : '.*[[:upper:]]' >/dev/null; then
        # Translate "X" to "-x" to enable aliases with uppercase letters.
    translatedAlias=$(echo "$1" | sed -e 's/[[:upper:]]/-\l\0/g')
        shift
        command git "$translatedAlias" "$@"
    else
        command git "$@"
    fi
}

You can then override git log by putting a script named git-log somewhere into your PATH:

#!/bin/sh
exec git log --abbrev-commit "$@"
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
8

I have a similar issue (many of the default options for Git commands are dumb). Here's my approach. Create a script called 'grit' (or whatever) on your path, as follows:

#!/bin/bash
cmd=$1
shift 1
if [ "$cmd" = "" ]; then
  git
elif [ $cmd = "log" ]; then
  git log --abbrev-commit $@
elif [ $cmd = "branch" ]; then
  git branch -v $@
elif [ $cmd = "remote" ]; then
  git remote -v $@
else
  git $cmd $@
fi

Very straightforward to read and maintain, in case you need to share it with Bash non-experts.

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
  • Do you really want "git remote blah" to call "git branch blah"? I'm using a variant of this where I put git earlier in the path and then explicitly call /usr/bin/git in my script. Yeah this probably breaks something. – LovesTha Apr 22 '15 at 05:52
  • 1
    I liked this and made two additional tweaks. I made it a bash function and let it be named `git` by adding a first line to get the real command path: `thegit=\`/usr/bin/which git\``. And I added an option to cancel the overrides with an x before the command like `git x log ...`: `if [ "$cmd" = "x" ]; then; $thegit $@`. – Joshua Goldberg Nov 03 '18 at 01:42
  • 1
    I would pair this wrapper with a git alias - the wrapper would call the alias, e.g. `git mycustomlog` (rather than the specific customized command). This allows the Git customizations to live in global git config and customized per project config, maybe more. – PotatoFarmer Mar 25 '22 at 01:20
1

A mixing of Steve Bennett's and PotatoFarmer's ideas above, but fully dynamic needing no script updates as well as allowing per-project flags by leveraging git's "alias" feature and our own convention: Anything prefixed with "my-" gets our treatment, otherwise it's just passed on. I have this as "~/bin/my_git" and use an alias in .bashrc to use it in place of git (alias git="$HOME/bin/my_git"):

#!/bin/bash
set -euo pipefail

cmd=${1:-}
shift 1

if [ "$cmd" = "" ]; then
    git
# Ask git itself if we have a "my-<cmd>" version to prefer
elif git config --list | egrep -q "^alias\.my-$cmd="; then
    git my-$cmd $@
else
    git $cmd $@
fi

And of course matching entries as desired in ~/.gitconfig and/or your project's .git/config file:

[alias]
    my-merge = merge --no-commit
    my-log = log --name-status
Byron Brummer
  • 51
  • 1
  • 6
0

Every utility we use (svn, maven, git, ...) are always encapsulated in a .bat (on Windows, or .sh on Unix), in order to offer our developers one directory to add to their path.

If git is encapsulated in a wrapper script, then... everything is possible.

But that remains a solution linked to the user's setup, not linked to Git itself or the git repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So you mean i should be able to achieve it by adding alias to my .bash_profile? – Jesper Rønn-Jensen Mar 24 '10 at 09:19
  • @Jesper: that is the idea, since your wrapper git.bat will be able to detect what Git command you want to execute and could add any option it wants. – VonC Mar 24 '10 at 10:11