0
0
Gitdevops~5 mins

Tracking branches concept in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work with Git, you often want your local branch to follow changes from a branch on a remote server. Tracking branches help you keep your local work connected to the remote branch, so you can easily see new updates and share your changes.
When you clone a project and want your local main branch to follow the remote main branch automatically.
When you create a new branch locally and want it to track a branch on the remote repository for easy pushing and pulling.
When you want to check which remote branch your local branch is connected to.
When you want to fetch updates from the remote branch and merge them into your local branch.
When you want to push your local changes to the remote branch without specifying the branch name every time.
Commands
This command lists all local branches with information about their tracking status and the latest commit. It helps you see which branches are tracking remote branches.
Terminal
git branch -vv
Expected OutputExpected
main 1a2b3c4 [origin/main] Initial commit * feature 5d6e7f8 [origin/feature: ahead 2] Added new feature
-vv - Show verbose information including tracking branch and commit details
This command sets the local branch 'feature' to track the remote branch 'origin/feature'. It connects your local work to the remote branch for easier syncing.
Terminal
git branch --set-upstream-to=origin/feature feature
Expected OutputExpected
No output (command runs silently)
--set-upstream-to - Specify which remote branch the local branch should track
This command fetches changes from the remote branch your current branch is tracking and merges them into your local branch. It keeps your work up to date.
Terminal
git pull
Expected OutputExpected
Updating 5d6e7f8..9a0b1c2 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
This command pushes your local commits to the remote branch your current branch is tracking. It shares your changes with others without needing to specify the remote branch name.
Terminal
git push
Expected OutputExpected
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 To github.com:user/repo.git 5d6e7f8..9a0b1c2 feature -> feature
Key Concept

If you remember nothing else from this pattern, remember: tracking branches link your local branch to a remote branch so you can easily sync changes without extra commands.

Common Mistakes
Not setting a tracking branch after creating a new local branch.
Without tracking, git pull and git push commands won't know which remote branch to sync with, causing errors or requiring manual branch names.
Use 'git branch --set-upstream-to=origin/branch-name local-branch' to link your local branch to the remote branch.
Assuming 'git pull' will always work without setting upstream.
If the local branch has no tracking branch, 'git pull' will fail with an error asking to specify the remote branch.
Set the upstream branch first or use 'git pull origin branch-name' explicitly.
Summary
Use 'git branch -vv' to see which local branches track remote branches and their status.
Set a tracking branch with 'git branch --set-upstream-to=origin/branch local-branch' to connect local and remote branches.
Once tracking is set, 'git pull' and 'git push' work smoothly without extra branch names.