【问题标题】:How can I make this git command alias?我怎样才能使这个 git 命令别名?
【发布时间】:2011-12-21 19:32:57
【问题描述】:

我想做一个别名,像下面这样

gc this is a test message 转换为git commit -m "this is a test message"

我该怎么做?我希望在我的 bashrc 中使用它。

【问题讨论】:

    标签: git bash alias


    【解决方案1】:

    bash alias 定义不带参数。

    尝试在 .bashrc 中使用 bash 函数:

    function gc () { 
        git commit -m "$*" 
    }
    

    【讨论】:

    • 我喜欢并尝试过这个,但在我的 bash 环境中不起作用。你知道我为什么会收到这个错误信息吗? : error: path spec '' did not match any file(s) known to git. .. 我这样运行它 $ gc test commit message here .. 我还尝试为函数创建别名
    • 我的猜测是,以某种方式,生成的调用在您预期之前有一个结束引号。您可以尝试在函数中的实际行之前添加一行 echo "git commit -m "$*" 以查看生成的内容。
    • 感谢您回来。我用回声尝试了它,然后我看到它工作了..我之前一定错过了一些东西,但现在它工作了。仅供参考,您的回声,我认为您错过了一个报价,我不确定它们是否嵌套;所以我这样做了:echo "git commit -m \"$*\" "
    【解决方案2】:

    这不是别名,而是试试

    function gc() {
      git commit -m "$*"
    }
    

    【讨论】:

      【解决方案3】:

      我的 .bashrc 中有这些别名:

      alias ga='git add'
      alias gp='git push'
      alias gl='git log'
      alias gs='git status'
      alias gd='git diff'
      alias gdc='git diff --cached'
      alias gm='git commit -m'
      alias gma='git commit -am'
      alias gb='git branch'
      alias gc='git checkout'
      alias gra='git remote add'
      alias grr='git remote rm'
      alias gpu='git pull'
      alias gcl='git clone'
      

      我通常使用gm "msg"提交

      【讨论】:

      • 很好,感谢分享这些。我喜欢在 CWD 上方有其他文件夹时在当前目录上获取 git 状态我不在项目中工作,所以添加了这个:alias gs='git status .' .. 然后因为它是我的 #1 git cmd,这甚至更快@ 987654324@
      【解决方案4】:

      这应该可行:

      alias ci = "!f() { git commit -m \"$*\"; }; f"
      

      不幸的是 gc 已经是一个子命令,不能别名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        • 2014-01-05
        • 1970-01-01
        • 2013-07-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多