我想知道是否有一种方法可以默认为git命令设置一个标志。具体来说,我想设置--abbrev-commit
标志,以便在执行git log
时,我想执行git log --abbrev-commit
。
与问题“is there any way to set a flag by default for a git command?”不同,显然没有用于将-abbrev-commit添加到git log的配置标志。此外,git手册指出我无法创建别名:"To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored"
我的第三个选择是在我的.gitconfig文件中发明一个新的别名,如glog=log --abbrev-commit
。但我宁愿不用新命令发明自己的DSL。
是否有其他方法可以实现它,以便默认设置abbrev-commit
标志?
答案 0 :(得分:53)
从git版本1.7.6开始,git config获得了一个log.abbrevCommit选项,可以设置为true。因此,答案是升级到至少1.7.6(截至本文撰写时的当前版本是1.7.11.4)并使用:
git config --global log.abbrevCommit true
答案 1 :(得分:43)
默认情况下,您可以使用自定义格式git log
模仿--abbrev-commit
:
git config format.pretty "format:%h %s"
答案 2 :(得分:32)
git中没有通用机制来为命令设置默认参数。
您可以使用git aliases定义带有所需参数的新命令:
git config alias.lg "log --oneline"
然后你可以运行git lg
。
某些命令还具有配置设置以更改其行为。
答案 3 :(得分:10)
VonC已在他的回答中暗示了一个shell包装器;这是我的这种包装器的Bash实现。如果你把这个,例如在您的.bashrc
中,您的交互式shell将支持覆盖Git内置命令以及大写别名。
# 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 -r gitAlias="git-$1"
if 'which' "$gitAlias" >/dev/null 2>&1; then
shift
"$gitAlias" "$@"
elif [[ "$1" =~ [A-Z] ]]; then
# Translate "X" to "-x" to enable aliases with uppercase letters.
translatedAlias=$(echo "$1" | sed -e 's/[A-Z]/-\l\0/g')
shift
"$(which git)" "$translatedAlias" "$@"
else
"$(which git)" "$@"
fi
}
然后,您可以通过将名为git log
的脚本放在PATH中来覆盖git-log
:
#!/bin/sh
git log --abbrev-commit "$@"
答案 4 :(得分:7)
我有类似的问题(Git命令的许多默认选项都很愚蠢)。这是我的方法。在路径上创建一个名为'grit'(或其他)的脚本,如下所示:
#!/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
非常直接阅读和维护,以防您需要与Bash非专家共享。
答案 5 :(得分:2)
我们使用的每个实用程序(svn,maven,git,...)总是封装在.bat(在Windows上,或Unix上的.sh)上,以便为我们的开发人员提供一个目录来添加到他们的路径中。
如果git封装在包装器脚本中,那么......一切皆有可能。
但这仍然是与用户设置相关联的解决方案,与Git本身或git repo无关。
答案 6 :(得分:1)
我喜欢git log --oneline
格式。
要将其设为默认值,请使用
git config --global format.pretty oneline
信用:https://willi.am/blog/2015/02/19/customize-your-git-log-format/