Search code examples
gitgithubvisual-studio-codeteam-explorer

Visual Studio Team Explorer GitHub - when you have two remotes how do you specify the one used for outgoing commits?


I had the same issue as SF 5181845 in terms of wanting to use a public repo on GitHub as the starting point for my project whilst retaining the ability to merge into it any changes made in the public repo. This seems like a common scenario.

I started by using the Git command line utility to create an empty private repo on GitHub. I then cloned the public repo from github to a local repo on my machine and renamed its remote from 'origin' to 'upstream'. Next I added a second remote called 'origin' to the URL of my empty private repo before finally pushing my local repo to it. Everything seemed fine so I opened the solution using Visual Studio and set about making my changes which were duly committed to my local repo. Unfortunately, I hit problems when using Team Explorer to sync to my private repo on GitHub - i.e. pushing the outgoing commit. It failed as Team Explorer was attempting to push to the public repo (upstream) instead of my own private repo (origin).

Question: when you have two remotes how do you specify the one used by Team Explorer for outgoing commits?

I found a work-around by changing the Repository Settings in Team Explorer. I edited the upstream remote and set the push URL to my private repo on GitHub in place of the URL of the public one.

Additional: This is how my branches look in Team Explorer - see answer below

Team Explorer - Branches


Solution

  • The solution provided by SO 5181845 for providing two remotes so you can modify a project in your own private repo whilst still being able to pull updates from a public repo was not complete. It is missing the git command provided by SO 4878249 which changes your local branch so it tracks your private repo rather than the public one as kindly pointed-out by Chad B - see comments.

    git branch master --set-upstream-to origin/master
    

    Therefore the complete recipe is:

    1. Create a new repo on GitHub and get its URL https://github.com/myname/myproject.git
    2. Create a working directory on your machine, say src
    3. Clone the base project into this directory, say https://github.com/sharedwork/baseproject.git
    4. Set your master branch so it tracks myproject rather than baseproject
    mkdir src
    cd src 
    git clone https://github.com/sharedwork/baseproject.git
    cd baseproject
    git rename origin upstream
    git remote add origin https://github.com/myname/myproject.git
    git push origin master
    git branch master --set-upstream-to origin/master
    

    You can now open the solution in Visual Studio and use Team Explorer to push your changes to https://github.com/myname/myproject.git in the normal way. The outgoing commits will go myproject rather than baseproject.

    When https://github.com/sharedwork/baseproject.git is updated you can pull the changes into your project using the following git commands.

    cd src\baseproject
    git pull upstream master
    

    However you may have to merge changes in any files updated by baseproject which you have also changed.