How-To Fork Git Repositories on Visual Studio Online
Visual Studio Online
Microsoft rebranded Team Foundation Service as Visual Studio Online with the launch of Visual Studio 2013 last fall. Visual Studio Online enables team collaboration using either Git or TFS source code control directly inside Visual Studio 2013, web browser, or the command line. Microsoft is entering a space dominated by GitHub & Bitbucket, and developers will seek familiar workflows and features offered by by these platforms.
GitHub Fork
GitHub established the paradigm of forking, upstream repositories, and pull requests for a lot of developers. VonC put together a nice graphic to illustrate the relationship of repositories in his response to a Stack Overflow question on the topic.
GitHub simplifies the process of forking an upstream repository with the simple click of the Fork button:
Visual Studio Online Fork
Visual Studio Online might not currently have a Fork button but the process of forking Git repositories between accounts is quite straightforward and involves:
- Creating the target project to contain the fork repository
- Cloning the upstream repository
- Adding a remote origin link to the fork
- Pushing the cloned repository to the fork origin
In this scenario, I will fork a Git repository name VisualStudioOnlineFork from an upstream master repository located in woodcp.visualstudio.com to a fork project located in toddwood.visualstudio.com using the owner account of the fork repository. The fork project owner account had access granted to the upstream repository by the upstream owner account.
Create Fork Repository
Login to the fork account and create a new team project named the same as the upstream repository. Technically the names do not need to match but that is the convention established by the GitHub Fork button.
Once the project is created, Visual Studio Online will display the empty repository and instructions on how to set the remote and push the upstream or any existing repository:
Clone Upstream Repository
With the target and empty fork repository created, we can clone the upstream repository to our local computer. Access the upstream repository in Visual Studio Online if you need to identify the clone Url:
A nice feature of the git clone command is to set the origin name to upstream when cloning the master repository using the --origin=upstream directive:
C:\> mkdir fork Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 1/3/2014 8:58 PM fork C:\> cd fork C:\fork> git clone --origin=upstream https://woodcp.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork Cloning into 'VisualStudioOnlineFork'... remote: remote: fTfs remote: fSSSSSSSs remote: fSSSSSSSSSS remote: TSSf fSSSSSSSSSSSS remote: SSSSSF fSSSSSSST SSSSS remote: SSfSSSSSsfSSSSSSSt SSSSS remote: SS tSSSSSSSSSs SSSSS remote: SS fSSSSSSST SSSSS remote: SS fSSSSSFSSSSSSf SSSSS remote: SSSSSST FSSSSSSFt SSSSS remote: SSSSt FSSSSSSSSSSSS remote: FSSSSSSSSSS remote: FSSSSSSs remote: FSFs (TM) remote: remote: Microsoft (R) Visual Studio (R) Team Foundation Server remote: Unpacking objects: 100% (3/3), done. Checking connectivity... done
Configure Remote Origin
At this point, we have cloned the repository with only a remote named upstream pointing to the master repository. Referencing the bare repository instructions in the fork repository, we add the remote origin pointing to the fork repository location:
C:\fork> cd .\VisualStudioOnlineFork C:\fork\VisualStudioOnlineFork [master]> git remote add origin https://toddwood.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork
Push to Fork
Once again, using the bare repository instructions we perform a git push -u origin --all to complete the fork process:
C:\fork\VisualStudioOnlineFork [master]> git push -u origin --all Counting objects: 3, done. Writing objects: 100% (3/3), 250 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Analyzing objects (3/3) (2 ms) remote: Storing pack file and index... done (800 ms) To https://toddwood.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork * [new branch] master -> master Branch master set up to track remote branch master from origin.
The fork of the upstream repository is now complete and available in Visual Studio Online:
Visual Studio Online Merge Upstream Changes
As time progresses the upstream repository will diverge from the forked repository. The fork maintainer can easily merge the upstream changes by fetching from upstream, merging master, and pushing to origin. This process is no different than when dealing with GitHub hosted repositories.
Upstream Changes
C:\WCP\VisualStudioOnlineFork [master]> echo Downstream Merge >> .\README.md C:\WCP\VisualStudioOnlineFork [master +0 ~1 -0]> echo MIT >LICENSE.md C:\WCP\VisualStudioOnlineFork [master +1 ~1 -0 !]> git add -A C:\WCP\VisualStudioOnlineFork [master +1 ~1 -0]> git commit -m "Added LICENSE and updated README" [master 7f7d2ab] Added LICENSE and updated README 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 LICENSE.md C:\WCP\VisualStudioOnlineFork [master]> git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 368 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) remote: Analyzing objects (4/4) (2 ms) remote: Storing pack file and index... done (374 ms) To https://woodcp.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork d2c3b1a..7f7d2ab master -> master C:\WCP\VisualStudioOnlineFork [master]>
Downstream Merge
C:\WCP\VisualStudioOnlineFork [master]> cd C:\fork\VisualStudioOnlineFork C:\fork\VisualStudioOnlineFork [master]> git fetch upstream remote: Microsoft (R) Visual Studio (R) Team Foundation Server Unpacking objects: 100% (4/4), done. From https://woodcp.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork d2c3b1a..7f7d2ab master -> upstream/master C:\fork\VisualStudioOnlineFork [master]> git merge master upstream/master Updating d2c3b1a..7f7d2ab Fast-forward LICENSE.md | Bin 0 -> 12 bytes README.md | Bin 26 -> 64 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 LICENSE.md C:\fork\VisualStudioOnlineFork [master]> git push Counting objects: 6, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 368 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) remote: Analyzing objects (4/4) (2 ms) remote: Storing pack file and index... done (496 ms) To https://toddwood.visualstudio.com/DefaultCollection/_git/VisualStudioOnlineFork d2c3b1a..7f7d2ab master -> master C:\fork\VisualStudioOnlineFork [master]>
Conclusion
I am a big fan of GitHub but when your clients decide to use Visual Studio Online that is exactly what you do. However, this does not mean you need to abandon certain Git repository work flows and setups that are proven to work.
I have been using Visual Studio Online for the past three months without any complaints. The integration with Visual Studio 2013 is very nice and the agile management online is well done. The lack of a Fork button on Visual Studio Online is a minor inconvenience considering the process of forking repositories is still possible as I demonstrated.