Tracking vs Non-Tracking Branch in Git
Introduction¶
This Gemstone delves into tracking and non-tracking branches in Git. It also includes steps to verify and convert between the branch types.
Tracking Branch¶
A tracking branch is a branch that is linked to a remote branch.
-
Create a new branch named my-local-branch. Make the new local branch track the main branch of the remote repository named origin. Type:
git checkout -b my-local-branch origin/main
-
Use the
git branch -vv
command to verify that the branch is a tracking branch. Type:git branch -vv
Look for branches with
[origin/main]
indicating they are trackingorigin/main
.
Non-Tracking Branch¶
A non-tracking branch is a branch that operates independently from remote branches.
-
Create a new non-tracking local branch named my-feature-branch. Type:
git checkout -b my-feature-branch
-
Non-tracking branches won’t show a remote branch next to them in the git branch -vv output. Check if my-feature-branch is a non-tracking branch.
Converting Non-Tracking to Tracking¶
-
Optionally, first make sure the latest changes from the main branch are merged into the target branch. Type:
git checkout my-feature-branch git merge main
-
Set up tracking to a remote branch:
git branch --set-upstream-to=origin/main my-feature-branch
Output:
Branch 'my-feature-branch' set up to track remote branch 'main' from 'origin'.
-
Verify the change. Type:
git branch -vv
Now,
my-feature-branch
should show[origin/main]
indicating it's tracking.
Conclusion¶
Understanding the nuances between tracking and non-tracking branches is vital in Git. This Gemstone clarifies these concepts and also demonstrates how to identify and convert between these branch types for optimal git workflow management.
Author: Wale Soyinka