简体   繁体   English

如何用自定义别名替换内部 git 命令,例如 `git status`

[英]How to replace internal git commands, such as `git status`, with custom aliases

This answer teaches how to do custom bash commands inside git aliases, and this article ( One weird trick for powerful Git aliases ) teaches it beautifully.这个答案教了如何在 git 别名中执行自定义 bash 命令,而这篇文章(一个强大的 Git 别名的怪异技巧)教得很好。 However, it doesn't seem to work when I alias internal git commands.但是,当我为内部 git 命令设置别名时,它似乎不起作用。 How can I replace internal git commands with custom scripts?如何用自定义脚本替换内部 git 命令?

Ex: I have a custom python script: custom_script.py例如:我有一个自定义 python 脚本: custom_script.py

print("hello")

In my project_name/.git/config file I add this alias:在我的project_name/.git/config文件中,我添加了这个别名:

[alias]
    statuss = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

I then run git statuss and it works perfectly, I see "hello" printed out.然后我运行git statuss并且它运行良好,我看到打印出“hello”。 followed by the "git status" return messages.随后是“git status”返回消息。

HOWEVER, if I rename my alias name from statuss to status , it no longer works.但是,如果我将别名从statuss重命名为status ,它将不再有效。

[alias]
    status = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

How can I do this so that by simply calling git status , it first calls my "custom_script.py" and then runs git status ?我该怎么做才能通过简单地调用git status ,它首先调用我的“custom_script.py”然后运行git status

Notes:笔记:

  • Ideally, this would work in Windows, Mac, or Linux, but Linux is what I care about most, so a Linux-only solution would be ok.理想情况下,这可以在 Windows、Mac 或 Linux 中运行,但 Linux 是我最关心的,因此仅 Linux 的解决方案就可以了。
  • Ideally, I'd like to do this via a git alias, but if I need to use a bash alias or other Linux/computer trick, that's better than nothing.理想情况下,我想通过 git 别名来执行此操作,但如果我需要使用 bash 别名或其他 Linux/计算机技巧,那总比没有好。

Git doesn't allow you to override internal commands in aliases, because doing so would break any scripting that uses those internal commands that expected them to function as normal. Git 不允许您覆盖别名中的内部命令,因为这样做会破坏任何使用那些期望它们正常运行的内部命令的脚本。 This is especially important because some Git commands are shell scripts, and overriding those commands could break Git's shell scripts.这一点尤其重要,因为一些 Git 命令是 shell 脚本,覆盖这些命令可能会破坏 Git 的 shell 脚本。

You can override this by writing a shell script called git which you place into a directory in $PATH , and that is the easiest way to accomplish it.您可以通过编写一个名为git的 shell 脚本来覆盖它,将其放入$PATH的目录中,这是完成它的最简单方法。 However, be aware that the first argument to git need not be a command: git takes a large number of options which precede the command, such as -c and -C , and your script would need to parse those in order to avoid breaking any other scripts that might call git (which might, for example, include your editor).但是,请注意git的第一个参数不必是命令: git在命令之前采用大量选项,例如-c-C ,您的脚本需要解析这些选项以避免破坏任何其他可能调用git的脚本(例如,可能包括您的编辑器)。

So while this is possible, it's very tricky, and any correct solution would be fairly lengthy (which is why I haven't attempted it here).因此,虽然这是可能的,但它非常棘手,而且任何正确的解决方案都将相当冗长(这就是为什么我没有在这里尝试过)。 Generally, the recommended solution is to use an alias which doesn't mirror a builtin name, which is much simpler.通常,推荐的解决方案是使用不反映内置名称的别名,这要简单得多。

However, if you want this only for interactive use, it's possible to create a script in your $PATH called, say, git-wrapper , and do something like this inside:但是,如果您只想将其用于交互式使用,则可以在$PATH中创建一个名为git-wrapper的脚本,并在其中执行以下操作:

#!/bin/sh

if [ "$1" = status ]
then
    python3 custom_script.py
fi
exec git "$@"

You can then run alias git=git-wrapper in your ~/.bash_profile or ~/.zshrc (but not ~/.bashrc or ~/.zshenv ).然后,您可以在~/.bash_profile~/.zshrc /.zshrc 中运行alias git=git-wrapper (但不是~/.bashrc~/.zshenv )。 This will affect only cases where you specifically write git status , but not any scripting uses.这只会影响您专门编写git status的情况,但不会影响任何脚本使用。 This might be good enough for you, or not.这对你来说可能足够好,也可能不够好。

You can use the following command: git config --global alias.co checkout and that's all.您可以使用以下命令:git config --global alias.co checkout 就可以了。

Link: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases链接: https ://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM