Git

How to Change the Remote Tracking Branch in Git

On Git, to build a connection between the local machine and remote hosting service, a cloning operation is performed. Then, developers are required to set the remote URL for tracking the remote repository changes in the local repository and set the local branch as tracking a remote branch. To perform this operation, the “$ git branch <branch-name> -u <remote-name>/<branch-name>” command can be used as it permits you to change the remote tracking branch for several purposes.

This guide will provide the method to change the remote tracking branch.

How to Change the Remote Tracking Branch in Git?

To change the existing remote-tracking branch, first, you need to navigate to the particular local repository and check the list of all current remote URLs. Next, add a new remote URL to the existing list and verify the newly added remote URL. After that, download the updated remote repository with the newly added remote. Lastly, run the “$ git branch <branch-name> -u <remote-name>/<branch-name>” command to change the remote tracking branch.

Now, implement the above-discussed procedure and view the results!

Step 1: Move to Local Repository
Use the “cd” command to navigate to the desired Git local repository:

$ cd "C:\Users\nazma\Git\demo5"

Step 2: View Remote Connections List
Next, check the existing remote connections list through the “git remote” command alone with the “-v” option:

$ git remote -v

Step 3: Add New Remote URL
Now, run the “git remote add” command to add a new remote URL in the local repository:

$ git remote add alpha https://github.com/GitUser0422/demo5.git

Step 4: Ensure Added Remote URL
To ensure that if the newly added remote URL is present in the URL list, execute the provided command:

$ git remote -v

Step 5: Fetch Updated Remote Branch
Then, execute the “git fetch” command to download or fetch the updated remote repository with the newly added remote:

$ git fetch alpha

Step 6: Change Remote Tracking Branch
Finally, change the remote tracking branch by executing the “git branch” command along with the remote and local branch name and “-u” option:

$ git branch master -u alpha/master

As you can see in the below-provided output, the newly added remote and particular local branch is set as a new remote-tracking branch:

That’s it! We have provided a method to change the remote tracking branch.

Conclusion

To change the remote-tracking branch, first, move to the desired local repository and view the list of existing remote URLs. Then, add a new remote URL and ensure the newly added remote URL. Next, fetch the updated remote repository along with the newly added remote, and lastly, use the “$ git branch <branch-name> -u <remote-name>/<branch-name>” command to change the remote tracking branch. This guide demonstrated the procedure for changing the remote tracking branch.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.