Setting up a local branch to track a remote branch in Git

Set up upstream branch for a branch

To set up a local branch to track a remote branch, use :

# Suppose you are on local temp branch
# Set up current branch temp to track topic branch from origin.
$ git branch -u origin/test

Now origin/topic is the current branch’s upstream branch, you can use @{u} to reference it for shortcut instead of origin/topic.

# Merge from upstream branch, 
# here it is origin/topic if you are on local temp branch.
$ git merge @{u}

Check which remote branch a local branch is tracking

If you want to see which remote branch a local branch is tracking, run below command:

# List local branches and which remote branches they are tracking.
$ git branch -vv
develop 99914fb [origin/develop: ahead 2] fix: missing comma
master 04a4f6a [origin/master: ahead 1, behind 2] feat: add menu
* temp 94f4439 [origin/topic: ahead 2] feat: add custom post type
test ac5446a [origin/test] initial commit

This command also tells whether a local branch is ahead or behind its upstream branch.