bash git别名中的管道?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19525387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:23:02  来源:igfitidea点击:

Pipes in a git Alias?

gitbashunix

提问by Lee Hampton

I work on feature branches that have annoying big names, so I often end up grepping my current branch name when I need to push up to the feature branch from my local feature branch, like so:

我在具有恼人的大名称的功能分支上工作,所以当我需要从本地功能分支推送到功能分支时,我经常最终会搜索我当前的分支名称,如下所示:

git branch | grep '*' | sed 's/* //' | xargs git push origin

This works fine. I want to make this an alias, so I did this in ~/.gitconfig:

这工作正常。我想把它设为别名,所以我在 ~/.gitconfig 中做了这个:

[alias]
   pushcur = branch | grep '*' | sed 's/* //' | xargs git push origin

Now, when I run git pushcur, I get the following error:

现在,当我运行时git pushcur,出现以下错误:

usage: git branch [options] [-r | -a] [--merged | --no-merged]

Leading me to believe that the alias is not properly parsing the pipes. Is there something else I should do to achieve the desired alias?

让我相信别名没有正确解析管道。我还应该做些什么来实现所需的别名?

回答by Michael Krelin - hacker

I don't think you can, but you can prefix it with an !to treat the command as a new shell command

我不认为你可以,但你可以在它!前面加上前缀以将命令视为新的 shell 命令

[alias]
    pushcur = ! git branch | grep '*' …

回答by forivall

I typically make small git-scripts and put them in a directory that's in my path (~/.local/bin). Check out git-extrasfor a bunch of good examples.

我通常制作小git-脚本并将它们放在我的路径 ( ~/.local/bin)中的目录中。查看git-extras以获得一堆很好的例子。

回答by Leon

A simple workaround is to add it as shell alias.

一个简单的解决方法是将其添加为shell alias

Here is an example:
alias grf="git rebase -i $(git merge-base --fork-point master)"
(rebase on the fork commit of current branch and master interactively)

这是一个例子:(
alias grf="git rebase -i $(git merge-base --fork-point master)"
基于当前分支和 master 的 fork 提交交互)

For bash, add it to ~/.bashrc, then you can simply use grf.

对于 bash,将其添加到 ~/.bashrc,然后您可以简单地使用grf.