【问题标题】:Creating Git Rebase Alias创建 Git Rebase 别名
【发布时间】:2020-10-21 02:10:59
【问题描述】:

我有这个 git 命令,我经常将它用作 zsh 函数。

git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)

我想要实现的是创建一个别名并能够将其称为 git upstream 而不是仅将 $ upstream 作为 zsh 函数调用。我得到的最接近的是:

[alias]
    upstream = "!fn() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) }; fn"

但是,我的猜测是,由于某些解析错误,它在 $(...) 处确实失败了。它显示给我的错误是这样的:

> git upstream
fn() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) }; fn: -c: line 1: syntax error: unexpected end of file

我试图通过别名做的事情可能吗?如果没有,你能指导我找到任何来源来创建git upstream 命令吗?

【问题讨论】:

  • 创建一个脚本git-upstream,然后当你调用git upstream这将调用git-upstream

标签: git zsh alias upstream-branch


【解决方案1】:

在您的本地 bin 目录中,例如 ~/bin 创建一个git-upstream 文件:

#!/bin/sh
git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)

然后让它可执行

chmod u+x git-upstream

现在您可以拨打git upstream

【讨论】:

  • 哦!这很棒,但这是如何工作的? git会自动解析-之后的名字吗?
  • @ogirginc 是的,确实如此,如果你这样做git x,git 会寻找git-x
  • 好吧,反过来——git不解析git-X,它从git X构造git-X
【解决方案2】:

原来我非常接近但错过了一个分号! ?‍♂️

[alias]
  upstream = "!f() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD); }; f"

【讨论】:

  • zsh 允许f () { cmd },但 Git 不使用您的登录 shell;它使用系统默认 shell,需要正确终止正文中的最后一个命令。
猜你喜欢
  • 2020-01-26
  • 1970-01-01
  • 2015-03-21
  • 2015-05-26
  • 1970-01-01
  • 2011-09-11
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多