在命令上设置 git 默认标志

我想知道是否有一种方法可以为 git 命令设置默认的标志。具体来说,我想设置 --abbrev-commit标志,以便在执行 git log时,我想执行 git log --abbrev-commit

与问题“ 有没有办法为 git 命令默认设置一个标志?”不同,显然没有用于向 git 日志添加—— abbrev-commit 的配置标志。此外,git 手册规定我不能创建别名: ”为了避免与脚本使用相混淆和麻烦,将忽略隐藏现有 git 命令的别名

我的第三个选择是在我的。Gitconfig 文件。但是我不想用新的命令发明我自己的 DSL。

是否有其他方法来实现它,使 abbrev-commit标志是默认设置?

23742 次浏览

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.

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.

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

git config format.pretty "format:%h %s"

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 "$@"

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.

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

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/

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