AlgoMaster Logo

Upstream Tracking

Last Updated: January 3, 2026

6 min read

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.

What is Upstream Tracking?

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.

Setting Up Upstream Tracking

You can set up upstream tracking in several ways, depending on your workflow. The most common methods include:

Using git push -u

When 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.

Using git branch --set-upstream-to

If 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.

During Branch Creation

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.

Working with Upstream Branches

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 pull

When 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 push

Similarly, 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.

Checking Upstream Status

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.

Merging and Rebasing

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.

Common Pitfalls and Best Practices

Understanding upstream tracking can prevent several common pitfalls that developers face when working with Git. Here are some best practices to keep in mind:

Avoiding Detached HEAD State

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.

Keeping Your Branches Updated

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.

Streamlining with Aliases

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.

Clean Up Unused 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.

Troubleshooting Upstream Issues

Occasionally, you may encounter problems with your upstream tracking. Here are some common scenarios and how to address them:

Incorrect Upstream Branch

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.

Diverged Branches

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.

Handling Multiple Remotes

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.