-u | --set-upstream
When your branch is not mapped to a remote upstream repository, you can use this configuration to set and push at the same time to push, and if it succeeds push the upstream to be from the remote to which you pushed. If you push multiple branches, everyone who has success will have upstream tracking updated.
I particularly only used the long version, did not know the abbreviation of it.
Reference: link
Usage example (pushing branch feature-upstream
to remote origin
):
git push origin -u feature-upstream
If you're working on a local branch that is not crawled and tries to give a git push
, the git
itself will suggest to you the command you probably want, which would be git push -u
/ git push --set-upstream
:
$ git push
fatal: The current branch feature-upstream has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature-upstream
You can also use git push -u
to push specific branches from one remote to another. For example, I have an internal server origin
, I'd like to send the branch master
from it to the external server gitlab
.
A thicker alternative would be to do:
git checkout origin/master -b master
git push -u gitlab master
This changes my working copy. But you can also directly upload the master
:
git push -u gitlab +origin/master:refs/heads/master
So I can push the branch from one remote to another without having to modify my working copy.
Explaining:
-
+origin/master:refs/heads/master
name of a <refspec>
; the +
sign is optional, its format is +<src>:<dst>
-
origin/master
as <src>
branch source code; it can be a SHA1 too, or anything treeish
-
refs/heads/master
as <dst>
destination indicative; in the case of a branch , it is a reference head, hence refs/heads
; in case, I wanted to save the branch name as master
The following branchs I uploaded the link using the following commands:
# eu estou com a cópia de trabalho no develop
git push -u gitlab develop
# sem mudar minha cópia de trabalho...
git push -u gitlab +origin/master:refs/heads/master
Italsoworked(whenthedevelop
branchintheremotegitlab
alreadyexists):
gitpush-ugitlab+origin/develop:develop
equivalentto:
gitpush-ugitlab+origin/develop:refs/heads/develop
ButIpreferred+origin/develop:develop
becauseit'sverycommonformetoforgetthepluralofheadsandjusttype+origin/develop:refs/head/develop
withheadinthesingular.
It'sworthusing%w/wwhenbranchdoesnotexistontheremoteinquestion.Forexample:
$gitpush-ugitlab+origin/feature-des-bounce:feature-des-bounceerror:unabletopushtounqualifieddestination:feature-des-bounceThedestinationrefspecneithermatchesanexistingrefontheremotenorbeginswithrefs/,andweareunabletoguessaprefixbasedonthesourceref.error:failedtopushsomerefsto'[email protected]:my-awesome-project.git'$gitpush-ugitlab+origin/feature-des-bounce:refs/heads/feature-des-bounceCountingobjects:20,done.Deltacompressionusingupto4threads.Compressingobjects:100%(8/8),done.Writingobjects:100%(20/20),1.72KiB|879.00KiB/s,done.Total20(delta11),reused16(delta7)remote:remote:Tocreateamergerequestforfeature-des-bounce,visit:remote:https://gitlab.com/my-awesome-project/merge_requests/new?merge_request%5Bsource_branch%5D=feature-des-bounceremote:Togitlab.com:my-awesome-project.git*[newbranch]origin/feature-des-bounce->feature-des-bounce
The comment that Ricardo Moraleida left behind summarizes the usage well:
Changing in Kids: It's a configuration of each local branch to determine which remote branch is going to be called if you use git pull or git fetch without arguments. Without this configuration these commands return with error unless you specify the source remote and branch, as in git pull origin master.