How can you track the upstream branch of a new local branch?
Posted by BobHarris
Last Updated: June 26, 2024
To track the upstream branch of a new local branch in Git, you typically set the upstream branch when you create the new branch or you can configure it afterward. Here are the steps for both methods:
Method 1: Set Upstream When Creating a New Branch
When you create a new local branch and want to set its upstream branch in one command, you can use:
git checkout -b new-branch-name origin/existing-branch-name
This command does two things: it creates a new branch named new-branch-name and sets the upstream branch to origin/existing-branch-name.
Method 2: Setting Upstream After Creating a Branch
If you have already created a local branch and want to set its upstream branch afterward, you can use the following command:
git push -u origin new-branch-name
The -u (or --set-upstream) option will link your local branch new-branch-name to the specified remote branch on origin, so in the future you can use git push and git pull without having to specify the branch.
Method 3: Configuring Upstream Branch Manually
If you want to set the upstream branch manually (e.g., the remote branch might not exist yet), you can use:
git branch --set-upstream-to=origin/existing-branch-name new-branch-name
This command allows you to configure the upstream branch without pushing if the branch exists locally.
Check Upstream Branch
After you set the upstream branch, you can check which upstream branch your local branch is tracking with the following command:
git status
You can also use:
git branch -vv
This will list all your branches and show you the upstream branches they are tracking.
Summary
In summary, you can set the upstream branch while creating a new branch or afterward with the git push -u command, among others. Use git branch -vv to verify your branch tracking.