1

I am a bit wondering about the naming "upstream" from git.

If I create a local branch

git checkout -b branch_local

and want to push it somewhere

git push remote_id branch_local:branch_remote

If I look which remote the branch follows:

git branch -vv --all

I see that there is nothing set. If I try:

git pull

I get

If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=<remote>/<branch> branch_local

Why I have to set the "upstream" to get the "downstream" set? Maybe I have not understood if there is an additional downstream setting?

Klaus
  • 24,205
  • 7
  • 58
  • 113

3 Answers3

2

"Upstream" refers to the remote the branch will push and pull by default. This is also called the "remote tracking branch". Sometimes Git will set this for you when you create the branch, but it depends on how you're configured.

In order to ensure the upstream branch is set, use git push -u the first time you push.

git push -u remote branch_local:branch_remote

You can change branch.autoSetupMerge to make Git do this automatically. true will set up tracking when you branch from a remote branch like git co -b foo remote/foo. always will always do the tracking.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Comming from: http://stackoverflow.com/questions/2739376/definition-of-downstream-and-upstream I feel that up and down are two different ways. Here it feels that is simply a link from local to remote, for up and down! That is my question,,, – Klaus Nov 06 '15 at 18:42
  • @Klaus "Upstream" and "downstream" are just different directions over the same pipe. `push` sends information "upstream" to the remote and `fetch` (which is what `pull` does) fetches it "downstream" to you. *Technically* you can set a different remotes for push and fetch (which is what pull does). Usually you don't. Git uses `branch..remote` for fetching (downstream) and pushing (upstream) but `branch..pushRemote` can provide a different push (upstream) target. – Schwern Nov 06 '15 at 18:44
  • I play currently a bit to get more fundamental understanding. So I try to get multiple remotes in sync, make local and remote branches with different names and so on. It is my target to get more then the basics here. Understanding starts with playing :-) – Klaus Nov 06 '15 at 18:48
  • 1
    @Klaus Apologies, I didn't realize you were the one who asked the question. I thought you were someone trying to point out extra, unnecessary detail. I've revised my comment with some more detail. Long story short, Git uses a single remote URL to both fetch (pull) and push... unless you tell it otherwise. When Git refers to "upstream" that is the remote repository and your local repository is "downstream". – Schwern Nov 06 '15 at 18:50
  • @VonC I don't use Twitter anymore and I don't remember the DM rules. This is a yes please! – Schwern Nov 10 '15 at 18:43
0

You need to tell git to have your local branch track the remote branch. Git being a distributed system, you can make plenty of local branches that don't track any upstream changes.

What you've done is push your local branch to the remote repository, but the local is still not tracking the upstream. There's no "downstream" per se, upstream is the git terminology for the remote repository you're tracking. Once you set that, both push and pull should work (assuming fetch and push urls are the same, but I'm guessing they are at this stage)

blackbird
  • 1,129
  • 1
  • 23
  • 48
  • quit clear of what is happening, but I am curious why there is the meaning of "set upstream" to get the downstream set. In a short: I see how it works and I can work with it. But I fear that there is a misunderstanding of up/down stream. – Klaus Nov 06 '15 at 18:39
  • @Klaus I think it's just the wording that's confusing you, downstream makes no sense in a git perspective because no one is actively pushing to you, you're the one always requesting from a remote server – blackbird Nov 06 '15 at 18:42
  • downstream makes sense in the case of having multiple remote servers. So setting the "downstream" will tell git only merge from the given remote branch. Maybe there are more then one! As mentioned above, upstream is wrong in wording and means more a "link" between local branch and exactly one of multiple branches or multiple remotes ( with also multiple branches ) – Klaus Nov 06 '15 at 18:45
0

Why I have to set the "upstream"

As of git 2.37+, you no longer need to if you set a new configuration parameter:

git config --global push.autoSetupRemote true

With this set, a simple git push in your example above (with branch_local checked out) will push that to the remote (origin/branch_local), and set the tracking branch relationship.


In previous versions of git or without this config, you need to do a more complex push like git push -u origin HEAD, where the -u sets the tracking relationship and the explicit origin and branch are required because the tracking relationship doesn't exist yet.

(assuming that the push.default config is at its default setting of simple)

Tao
  • 13,457
  • 7
  • 65
  • 76