Pipes in a Git alias?
Asked Answered
C

3

74

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:

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

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

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?

Cariotta answered 22/10, 2013 at 18:18 Comment(4)
Not related to solving the general problem, but in reasonably recent versions of git, you can configure push.default (or push.origin.default to limit the effect to origin) to current (although upstream might sometimes be more appropriate). See the push.default section in git-config.Barrelhouse
consider also using a bash autocompletion to TAB away the branch names! zsh with oh-my-zsh has it by default ;)Tolkan
Similar question with additional answer about git aliases: stackoverflow.com/questions/46528736/…Drillmaster
I use two aliases to push current pranch: cb = rev-parse --abbrev-ref HEAD push-me = ! git push origin $(git cb) push-mef = ! git push --force origin $(git cb)Aspersion
C
122

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

[alias]
    pushcur = ! git branch | grep '*' …
Culbert answered 22/10, 2013 at 18:20 Comment(10)
"However, maybe you want to run an external command, rather than a Git subcommand. In that case, you start the command with a ! character." - git bookCantata
This question is the precise reason I came here but the bang pipe quote combination I'm trying fails with "fatal bad config line". I think the real problem is due to the way I'm using quotes. I worked around by eliminating quotes by changing my sed pipe to a tr pipe. I had to double double escape the backslash so used ...| tr \\\\nDrillmaster
Getting the string properly escaped may be a hell sometimes, yes…Culbert
Using the ! allowed me to include pipes in my alias, but it doesn't maintain the current directory. You have to include $GIT_PREFIX to get back to where you started for subcommands like ls-files I had to do git ls-files $GIT_PREFIX |....Varioloid
How can you pass in input args? This (adding "$@") pushcur = ! git branch "$@" | grep '*' … doesn't seem to work. I think I'll just use an actual bash command that I put into ~/bin/git-pushcur instead of a git alias then. This sitll allows calling it as git pushcur.Marceau
Done. Example of what I was talking about in my previous comment: https://mcmap.net/q/13000/-hide-but-still-save-a-branch-with-gitMarceau
@GabrielStaples, generally params are appended to the command, but if you want to put them elsewhere, the trick is to create bash function like par321 = "!par321() { echo $3 $2 $1 ; } ; par321" then git par321 1 2 3 will output 3 2 1.Culbert
@hacker, perfect. Thanks. This article talks about that too it looks like, under the section "Git Aliases with Functions": mikebarkas.dev/2018/git-alias-bash-functions-with-arguments.Marceau
Aha, I came up with this way before the article was published as probably thousands or millions of other git users (how many are there?) who faced the same problem :)Culbert
this works for me if I don't leave a space between ! and git, so !git myalias | sed 's/ago//'Voroshilovgrad
E
6

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

Eureka answered 22/10, 2013 at 18:26 Comment(0)
T
1

A simple workaround is to add it as 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)

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

Tuyettv answered 16/8, 2018 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.