What is the purpose of git push -u?

15

I have already observed several people asking questions about possible GIT errors and I noticed that sometimes it involves -u . For example Error in Android Studio 2.3 integration with GitLab and Error uploading files to the remote server .

Most of the time the person is in a learning phase in relation to the GIT and searches for any tutorial on the internet and / or does not read or even know what the -u means or any other GIT option

What is the goal of git push -u ? How should it actually be used?

    
asked by anonymous 06.06.2017 / 17:14

1 answer

15
-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(whenthedevelopbranchintheremotegitlabalreadyexists):

gitpush-ugitlab+origin/develop:develop
    

equivalentto:

gitpush-ugitlab+origin/develop:refs/heads/develop
    

ButIpreferred+origin/develop:developbecauseit'sverycommonformetoforgetthepluralofheadsandjusttype+origin/develop:refs/head/developwithheadinthesingular.

    

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.

    
06.06.2017 / 17:29