Existing Git branch track a remote branch

In this article we will learn how to make a new branch that tracks remote branches and how to make an existing branch track a remote branch?

To make an existing Git branch track a remote branch, you can use the command git branch --set-upstream-to=origin/<remote-branch> <local-branch>.

 

This command tells Git to set the upstream branch of your local branch to the remote branch you specified. The origin in the command refers to the name of the remote repository, and remote-branch is the name of the branch on that remote repository that you want to track. local-branch is the name of the branch on your local repository that you want to set the upstream for.

For example, if you want to make your local branch my-feature track the remote branch feature/new-design on the remote repository origin, you would run the command:

git branch --set-upstream-to=origin/feature/new-design my-feature

You can also use git branch -u as a short form for --set-upstream-to.

 

Example to make an existing branch track a remote branch.

git branch -u origin/feature/new-design my-feature

Once the upstream is set, you can now use git pull or git push commands without specifying the remote and branch name and git will know which branch to use.

 

To make a new branch that tracks remote branches

To create a new local branch that tracks a remote branch, you can use the git branch command with the -t (or --track) option. The syntax for this command is:

git branch --track [new_branch_name] [remote_branch_name]

For example, if you want to create a new local branch called "my-branch" that tracks the remote branch "origin/my-branch", you would run the following command:

git branch --track my-branch origin/my-branch

Alternatively you can use the following command

git checkout -b [new_branch_name] [remote_branch_name]

For example,

git checkout -b my-branch origin/my-branch

This will create a new local branch called "my-branch" and switch to it, and it will also set up tracking so that the local branch is linked to the remote branch.


Tags:

git

Share:

Related posts