【问题标题】:Is it possible to run git commands without git prefix是否可以在没有 git 前缀的情况下运行 git 命令
【发布时间】:2014-08-12 10:22:12
【问题描述】:

正如标题所说,是否可以启动一个 interactive git shell,其中所有命令都自动以git为前缀?

所以不要这样做:

git fetch
git add
git commit

我希望能够做这样的事情:

git -i  #start the 'interactive' git shell, not the right command obviously

fetch   #does git fetch
add     #does git add
commit  #does git commit

git -x  #exit the 'interactive' git shell

【问题讨论】:

  • 你为什么要这样做? git 的输入并不多(您可以创建一个 alias g=git 以使其更短)。你的 shell 应该如何处理诸如lscd 之类的命令?但据我所知,Git 没有这样的功能。
  • @knittl 与例如相同的方式ftp 是吗?假设任何命令是针对该程序的,如果该命令未被识别,则发出错误消息。我在问题中没有看到任何表明非 Git 命令仍然需要工作的内容。
  • @knittl,你可以让 shell 处理 lscd 完全正常,但对于任何无法识别的命令,请尝试以 git 作为前缀运行它。
  • rm 应该怎么做?我可以看到输入更短的命令有一些小用处,但如果它失去了将git 命令与非git 命令混合的能力,则不会。
  • 这个交互式shell不需要处理非git命令。

标签: linux git ubuntu


【解决方案1】:

我不认为这种模式集成在 git 中。我建议你检查git-sh。您可以将其配置为使用您喜欢的别名。

【讨论】:

  • 我不想使用 git-sh 因为我必须重新定义所有别名,我正在寻找一个解决方案,git 或其他工具,它可以让我启动一个交互式 shell 和前缀他们用'git',
【解决方案2】:

如果你使用 Bash shell,你可以设置一个“command not found handler”,这是一个 shell 函数,当任何命令未被识别时就会运行。如果您运行 status 并且 shell 找不到该命令,则可以使用它来尝试运行 git-status,例如

command_not_found_handle() {
    gitcmd=`git --exec-path`/git-$1 ;
    if type -a $gitcmd >/dev/null  2>&1 ;
    then
        shift ;
        exec $gitcmd "$@" ;
    fi ;
    echo "bash: $1: command not found" >&2 ;
    return 1 ;
}

这不会扩展git 别名,它只识别在GIT_EXEC_PATH 目录中作为可执行文件存在的命令,例如/usr/libexec/git-core/git-status

master*% src$ pwd
/home/jwakely/src/foo/src
master*% src$ git status -s
 M include/foo.h
?? TODO
master*% src$ status -s      # runs 'git-status -s'
 M include/foo.h
?? TODO
master*% src$ git st         # a git alias
 M include/foo.h
?? TODO
master*% src$ st             # does not recognize git alias
bash: st: command not found

如果您希望它处理别名,但缺点是 任何 无法识别的命令(包括拼写错误)将被传递给 Git,您可以让它变得更简单:

command_not_found_handle() { git "$@" ; }

master*% src$ st            # runs 'git st'
 M include/foo.h
?? TODO
master*% src$ statu         # runs 'git statu'
git: 'statu' is not a git command. See 'git --help'.

Did you mean one of these?
        status
        stage
        stash

【讨论】:

  • 这听起来很接近我想要的,但是有没有办法阻止非 git 命令运行?例如:使用上述运行重置运行正常重置而不是 git 重置。这些命令是否也可以自动完成?
  • 听起来你想要 git-sh 代替。您可以PATH 中删除所有内容,并为每个命令调用command-not-found 处理程序,并让它调用usr/bin/git(或您机器上的任何绝对路径)。您可以将 bash 自动完成配置为对 reset 执行与 git reset 相同的自动完成,但这需要大量工作。
【解决方案3】:

gitsh 是,也许,你在找什么。

http://robots.thoughtbot.com/announcing-gitsh

还有 github 仓库: https://github.com/thoughtbot/gitsh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2019-08-19
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多