【问题标题】:How to make bash suppose all commands to be git commands?如何让 bash 假设所有命令都是 git 命令?
【发布时间】:2012-11-21 02:26:28
【问题描述】:

我可以让 bash 假设命令始终是 git 命令吗?

我的意思是:

如果我写了push origin master,那么它会执行git push origin master

注意:我会在 git bash(Windows 环境)中使用它,所以我不需要常规命令(ls、cd 等)来工作。

【问题讨论】:

    标签: git bash alias


    【解决方案1】:

    你可能想试试这个小的 git shell,而不是:

    gitsh () {
        while read -r -p "gitsh> " -a GITCOMMAND; do
            git "${GITCOMMAND[@]}"
        done
    }
    

    定义该函数后,运行一次,它将处于无限循环中, 读取命令行并将其作为选项传递给git 命令。

    编辑:按照 gniourf_gniourf 的建议,将 -e 选项添加到 read 命令,为您提供了一个基于 Readline 的良好环境,您可以在其中检索和编辑历史记录。

    【讨论】:

    • 这显然是一个很好的答案。将-e 选项设置为read 将是完美的。
    • 非常正确。我自己不经常使用-e,所以没有考虑。
    【解决方案2】:

    您可以为常用命令添加别名,例如push 将变为 git push 等。此解决方案需要指定您正在使用的所有命令,但不应破坏任何内容。

    【讨论】:

      【解决方案3】:

      假设“git bash”就像普通的 bash,使用 command_not_found_handle 函数:

      function command_not_found_handle {
        command git "$0" "$@"
      }
      

      http://www.gnu.org/software/bash/manual/bashref.html#Command-Search-and-Execution

      存在发送 git 无意命令的可能性,但这就是您在问题中所要求的。

      【讨论】:

      • 我在考虑这种方法,但是它与 $PATH 中已经存在的命令以及 git 中存在的命令存在缺陷,例如,rm...
      【解决方案4】:

      您可以将名为 push、commit 等的脚本添加到您的 PATH 中。这样你的外壳就会有推送、提交等“命令”。这应该适用于您使用的任何 shell,而不仅仅是 bash。

      【讨论】:

        猜你喜欢
        • 2012-09-29
        • 1970-01-01
        • 1970-01-01
        • 2015-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        相关资源
        最近更新 更多