2

What I currently do is a common work scenario for me. When I create a new feature branch, I want to start at point 'develop' and set upstream branch with the same name as my feautre branch so it goes like

git checkout -b feature/MyFeature origin/develop --no-track

and with the first push I do

git push -u origin feature/MyFeature

I am wondering if it is possible to create new branch with given starting point and setting upstream with matching name in single git command ?

something like

git checkout -b fastBranch --start-at origin/develop --set-upstream origin/fastBranch

and favoreably with the ability to ommit repeating myself regarding origin/fastBranch part

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Because you've just *created* `feature/MyFeature` locally and (presumably) there is no `feature/MyFeature` in the Git repository at `origin`, there is no `origin/feature/MyFeature` in your own repository yet. You therefore cannot set the upstream of `feature/MyFeature` to `origin/feature/MyFeature`: before you can set *U* as some upstream, *U* must exist. To create it, you'll use `git push`. You could create an alias that: (1) creates a branch from some starting commit with `--no-track` as you show, then (2) runs `git push origin --set-upstream` to create the upstream *U* and set it. – torek Dec 20 '22 at 10:14
  • Alternatively, you can change `push.default` so that the upstream need not be set. Be aware that, while this works fine *if you're careful*, it has caused some people some trouble, which is why it's not the default. – torek Dec 20 '22 at 10:14
  • That is true. Aldough it fits my needs somehow as I dont have to repeat myself (with the exception to remember to do first `push -u` I feel that this is NOT something I should always do as it might be error prone. As for the first comment, you are totally correct and it was my fault of not knowing that I cannot set upstream to non existing one (that would in my head create itself when pushing). This explains why I could find an example of doing so as it is impossible by design :) – Antoniossss Dec 20 '22 at 12:01
  • It's actually a bit annoying that Git won't let you say "this is the name, even though it doesn't exist yet" since the *rest* of Git handles that just fine. (To test that out, use `git config` to set the name in the two parts and observe that it works. But since it's not officially supported, perhaps in the future it will stop working. Unless, that is, you can get it to become officially supported.) – torek Dec 20 '22 at 12:02

0 Answers0