1

I have a git alias that is supposed to do a git checkout -b <branchname> (basically create the branch and check it out). My alias looks like:

newbranch = !sh -c 'git checkout -b "$1"'

But when I try git newbranch mytestbbranch I get an error saying that the "b" switch requires an argument.

I have a similar alias for rename that looks like:

rename = !sh -c 'git branch -m "$1" "$2"'

And that one works just fine. I'm confused why the newbranch alias isn't working.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • Duplicate of [git alias with positional parameters (git foo aaa bbb ccc => foo aaa && bar bbb && baz ccc)](http://stackoverflow.com/questions/3321492/git-alias-with-positional-parameters-git-foo-aaa-bbb-ccc-foo-aaa-bar-bbb) and [Pass an argument to a Git alias command](http://stackoverflow.com/questions/7005513/pass-an-argument-to-a-git-alias-command). –  Aug 21 '13 at 01:01

1 Answers1

2

When you use an alias like

!sh -c 'git checkout -b "$1"'

you should need an ending dash -:

!sh -c 'git checkout -b "$1"' -

I don't understand why your other alias works.

According to Advanced aliases with arguments from the Linux Kernel Git wiki:

Starting with version 1.5.3, git supports appending the arguments to commands prefixed with "!", too. If you need to perform a reordering, or to use an argument twice, you can use this trick:

 [alias]
      example = !sh -c 'ls $2 $1' -

The final dash is so that arguments start with $1, not with $0.

See also:

  1. git alias with positional parameters (git foo aaa bbb ccc => foo aaa && bar bbb && baz ccc).
  2. Pass an argument to a Git alias command.
Community
  • 1
  • 1
  • Thanks, I'm not sure why the other one works either in that case...but with the trailing dash all works. I missed that line about the trailing dash. – Jeff Storey Aug 21 '13 at 01:00