9

I have a script which automatically creates a new branch with a name based on external information (JIRA ticket). I don't want to create the remote branch until I've committed and pushed some code, but I don't want to have to do "git push --set-upstream origin"

In other words, I want to set the upstream before I push.

git checkout -b mybranch
git <do-something-to-prepare origin/mybranch without talking to origin>
<do work>
git commit -a -m "Made my changes."
git push

I've tried:

git branch --set-upstream-to=origin/mynewbranch

This results in:

error: the requested upstream branch 'origin/mynewbranch' does not exist.

Is there any way to do this?

Daniel
  • 4,235
  • 9
  • 32
  • See: http://stackoverflow.com/questions/1519006/git-how-to-create-remote-branch/1519032#1519032 – D Krueger Feb 27 '14 at 21:38
  • @DKrueger That creates a remote branch before its ready. I'm doing these commands in a script, and I don't want the script creating a remote branch, just preparing it so that I can create the remote by simply typing "git push". – Daniel Feb 27 '14 at 21:41
  • @Daniel So you want to have `origin/myBranch` without `origin` actually having `myBranch`? – Schleis Feb 27 '14 at 21:50
  • @Schleis Yes, that is the heart of it. – Daniel Feb 27 '14 at 21:53

3 Answers3

12

You can do this using the following commands:

git config branch.mybranch.remote origin
git config branch.mybranch.merge refs/heads/mybranch

This essentially configures the same thing as --set-upstream-to without checking that the upstream branch already exists first.

The next step is to change the push.default option, which currently (Git 1.9) defaults to matching (the documentation says this default will change to simple in Git 2.0). Using matching won't do what you want because there is no matching branch at the upstream remote yet. So:

git config push.default simple

You can set this globally (for all your repositories) using the --global switch.

After doing this, a

git push

will push the current branch (and only the current branch) to its upstream (which you set above), creating the upstream branch if necessary.

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • This works exactly as I had wanted! Thanks. I thought I had tried this too, but must have missed some critical part. My push.default was already set to simple. – Daniel Feb 27 '14 at 21:59
2

You can use the -u option when you push to track your local branch

git push -u origin myBranch

http://csurs.csr.uky.edu/cgi-bin/man/man2html?1+git-push

Schleis
  • 34,455
  • 6
  • 60
  • 79
  • This causes the remote to have the branch before its ready (see other answer). Not quite what I'm asking for. – Daniel Feb 27 '14 at 21:42
1

Try --track instead of --set-upstream-to, eg:

git branch --track origin/mynewbranch

I haven't found out exactly what the difference between the two is, but --track seems to do what you're wanting (and what I was wanting to do when I found this question!)

Mark Raymond
  • 686
  • 7
  • 19