Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does the "u" do? "git push -u origin master" vs "git push origin master"

Tags:

git

I'm apparently terrible at using git, despite my best attempts to understand it.

From kernel.org for git push:

-u

--set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge in git-config(1).

Here's branch.<name>.merge from git config:

branch.<name>.merge

Defines, together with branch.<name>.remote, the upstream branch for the given branch. It tells git fetch/git pull which branch to merge and can also affect git push (see push.default). When in branch <name>, it tells git fetch the default refspec to be marked for merging in FETCH_HEAD. The value is handled like the remote part of a refspec, and must match a ref which is fetched from the remote given by "branch.<name>.remote". The merge information is used by git pull (which at first calls git fetch) to lookup the default branch for merging. Without this option, git pull defaults to merge the first refspec fetched. Specify multiple values to get an octopus merge. If you wish to setup git pull so that it merges into <name> from another branch in the local repository, you can point branch.<name>.merge to the desired branch, and use the special setting . (a period) for branch.<name>.remote.

I successfully set up a remote repository with github, and I successfully pushed my first commit to it with:

git push -u origin master 

Then, I unwittingly successfully pushed my second commit to my remote repository using:

git commit -m '[...]' 

However, incorrectly thinking I would have to push again to origin from master, I ran:

# note: no -u git push origin master 

What did that do? It didn't seem to have any effect at all. Did I "undo" git push -u origin master?

like image 599
ClosureCowboy Avatar asked Apr 18 '11 01:04

ClosureCowboy


People also ask

What is the difference between git push origin Main and git push origin master?

Git Push Origin pushes all the branches to the main branch. Git Push Origin Master pushes your master branch to the origin. Behavior could be changed via git config. Behaviour is by default.

What does git push U origin master do?

The git push -u <remote> <branch name> command uploads content from a local repository to a remote repository. It is generally used to upload modifications in a local repository with remote team members.

What is the difference between origin master and master in git?

Master: This is a branch name where we first initiate git and then we use to make commits. And the changes in the master can pull/push into a remote. origin/master: This is a remote branch, which has a local branch named master on a remote named origin.

What does push origin master mean?

With git push origin master you tell git to push all of the commits in the currently checked out local branch (i.e. from your file system) to the remote repo identified by the name origin on its remote branch named master .


1 Answers

The key is "argument-less git-pull". When you do a git pull from a branch, without specifying a source remote or branch, git looks at the branch.<name>.merge setting to know where to pull from. git push -u sets this information for the branch you're pushing.

To see the difference, let's use a new empty branch:

$ git checkout -b test 

First, we push without -u:

$ git push origin test $ git pull You asked me to pull without telling me which branch you want to merge with, and 'branch.test.merge' in your configuration file does not tell me, either. Please specify which branch you want to use on the command line and try again (e.g. 'git pull <repository> <refspec>'). See git-pull(1) for details.  If you often merge with the same branch, you may want to use something like the following in your configuration file:      [branch "test"]     remote = <nickname>     merge = <remote-ref>      [remote "<nickname>"]     url = <url>     fetch = <refspec>  See git-config(1) for details. 

Now if we add -u:

$ git push -u origin test Branch test set up to track remote branch test from origin. Everything up-to-date $ git pull Already up-to-date. 

Note that tracking information has been set up so that git pull works as expected without specifying the remote or branch.

Update: Bonus tips:

  • As Mark mentions in a comment, in addition to git pull this setting also affects default behavior of git push. If you get in the habit of using -u to capture the remote branch you intend to track, I recommend setting your push.default config value to upstream.
  • git push -u <remote> HEAD will push the current branch to a branch of the same name on <remote> (and also set up tracking so you can do git push after that).
like image 127
dahlbyk Avatar answered Sep 30 '22 23:09

dahlbyk