1

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

wpqs
  • 657
  • 1
  • 7
  • 18

2 Answers2

0

If the current branch is tracking a remote tracking branch from a particular remote (ex: upstream), the push will default to pushing to that remote. If the current branch is not tracking any branch and there is a single remote, Team Explorer will push to that remote. If there are multiple remotes, Team Explorer will default to the 'origin' remote (if found).

Chad B
  • 1,416
  • 9
  • 12
  • There is only one branch in my repo - master. Team Explorer shows it tracking the 'upstream' remote so that explains why pushing my outgoing commit fails - VS is attempting to commit to the public repo (without authority) rather than my private one (with authority). To solve my problem can you tell me how to change my current branch (master) so it is tracking master in the 'origin' remote rather than in the 'upstream' remote? Thanks for your help by the way. – wpqs Sep 09 '20 at 08:23
  • SO 696 proposes the following command for change my current branch (master) so it is tracking master in the 'origin' remote rather than in the 'upstream' remote >>> git branch master --set-upstream-to origin/master <<< I'll credit Chad B with the answer if he agrees and can update his answer with this information + link to SO 696 – wpqs Sep 09 '20 at 09:02
  • That's correct. You'll need to update the remote tracking branch that is being tracked by your local master branch. Using git branch --set-upstream-to is the correct approach. Team Explorer will let you "Unset Upstream Branch", as you see in the screenshot of the context menu, but it does not currently provide a way to set the new tracked branch. – Chad B Sep 09 '20 at 15:32
  • Thanks. I am going to handle this as 'answer your own question' so I can close this issue, but will give you due acknowledgement. I hope you're ok with that. – wpqs Sep 10 '20 at 16:36
0

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.

wpqs
  • 657
  • 1
  • 7
  • 18