1

I am trying to create a Git alias that will automatically initialize and publish a new branch. The alias is as follows:

[alias]
public-branch = !git checkout -b $1 && git config branch.$1.autosetuprebase always && git config branch.$1.mergeoptions --ff-only && git push -u origin $1

When I run each of the commands in the alias separately, they are successful. However, when running git public-branch feature, I get the following error output:

Switched to a new branch 'feature'
Username for 'https://github.com': my_username
Password for 'https://username@github.com':
error: dst ref refs/heads/feature receives from more than one src.
error: failed to push some refs to 'https://github.com/username/GitTestRepo.git'

The alias is successfully adding the first two config properties but is failing on the push command. I can see in my config file that the tracking information has not been successfully added:

[branch "feature"]
    autosetuprebase = always
    mergeoptions = --ff-only

This error also only occurs when the push command is the last command listed in the alias; if I put either of the git config commands after the push, the alias runs without a hitch. I'm guessing this is just a result of git not parsing the alias correctly but I can't see how to resolve the issue. Any input you can provide is appreciated.

Saber5470
  • 13
  • 4
  • Have you considered using [this format](http://stackoverflow.com/a/3326971/456814) for an alias with positional parameters instead? I'm not sure if that's actually your problem, but if I remember correctly, you can't actually use positional parameters with the alias form that you're currently using. –  Jun 24 '14 at 00:05
  • @Cupcake: that is actually the problem, I think. What happens here is that `git public-branch x` expands to `git checkout ... push -u origin x x`, i.e., the parameters are added on the end of the complete expansion, even though that final `$1` already adds one, so that there are now two. – torek Jun 24 '14 at 01:12
  • possible duplicate of [Git alias with positional parameters](http://stackoverflow.com/questions/3321492/git-alias-with-positional-parameters) –  Jun 24 '14 at 01:29
  • @torek: Yes, that would make sense. It would explain why only the push command was failing when left as the last command in the sequence. – Saber5470 Jun 24 '14 at 03:49

1 Answers1

1

I changed your configuration to use the alias form from this answer to Git alias with positional parameters, and it worked just fine:

[alias]
    public-branch = !sh -c 'git checkout -b $1 && git config branch.$1.autosetuprebase always && git config branch.$1.mergeoptions --ff-only && git push -u origin $1' -

So basically, put all of your commands inside this "wrapper":

!sh -c '<insertCommandsHere>' -

This was the result:

$ git public-branch football
Switched to a new branch 'football'
Total 0 (delta 0), reused 0 (delta 0)
To c:/Users/Keoki/Documents/GitHub/bare
 * [new branch]      football -> football
Branch football set up to track remote branch football from origin.

and in the .git/config file:

[branch "football"]
    autosetuprebase = always
    mergeoptions = --ff-only
    remote = origin
    merge = refs/heads/football

See Also

Community
  • 1
  • 1
  • I should have mentioned that I'm running this on a Windows machine so, sadly, I can't wrap commands in a shell script. – Saber5470 Jun 24 '14 at 01:54
  • @Saber5470 can you not use [Git Bash](http://git-scm.com/download/win)? It's made for Windows. –  Jun 24 '14 at 01:56
  • Thanks Cupcake, that was the solution. When I tried this earlier I missed the trailing '-' character. – Saber5470 Jun 24 '14 at 03:47
  • @Saber5470 people often miss that one `;)` –  Jun 24 '14 at 03:50