How to Stop Tracking a Remote Branch in Git

John Wachira Mar 11, 2025 Git Git Branch
  1. Understanding Remote Tracking Branches
  2. Method 1: Using Git Branch Command
  3. Method 2: Using Git Fetch Command
  4. Method 3: Manually Editing the Configuration
  5. Conclusion
  6. FAQ
How to Stop Tracking a Remote Branch in Git

Managing branches in Git can sometimes feel overwhelming, especially when you are dealing with remote branches. As your project evolves, you may find that certain remote branches are no longer relevant or necessary. Thankfully, Git provides a straightforward way to stop tracking these branches from your local repository.

In this article, we will explore the steps to remove a remote tracking branch in Git, ensuring your local setup remains clean and organized. Whether you’re a beginner or an experienced developer, this guide will equip you with the knowledge to manage your branches effectively.

Understanding Remote Tracking Branches

Before diving into the methods of stopping tracking a remote branch, it’s essential to understand what remote tracking branches are. When you clone a repository, Git creates a local reference to the branches in the remote repository. These references are known as remote tracking branches. They help you keep track of the state of the remote branches and facilitate collaboration with other developers.

However, as your project progresses, some branches may become obsolete or merged into others. In such cases, you might want to stop tracking these remote branches to avoid cluttering your local environment. Let’s look at how to accomplish this.

Method 1: Using Git Branch Command

One of the simplest ways to stop tracking a remote branch is by using the git branch command with the -d option. This method allows you to delete the local reference to the remote branch without affecting the branch on the remote repository. Here’s how to do it:

git branch -d -r origin/branch_name

Output:

Deleted remote-tracking branch origin/branch_name (was commit_hash).

In this command, replace branch_name with the name of the remote branch you want to stop tracking. The -d option stands for delete, while -r indicates that you are working with a remote branch. After executing this command, Git will remove the local reference to the specified remote branch. However, do note that this action does not delete the branch from the remote repository; it merely removes your local reference to it.

This method is particularly useful when you want to clean up your local repository without affecting your collaborators. It keeps your branch list tidy and helps you focus on the branches that are still active and relevant to your workflow.

Method 2: Using Git Fetch Command

Another effective way to stop tracking a remote branch is by using the git fetch command combined with the --prune option. This method not only removes the tracking reference but also cleans up any stale references to remote branches that no longer exist. Here’s how you can do it:

git fetch --prune

Output:

From https://github.com/user/repo
 - [deleted]         (none)     -> origin/branch_name

When you run this command, Git will fetch the latest changes from the remote repository and remove any local references to branches that have been deleted on the remote. This is particularly useful when branches have been merged or deleted by other team members.

The --prune option ensures that your local repository is in sync with the remote repository, removing any unnecessary clutter. It’s a great way to maintain a clean environment, especially in collaborative projects where branches are created and deleted frequently.

Method 3: Manually Editing the Configuration

If you prefer a more hands-on approach, you can manually edit your Git configuration to stop tracking a remote branch. This method involves modifying the .git/config file in your local repository. Here’s how to do it:

  1. Open the .git/config file in your preferred text editor.
  2. Locate the section labeled [remote "origin"].
  3. Find the branch you want to stop tracking and remove its entry.

For example, your configuration might look like this before editing:

[remote "origin"]
    url = https://github.com/user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

After removing the specific branch entry, your configuration will be cleaner:

[remote "origin"]
    url = https://github.com/user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

By manually editing the configuration, you can customize your tracking settings according to your preferences. While this method requires a bit more effort, it gives you complete control over your remote tracking branches.

Conclusion

Stopping the tracking of a remote branch in Git is a crucial skill that helps maintain an organized local repository. Whether you choose to use the git branch command, the git fetch --prune command, or manually edit your configuration file, each method offers a straightforward approach to decluttering your branch list. By mastering these techniques, you can ensure that your Git environment remains efficient and manageable, allowing you to focus on the branches that truly matter to your project.

FAQ

  1. How do I know which remote branches I am tracking?
    You can list all remote branches by using the command git branch -r.

  2. Will stopping tracking a remote branch delete it from the remote repository?
    No, stopping tracking a remote branch only removes the local reference; it does not affect the remote branch.

  1. Can I recover a deleted remote branch in Git?
    If a remote branch is deleted, you can recover it if you have a local copy of the branch or if it exists in another clone of the repository.

  2. What does the --prune option do in Git?
    The --prune option removes local references to remote branches that no longer exist on the remote repository.

  3. Is there a risk in manually editing the .git/config file?
    Yes, manually editing the configuration file can lead to errors if not done carefully. Always make a backup before making changes.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Branch