创建分支并推送它 git 别名

Create branch and push it git alias

如何创建一个别名来创建本地分支并将其推送到上游?我试过了

publish = !git checkout -b  && git push -u origin 

我明白了

Switched to a new branch 'mybranch/test'
error: dst ref refs/heads/mybranch/test receives from more      than one src.
error: failed to push some refs to 'ssh://myurl'

您的最终命令结果是 git checkout -b mybranch/test && git push -u origin mybranch/test mybranch/test,因为 </code> 被替换为第一个参数,并且该参数也被添加到命令的末尾。要么省略最后一个 <code>,这样你就有

publish = !git checkout -b  && git push -u origin

或者将您的命令链包装在一个函数中,例如

publish = !publish_new_branch() { [ $# -ne 1 ] && echo 'error: publish needs exactly one argument' >&2 && exit 1; git checkout -b \"\" && git push -u origin \"\"; }; publish_new_branch

我通常更喜欢后一种选择,因为它更清楚会发生什么。

我使用它是因为我经常使用遥控器。第一个参数是遥控器;第二个是你的分支名称。 "nb" 用于 "new branch"。

# ~/.gitconfig

[alias]
  nb = "!sh -c \"git checkout -b ; git push -u  \" -"

用法:

git nb origin my-new-branch