How to change the remote a branch is tracking?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding Branch Tracking in Git
In Git, a branch can track a remote branch, which essentially means that the local branch is linked to the remote branch on a particular repository. This allows developers to easily sync changes between the local copy and the remote server. However, you might need to change the remote a branch is tracking, for instance, if you clone a fork of a repository and want to track the original repository directly or if a repository's remote URL changes.
This article explains how to change the remote tracking branch and provides in-depth technical details, supporting examples, and additional insights.
Technical Explanation
When you clone a Git repository, branches typically start with a tracking setup that links them to a remote branch. Git handles this through configuration settings inside the .git/config file under the [branch "branch_name"] section, with settings like remote and merge.
For example, a typical branch configuration might look like this:
This setup indicates that the local branch main is tracking the main branch of the origin remote.
Changing the Remote Tracking Branch
There are multiple ways to change the remote a branch is tracking:
- Using
git branchCommand: You can use thegit branchcommand with the--set-upstream-tooption to change the tracking configuration. This method is straightforward and involves one succinct command. - Modifying
.git/configFile: Directly editing the.git/configfile offers granular control but is more error-prone. It is typically used in complex scenarios where automation scripts are managing configurations.
Example: Using git branch --set-upstream-to
Suppose you want to change the tracking of a branch named feature-branch to track the develop branch on a remote named upstream. You would run the following command:
After executing this command, the local feature-branch will now track the upstream/develop branch. To verify the change, you can use:
The output will indicate that feature-branch is tracking the remote upstream/develop.
Modifying .git/config
In some cases, particularly in automated environments, you might edit the .git/config file. Here's how to do it:
- Open the
.git/configfile in a text editor. - Locate the section that corresponds to the branch you wish to modify.
- Update the
remoteandmergeattributes as necessary. For example:
Save the file after making the changes. Remember that manual edits come with a risk of configuration errors.
Additional Considerations
- Checking Remote URLs: It’s a good practice to verify the remote URLs using
git remote -vto ensure you are working with the correct repositories. - Removing Upstream Information: To remove upstream information if needed, you can use:
- Working with Forks: When working with forks, you may add multiple remotes and use the
git remotecommand to push and fetch changes selectively.
Summary Table
Below is a succinct summary of commands and their purposes:
| Command | Description |
git branch --set-upstream-to | Changes the remote tracking of a branch. |
git remote -v | Displays remote URLs for verification. |
git branch --unset-upstream | Removes the upstream information from a branch. |
git remote add <name> <url> | Adds a new remote repository URL. |
.git/config manual edits | Allows direct control over git configurations. |
In conclusion, changing the remote a branch is tracking is a vital skill for any developer working with Git, providing flexibility to manage changes across different repositories seamlessly.

