3

my workflow currently after checkout -b <new_branch> from master, I will push to <new_branch>, but one thing annoy me is that git force me to do

git push --set-upstream origin <new_branch>

how can I skip it by default? I don't see the point of doing that.

Nadiely Jade
  • 195
  • 2
  • 9
  • There are [multiple plus points for setting an upstream](https://stackoverflow.com/q/37770467/1256452), but if you have no interest in any of them, see [Dev-vruper's answer]. – torek Dec 14 '20 at 20:21

3 Answers3

4

From the Git Push.Default Documentation ,

Defines the action git push should take if no refspec is given (whether from the command-line, config, or elsewhere). Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want. Possible values are:

nothing - do not push anything (error out) unless a refspec is given. This is primarily meant for people who want to avoid mistakes by always being explicit.

current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

So you can do below and set it to

git config --global push.default current

Dev-vruper
  • 421
  • 3
  • 12
1

In any case you have to push your new branch to the remote repo, but to make things shorter you can do following:

  • git push -u origin <new_branch> (instead of --set-upstream)
  • git push -u origin HEAD Pushing to HEAD is equivalent to pushing to a remote branch having the same name as your current branch
  • You also can create an alias for this commands, to do so check this out: https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
Monsieur Merso
  • 1,459
  • 1
  • 15
  • 18
0

Starting with git 2.37.0, you can run this command once, in order to avoid having to specify ``--set-upstream`:

git config --global --add --bool push.autoSetupRemote true

From the documentation:

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch; this option takes effect with push.default options simple, upstream, and current. It is useful if by default you want new branches to be pushed to the default remote (like the behavior of push.default=current) and you also want the upstream tracking to be set. Workflows most likely to benefit from this option are simple central workflows where all branches are expected to have the same name on the remote.

Mike Morearty
  • 9,953
  • 5
  • 31
  • 35