Find Out Which Remote Branch a Local Branch Is Tracking
To determine which remote branch a local branch is tracking in Git, you can use a few different commands. Here’s a step-by-step guide to help you find this information:
1. Using git branch
The git branch
command with the -vv
option provides detailed information about local branches, including which remote branch they are tracking.
git branch -vv
vv
: Shows verbose information including the remote-tracking branch and the last commit on each branch.
Example Output:
* main 7f9c2d1 [origin/main] Update README
feature-branch 8a9b1c2 [origin/feature-branch] Add new feature
In this output:
main
is trackingorigin/main
.feature-branch
is trackingorigin/feature-branch
.
2. Using git status
You can also use the git status
command when you are on a specific branch. It shows which remote branch the current branch is tracking.
git status
Example Output:
On branch feature-branch
Your branch is up to date with 'origin/feature-branch'.
This tells you that the feature-branch
is tracking origin/feature-branch
.
3. Using git rev-parse
For a more direct approach, use the git rev-parse
command with --abbrev-ref
and --symbolic-full-name
options.
git rev-parse --abbrev-ref <local-branch>@{upstream}
<local-branch>
: Replace this with the name of your local branch.
Example:
git rev-parse --abbrev-ref main@{upstream}
This will output the name of the remote branch that main
is tracking, such as origin/main
.
4. Using git for-each-ref
You can use git for-each-ref
to get detailed information about all branches and their tracking status.
git for-each-ref --format '%(refname:short) %(upstream:short)' refs/heads/
Example Output:
main origin/main
feature-branch origin/feature-branch
This lists all local branches and their corresponding remote-tracking branches.
Summary
List Local Branches with Remote Tracking Information:
git branch -vv
Check Tracking Branch for the Current Branch:
git status
Find Remote Branch Tracking a Specific Local Branch:
git rev-parse --abbrev-ref <local-branch>@{upstream}
List All Branches with Their Tracking Information:
git for-each-ref --format '%(refname:short) %(upstream:short)' refs/heads/
These commands help you determine the remote branch each local branch is tracking, which is useful for understanding and managing your branch relationships in Git.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github