Last Updated: January 3, 2026
When you work with remote repositories, you often need to coordinate your local branches with their corresponding upstream branches.
This chapter dives deep into upstream tracking, explaining what it means, how to set it up, and best practices for working with upstream branches.
At its core, upstream tracking is about linking your local branches to remote branches. When you track a branch upstream, it allows Git to know which remote branch to compare against during operations like git pull and git push.
This concept helps manage changes efficiently between your local work and the remote repository, ensuring you are always in sync with the team's progress.
When you create a local branch and specify an upstream branch, Git can automatically determine where to fetch updates from and where to push your changes. This simplifies your workflow and reduces the chances of errors that might come from specifying the wrong branch.
In Git, the tracking relationship is stored in the .git/config file under the section for each branch. You will often see entries such as:
This indicates that your local feature-branch is tracking the feature-branch on the origin remote.
You can set up upstream tracking in several ways, depending on your workflow. The most common methods include:
git push -uWhen you create a new branch and want to push it to a remote for the first time, you can use the -u flag with git push. This sets the upstream branch during the push.
In this example, you create a new branch new-feature and push it to the origin remote. By using the -u flag, you set origin/new-feature as the upstream branch for your local new-feature.
git branch --set-upstream-toIf you already have a local branch and want to set or change its upstream branch, you can use:
This command explicitly links your local feature-branch with the remote origin/feature-branch. After this, operations like git pull will automatically know where to pull from.
You can also set the upstream branch when you create a branch directly. This can be done by using the --track option:
This command creates a local branch named new-feature that tracks the remote origin/new-feature. It's a convenient one-liner for establishing tracking as you create a new branch.
Once you have set up upstream tracking, several commands become more intuitive since Git understands the relationship between your local and remote branches. Here are some of the most commonly used commands and how they interact with upstream tracking.
git pullWhen you run git pull, Git knows which upstream branch to pull changes from because of the tracking relationship. This command performs a fetch followed by a merge to bring in changes from the remote branch.
If your local branch is tracking a remote branch, simply running git pull will automatically fetch updates from the appropriate upstream branch. This saves time and avoids errors associated with manually specifying the branch.
git pushSimilarly, when you run git push, Git knows where to send your commits:
As long as your branch is set up to track a remote branch, this command will push your changes to the correct remote branch without needing to specify it.
To check which upstream branch your current branch is tracking, you can use:
This will display a message showing if your branch is ahead, behind, or diverged from the upstream branch. Alternatively, you can use:
This command lists all your branches, showing the upstream branch they are tracking and how many commits ahead or behind they are.
Upstream tracking also simplifies merging and rebasing. When you want to incorporate changes from the upstream branch into your local branch, you can run:
You can also rebase your branch against the upstream branch to maintain a cleaner commit history:
Both operations will automatically use the upstream branch you have set.
Understanding upstream tracking can prevent several common pitfalls that developers face when working with Git. Here are some best practices to keep in mind:
One common mistake involves checking out a remote branch directly without creating a local tracking branch. This results in a detached HEAD state, which can be confusing. Always create a local branch that tracks a remote one if you intend to make changes.
Regularly syncing your local branch with its upstream counterpart is crucial. Use git pull frequently to avoid running into conflicts later. Ignoring this can lead to larger merges, which are more challenging to resolve.
Setting up Git aliases can streamline your workflow. For example, you can create an alias for checking the status of upstream branches:
Now, running git upstream will quickly show you the tracking status of your branches.
Over time, you may find yourself with multiple branches that are no longer in use. Regularly clean up your branches, both locally and remotely, to keep your repository manageable.
Occasionally, you may encounter problems with your upstream tracking. Here are some common scenarios and how to address them:
If you realize that you set the wrong upstream branch, you can change it using:
This command will update the upstream tracking reference for your current branch.
If your local branch has diverged from its upstream branch, you might see messages indicating this when you run git status. To resolve this, you can either merge or rebase, depending on your workflow preference.
Decide which method fits your needs based on your project's collaboration style.
If you work with multiple remotes, you can specify which remote branch to track using the full reference:
This allows you to manage upstream tracking effectively across different remotes.
In the next chapter, we will look at how to manage and work with remote branches effectively, adapting your local workflow to accommodate the complexities of collaborating in a distributed environment.