What purpose of git push-u?

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

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

What purpose of git push -u? How in fact should it be used?

Author: viana, 2017-06-06

1 answers

-u | --set-upstream

When your branch is not mapped to a remote upstream repository, you can use this setting to set and push at the same time to push, and if the push succeeds, set the upstream to be from the remote you pushed to. If you push multiple branches, everyone who has succeeded will have upstream tracking updated.

I particularly only used the long version, I didn't know the abbreviation of it.

Reference: https://git-scm.com/docs/git-push#git-push--u

Usage example (pushing branch feature-upstream to remote origin):

git push origin -u feature-upstream

In case you are working in an untracked local branch and trying to give a git push, git itself will suggest to you the command you probably want, which would be the 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 branchs from one remote to another. For example, I have an internal server origin, I would like to send the branch master from him 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 it is also possible to directly raise 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>; sign of + is optional, its format is +<src>:<dst>
  • origin/master as <src>
    source code of the Branch ; can be a SHA1 as well, or anything treeish
  • refs/heads/master as <dst>
    target callsign; in the case of a Branch, it is a reference head, hence refs/heads; in the case, I wanted to save the name of the Branch as master

The following branchs I climbed on https://gitlab.com 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

demonstrating that the master and develop branches were pushed

Also worked (when there is already branch develop in the remote gitlab):

git push -u gitlab +origin/develop:develop

Equivalent to:

git push -u gitlab +origin/develop:refs/heads/develop

But I preferred the +origin/develop:develop because it is very customary for me to forget the plural of heads and type only +origin/develop:refs/head/develop with head in the singular.

It is worth using the com refs/heads when the Branch does not exist on the remote in question. For example:

$ git push -u gitlab +origin/feature-des-bounce:feature-des-bounce
error: unable to push to unqualified destination: feature-des-bounce
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to '[email protected]:my-awesome-project.git'
$ git push -u gitlab +origin/feature-des-bounce:refs/heads/feature-des-bounce
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (20/20), 1.72 KiB | 879.00 KiB/s, done.
Total 20 (delta 11), reused 16 (delta 7)
remote:
remote: To create a merge request for feature-des-bounce, visit:
remote:   https://gitlab.com/my-awesome-project/merge_requests/new?merge_request%5Bsource_branch%5D=feature-des-bounce
remote:
To gitlab.com:my-awesome-project.git
 * [new branch]      origin/feature-des-bounce -> feature-des-bounce

The comment that Ricardo Moraleida left summarizes the use well:

Swapping in kids: it is a configuration of each local branch to determine which remote branch will 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.

 21
Author: Jefferson Quesado, 2018-06-03 19:53:54