What does git --set-upstream
do?
I tried to understand it by reading the git manual, but I didn't quite get it.
The git set-upstream allows you to set the default remote branch for your current local branch. By default, every pull command sets the master as your default remote branch.
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.
Upstream and downstream in Git are usually defined with the context of a repository. In general terms, upstream refers to the location through which we clone the repository. The term downstream is defined as the process of integrating our work with other works.
Setup Git Upstream For a Repository Let's say you are working on a forked project and you want to sync changes from the main project repo. You can add the actual repo as an upstream to your local copy. This way you can pull all the changes happening in the main project repo.
To avoid confusion,
recent versions ofgit
deprecate this somewhat ambiguous--set-upstream
option
in favor of a more verbose--set-upstream-to
option
with identical syntax and behavior.
[ Reference ]
git branch --set-upstream-to <remote-branch>
sets the default remote branch for the current local branch.
Any future git pull
command (with the current local branch checked-out),
will attempt to bring in commits from the <remote-branch>
into the current local branch.
One way to avoid having to explicitly type --set-upstream
/ --set-upstream-to
is to use its shorthand flag -u
as follows:
git push -u origin local-branch
This sets the upstream association for any future push/pull attempts automatically.
For more details, checkout this detailed explanation about upstream branches and tracking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With