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:
Step 2: View Remote Connections List
Next, check the existing remote connections list through the “git remote” command alone with the “-v” option:
Step 3: Add New Remote URL
Now, run the “git remote add” command to add a new remote URL in the local repository:
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:
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:
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:
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.