EDIT: This was updated from an original article on 2013/15. There is an additional pre-requisite below (See red) for doing this with 2017/19.
I had some pending changes recently on the wrong branch within TFS in Visual Studio 2017. Rather than clone all my changes in the other branch, I wanted to “migrate” my changes. In GIT this is fairly trivial, in TFS however…
To move changes between 2 branches, you have to ensure:
- The changes you want to migrate are shelved on the source branch.
- There are no pending changes in the workspace – This was rather annoying but a limitation of the tooling.
- You do a “get-latest” on both branches.
- You have access to Visual Studio Command Prompt.
- TFS Power tools . (Edit: See below). Either 2013 (https://marketplace.visualstudio.com/items?itemName=TFSPowerToolsTeam.MicrosoftVisualStudioTeamFoundationServer2013Power) or 2015 (https://marketplace.visualstudio.com/items?itemName=TFSPowerToolsTeam.MicrosoftVisualStudioTeamFoundationServer2015Power)
- The source and target branch are in the same workspace. This took me longer than I want to admit to work out as the error is not helpful!
With the above prerequisites met, you need to spin up the VS Command Prompt. This can be found via a start menu search but you can also add it to VS (If not already), following the steps below in VS.
Update for VS2015/17/19
This article was originally written for VS2013/15 and ported over. I failed to mention that the TFPT required VS2013 or 15 on the box. You can still follow the instructions below but will need to install the VS that matches the power-tools being used. You can use the free Team Explorer or Express version to do this and don’t require a full licence copy to use TFPT against it.
The easiest way to acquire this (2013 Team Explorer for example) is via Chocolatey, at https://chocolatey.org/packages/VisualStudio2013TeamExplorer or via the Microsoft downloads page for older versions. https://visualstudio.microsoft.com/vs/older-downloads/
Either way, you will need a VS to match the version of Power Tools – i.e. TFPT 2013 needs VS 2013 and TFPT 2015 needs VS 2015.
Adding Visual Studio Command Prompt to Visual Studio
Go to “Tools” > “External Tools”, and select “Add”.
Give it an appropriate title – I chose “VS Command Prompt.”. From here we want to specify the following:
- Command: C:\Windows\System32\cmd.exe
- Arguments: /k “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat”
- Initial Directory: $(SolutionDir)
This means (On saving) that if you go to “Tools” you will now see an option “VS Command Prompt”.
Back to the migration…
With the console open, and using a working directory of a folder under source control (I use the source solution directory), run the command:
tfpt unshelve /migrate /source:"$/Core/MyProduct" /target:"$Core/MyProduct-Branch" "MyShelveset"
In this command, we are saying to use the TFS Power Tool to unshelve a shelveset named “MyShelveset”. The migrate flag indicates that it will be moving between areas, and the source and target are named TFS folders.
If you get an error “An item with the same key has already been added“, ensure you do not have any pending changes in the source or target.
If you get an error “unable to determine the workspace“, make sure you are running the tool within a directory under the source folder.
Providing this command runs successfully, you will then see the “Shelveset Details” panel.
In this panel you should see the files that make up the shelveset you defined in the command. Pressing “Unshelve” will start the process.
In my case I also saw a “Unshelve/Merge Shelveset” window. You should be able to “auto-merge all”.
Oddly, Auto-Merge took quite a while on my machine (You can see the progress in the cmd window). I am unsure if this is normal, or because I was remote working that day over a VPN.
“Item could not be found in your workspace, or you do not have permission to access it. “
If you get this during the merge, you may do what I did and go down a rabbit hole of getting latest, checking mappings etc. Turns out that this command does not work cross-workspace. When I branch, I map the branch to a completely new workspace as it’s cleaner.
The workaround for this (If like me, you use a new workspace per branch) is to temporarily map the branch into the same workspace.
So overall, it is possible to do but a process that would take a GIT Novice like me minutes to do in GIT took closer to an hour total! Luckily this is still less effort than a manual merge, but if you only had a couple of files I would recommend just doing it manually…
Bonus round: Unshelving another users shelveset into another branch
If the shelveset is a colleagues and not yours, you can simply append “;username” at the end of the command above (Where username is their TFS user), and it will search for that shelveset under that user.