在命令上设置 git 默认标志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2500586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:11:21  来源:igfitidea点击:

Setting git default flags on commands

gitlogging

提问by Jesper R?nn-Jensen

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-commitflag so that when executing git log, I want to execute git log --abbrev-commit.

我想知道是否有办法为 git 命令默认设置标志。具体来说,我想设置--abbrev-commit标志,以便在执行时git log,我想执行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"

与问题“有没有办法默认为 git 命令设置标志?”不同,显然没有将 --abbrev-commit 添加到 git log 的配置标志。此外,git 手册指出我不能创建别名:“为了避免脚本使用的混乱和麻烦,隐藏现有 git 命令的别名将被忽略”

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

我的第三个选择是glog=log --abbrev-commit在我的 .gitconfig 文件中创建一个新别名。但我宁愿不使用新命令发明我自己的 DSL。

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

有没有另一种方法来实现它,以便abbrev-commit默认设置标志?

采纳答案by underrun

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 1.7.6 版本开始,git config 获得了一个 log.abbrevCommit 选项,可以设置为 true。因此,答案是至少升级到 1.7.6(在撰写本文时当前为 1.7.11.4)并使用:

git config --global log.abbrevCommit true

回答by dahlbyk

You can use a custom format to have git logmimic --abbrev-commitby default:

默认情况下,您可以使用自定义格式来git log模拟--abbrev-commit

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

回答by hasen

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

git 中没有为命令设置默认参数的通用机制。

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

您可以使用git 别名来定义一个带有所需参数的新命令:

git config alias.lg "log --oneline"

Then you can run git lg.

然后你就可以运行了git lg

Some commands also have configuration settings to change their behavior.

某些命令还具有配置设置以更改其行为。

回答by Ingo Karkat

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.

VonC 已经在他的回答中暗示了一个 shell 包装器;这是我对这种包装器的 Bash 实现。如果你把这个 eg 放到你的.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-"
    if 'which' "$gitAlias" >/dev/null 2>&1; then
        shift
        "$gitAlias" "$@"
    elif [[ "" =~ [A-Z] ]]; then
        # Translate "X" to "-x" to enable aliases with uppercase letters. 
        translatedAlias=$(echo "" | sed -e 's/[A-Z]/-\l
#!/bin/sh
git log --abbrev-commit "$@"
/g') shift "$(which git)" "$translatedAlias" "$@" else "$(which git)" "$@" fi }

You can then override git logby putting a script named git-logsomewhere into your PATH:

然后git log,您可以通过将一个名为git-log某处的脚本放入您的 PATH来覆盖:

#!/bin/bash
cmd=
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

回答by Steve Bennett

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:

我有一个类似的问题(Git 命令的许多默认选项都是愚蠢的)。这是我的方法。在您的路径上创建一个名为“grit”(或其他)的脚本,如下所示:

##代码##

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

非常易于阅读和维护,以防您需要与非 Bash 专家共享。

回答by Ariel Gabizon

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

我喜欢这种git log --oneline格式。要将其设为默认值,请使用

git config --global format.pretty oneline

git config --global format.pretty oneline

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

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

回答by VonC

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.

我们使用的每个实用程序(svn、maven、git 等)总是封装在 .bat 中(在 Windows 上,或在 Unix 上为 .sh),以便为我们的开发人员提供一个目录来添加到他们的路径中。

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

如果 git 被封装在一个包装脚本中,那么……一切皆有可能。

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

但这仍然是一个链接到用户设置的解决方案,而不是链接到 Git 本身或 git repo。