What’s the point of git push -u?

Asked

Viewed 7,076 times

20

I’ve seen a lot of people asking questions about possible errors in GIT and noticed that sometimes it involves -u. For example Error in Android Studio 2.3 Integration with Gitlab and Error while uploading files to remote server.

Most of the time the person is in a learning phase in relation to GIT and looks for any tutorial on the internet and/or doesn’t even read or even know what the -u or any other GIT option.

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

1 answer

21


-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 push succeeds, set the upstream as being from the remote to which pushed. 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 its abbreviation.

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

Example of use (pushing the branch feature-upstream to the remote origin):

git push origin -u feature-upstream

In case what you’re working on is an untraceable local branch and try to give a git push, own git 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 the git push -u to push branchs specific from one remote to another. For example, I have an internal server origin, would 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 it is also possible to 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 of + is optional, its format is +<src>:<dst>
  • origin/master as <src>
    source code for branch; can be an SHA1 too, or anything treeish
  • refs/heads/master as <dst>
    destination code; in the case of a branch, he’s a reference head, hence refs/heads; in case, I wanted to save the name of branch as master

The following branchs I climbed into the 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

demonstrando que os branchs master e develop foram empurrados

It also worked (when the branch already exists develop 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 just type +origin/develop:refs/head/develop with head in the singular.

It’s worth using the with refs/heads when the branch does not exist in 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 commenting that Ricardo Moraleida left well summarizes the use:

Switching in kids: it’s 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 setting these commands return with error unless you specify the remote and source branch, such as git pull origin master.

  • 5

    Switching in kids: is a configuration of each local branch to determine which remote branch will be called if you use git pull or git fetch no arguments. Without this setting these commands return with error unless the remote and branch of origin, as in git pull origin master.

  • 1

    @Ricardomoraleida , I will update my answer with this, I found it too pertinent

  • 1

    Thanks! Terms like "upstream tracking" work for people who know what it is, for those who are starting it is good to be more didactic :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.