Upstream handling in Git

Explicit reference
When no default upstream is defined (see below) or you want to pull from or push to a different upstream, you can always specify it directly. When pushing:


git push
# For example:
git push origin master

# You can also push all branches
git push origin --all

And for pulling, it works exactly the same way.

git pull
# For example:
git pull origin master

You can also pull and push from local branches, just leave the remote away.

However, always having to specify this can be quite tedious, especially if you're not always working with the default names (remote origin and master branch) or are working with multiple branches. To solve this problem, git allows to set a default upstream for each branch. When cloning a remote repository, git will automatically set the default upstream to the repository you cloned from. But that doesn't happen when a new repository is initialized locally or the default upstream needs to be changed. Git provides two ways to define that default upstream location explicitly.

During push
The -u flag for git push allows to tell git the the default upstream is now the branch you are pushing to.

git push -u new_origin master

Git will then automatically report that the upstream has been set to this location.

Using git branch --set-upstream
That command allows you to change the default upstream without having to execute a push command. The command is used like this:

git branch --set-upstream /
# For example:
git branch --set-upstream master new_origin/master