How to Stop Tracking a Remote Branch in Git

John Wachira Feb 15, 2024
How to Stop Tracking a Remote Branch in Git

This article illustrates how we can stop tracking a remote branch in Git. By now, you must be all too familiar with the concept of tracking remote branches.

This session will cover the various methods you can use to stop tracking a remote branch. Let’s jump right in.

Stop Tracking a Remote Branch in Git

Here is a list of all remote-tracking branches in our repository.

git branch

If you recall, newer versions of Git will display the remote tracking branch in blue. Hence we can see that Dev2.1 in our local repo is tracking Dev2.1 in our remote origin.

We can use the steps below to stop tracking the remote branch.

First, we will checkout the Dev2.1 branch, as shown below.

$ git checkout Dev2.1

We can now run the command below to stop tracking the remote branch.

$ git branch --unset-upstream

Once we execute the command, our Dev2.1 will stop tracking the remote. Let’s check if this is the case.

git branch –unset-upstream

We can see that our Dev2.1 branch does not have a remote-tracking branch.

Note that when you stop tracking a remote branch, you can no longer push the changes you make in your local repository to the remote branch.

Other Options

Alternatively, you can delete the remote tracking branch in your local repository. This does not apply to everyone.

It is a quick solution for those who do not need the branch anymore.

$ git branch -d -r origin/<remote branch name>

Substitute remote branch name with the name of the tracking branch. The command will not delete the branch in the remote repository.

It will only do away with the branch set to track the remote.

Another way of dealing with this is making edits to your configuration file to remove the link between the local and remote branches.

Run the command below.

$ git config --unset branch.<local-branch-name>.remote
$ git config --unset branch.<local-branch-name>.merge

This will edit the configuration to remove the association between the remote and local branches.

In a nutshell, we have covered three methods you can use to stop tracking a remote branch. The git branch --unset-upstream command is the cleanest and simplest.

You can also remove the link between the two branches by editing the Git configuration file.

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