71

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?

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
Lee Hampton
  • 1,039
  • 9
  • 7
  • 2
    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](https://www.kernel.org/pub/software/scm/git/docs/git-config.html). – torek Oct 22 '13 at 19:46
  • consider also using a bash autocompletion to TAB away the branch names! zsh with oh-my-zsh has it by default ;) – caesarsol Nov 04 '15 at 18:41
  • Similar question with additional answer about git aliases: https://stackoverflow.com/questions/46528736/git-alias-returns-an-error-when-using-pipe-command/ – NeilG Aug 22 '19 at 03:57
  • 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) – MichealKum Jul 07 '23 at 09:03

3 Answers3

117

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 '*' …
Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 3
    "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 book](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases) – nietaki Nov 04 '16 at 11:09
  • 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 \\\\n` – NeilG Aug 22 '19 at 03:26
  • Getting the string properly escaped may be a hell sometimes, yes… – Michael Krelin - hacker Aug 22 '19 at 08:09
  • 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 |`.... – chicks Jan 05 '21 at 18:28
  • 1
    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`. – Gabriel Staples Mar 10 '21 at 23:45
  • Done. Example of what I was talking about in my previous comment: https://stackoverflow.com/a/66574807/4561887 – Gabriel Staples Mar 11 '21 at 01:03
  • 2
    @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`. – Michael Krelin - hacker Mar 11 '21 at 17:24
  • @hacker, perfect. Thanks. This article talks about that too it looks like, under the section "Git Aliases with Functions": https://mikebarkas.dev/2018/git-alias-bash-functions-with-arguments/. – Gabriel Staples Mar 11 '21 at 17:58
  • 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 :) – Michael Krelin - hacker Mar 11 '21 at 21:03
  • this works for me if I don't leave a space between ! and git, so !git myalias | sed 's/ago//' – Mz A Nov 24 '21 at 08:29
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.

forivall
  • 9,504
  • 2
  • 33
  • 58
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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Leon
  • 3,124
  • 31
  • 36