59
votes

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?

3
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.torek
consider also using a bash autocompletion to TAB away the branch names! zsh with oh-my-zsh has it by default ;)caesarsol
Similar question with additional answer about git aliases: stackoverflow.com/questions/46528736/…NeilG

3 Answers

94
votes

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 '*' …
5
votes

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.

1
votes

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.