Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git - push current vs. push upstream (tracking)

Tags:

git

I have read the git man about push command, but I still don't understand the EXACT difference between current and upstream to be set in the push.default

I want that our team will just do push, and only changes on the branch that they are currently working on, will be pushed. As I understand, this branch is the one that marked with * (star) when I do git branch.

Thanks for helping out.

like image 956
Eyal Golan Avatar asked Dec 06 '12 19:12

Eyal Golan


People also ask

Does git push push to upstream?

When you set your upstream (or tracking) branches, you can simply execute pulls and pushes without having to specify the target branch. Git automatically knows that it has to fetch the new commits to the remote tracking branch. Similarly, Git already knows that it has to push new commits to the upstream branch.

What is upstream tracking in git?

Option -u or --set-upstream allows to add upstream (tracking) reference for every branch that is up to date or successfully pushed. For example, my local repository is checkout in issue-1 : if I want to push it to origin and add upstream reference as origin/issue-1 , I can do: $ git push -u origin issue-1.

What does it mean to push upstream in git?

pushing " upstream " means that your current branch B has remote/B has its upstream branch. Ie: branch. B. merge is set, when your are pushing the " upstream " branch. Ie: when pulling to B , git knows what branch to pull (as well as which remote repo: branch.B.remote )

Does git push push current branch?

By default, Git chooses origin for the remote and your current branch as the branch to push. If your current branch is main , the command git push will supply the two default parameters—effectively running git push origin main .


1 Answers

The question is what are you pushing, and to where:

  • current:

    • "what" is only your current branch (no other branches),
    • "to where" is a branch of the same name (created if it doesn't exist) in the upstream repo.
  • upstream:

    • "what" is also only the current branch,
    • "to where" is to whatever branch (not necessarily of the same name) on the upstream repo has been assigned as an upstream branch for the local branch you are pushing.

As explained here, Git2.0 will additionally introduce a new default for push.default: simple

simple is like upstream, but the upstream has to have the same name as well or the push will fail.


Pushing only one branch (with the mode "simple", "current" or "upstream") avoids the scenario where all matching branches are pushed (mode "matching", which was the default for a long time), even though some of your branches might not be ready to be pushed.

(master)> git push ... To [email protected]:jkubicek/my_proj.git    21b430d..dd378ca  master -> master  ! [rejected]        release -> release (non-fast-forward) error: failed to push some refs to '[email protected]:jkubicek/my_proj.git' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart.  hint: If you did not intend to push that branch, you may want to hint: specify branches to push or set the 'push.default' configuration hint: variable to 'current' or 'upstream' to push only the current branch. 

The difference between the two (current and upstream) is in the pull (what to pull from the remote to your branch?):

  • pushing "current" doesn't mean that your current branch B has remote/B has its upstream branch.
    Ie: branch.B.merge isn't set, when your are pushing the "current" branch.
    Ie: when pulling to B, git won't know what branch to pull.

  • pushing "upstream" means that your current branch B has remote/B has its upstream branch.
    Ie: branch.B.merge is set, when your are pushing the "upstream" branch.
    Ie: when pulling to B, git knows what branch to pull (as well as which remote repo: branch.B.remote)

like image 70
VonC Avatar answered Sep 28 '22 08:09

VonC