是否可以在没有git前缀的情况下运行git命令

时间:2014-08-12 10:22:13

标签: linux git ubuntu

正如标题所说,是否可以启动交互式 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

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

如果您正在使用Bash shell,则可以设置“命令未找到处理程序”,这是一个shell函数,只要无法识别任何命令,它就会运行。如果您运行git-status并且shell无法找到该命令,您可以使用它来尝试运行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

答案 2 :(得分:1)

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

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

和github存储库: https://github.com/thoughtbot/gitsh