In Git, an upstream branch is the default remote branch that your local branch is connected to. Setting an upstream branch helps in keeping your local branch in sync with the remote branch and makes it easier to pull updates from the remote branch.
To set an upstream branch on Git, you can use the git push
command with the --set-upstream
or -u
option followed by the name of the remote branch. Here’s an example:
- First, check which branch you are currently on by running the
git branch
command
$ git branch * main feature-branch
- Next, run the
git push
command with the--set-upstream
or-u
option followed by the name of the remote branch:
$ git push --set-upstream origin feature-branch
This sets the upstream branch of your local feature-branch
to the origin
remote’s feature-branch
.
Alternatively, you can use the -u
option as shorthand:
$ git push -u origin feature-branch
- Finally, you can verify that the upstream branch has been set correctly by running the
git branch -vv
command:
$ git branch -vv main 0123456 [origin/main] Commit message here * feature-branch 789abc0 [origin/feature-branch] Another commit message here
The origin/feature-branch
label next to your local branch indicates that it is tracking the remote feature-branch
branch as its upstream branch.