Unshelving TFS changes into another branch (VS 2017)
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.
Wrap up
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.
I have VS2017 with TFS shell Extensión, when I try run the migrate command, shows me the next message: “‘tfpt’ is not recognized as an internal or external command,
operable program or batch file.”
Help me, please, thanks.
Usually this is because TFPT.exe is not in the systems “PATH”.
If you find the location of your TFPT.exe (Usually somewhere like C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\) you can add it to the PATH of your system, or call the full path in the command line , i.e.
`C:\Program Files (x86)\Microsoft Team Foundation Server 2015 Power Tools\tfpt.exe somecommand`
Hello,
Thank you for the excellent post. The TFS power tools is sadly not available for VS 2017 (and 2019). In order to have a working solution we have to try with an older version of TFS (like 2015) and a compatible version of VS (eg. VS 2015).
Not sure why Microsoft has removed that.
Source: https://docs.microsoft.com/en-us/visualstudio/releasenotes/tfs2017-relnotes . Under section: “Known Issues”
I further tried the latest versions of TFS Template editors from the VS Marketplace, (URL: https://marketplace.visualstudio.com/items?itemName=KarthikBalasubramanianMSFT.TFSProcessTemplateEditor), however, was not able make this solution working. Couldn’t get the command “tfpt” working. I tried with both VS 2017 and 2019 versions (installed with same versions of TFS). I did not have any older versions installed.
I could be wrong here, as I haven’t needed to do this for a while, but I think you still use the old version of TFPT but you do have to have an older version of VS or the team explorer available too.
I did however see the extension MultiMerge which may be of use to you. https://marketplace.visualstudio.com/items?itemName=Jesusfan.MultiMerge2017 This apparently does branch migration!
Hi, I also tried to run this on a computer that only has VS Code and VS2019 Professional installed. With no success. Further search on the internet showed that the ‘tfpt.exe’ you are referring to in this method is only provided through the separate software ‘Microsoft Team Foundation Server 2013 Power Tools’ which is deprecated.
After spending over an hour searching for alternatives I am yet to find another commandline tool that provides the same simple functionality for TFVC.
As you already wrote.. it would be so much easier in git….
Yes, apologies, this guide was ported from an older one. I actually tested this recently by using chocolatey to download the free “Team Explorer” edition of 2013 (or 2015 depending) and then downloaded the matching TFPT. Still seemed to work but it’s a bit of a mess around. There are some third party extensions I have seen for doing similar things but they get mixed reviews and I haven’t personally played with them.
Thank you for the information, but the title is misleading. This doesn’t work when you have just VS 2017 installed and instructed.
Yes, apologies, this guide was ported from an older one. I actually tested this recently by using chocolatey to download the free “Team Explorer” edition of 2013 (or 2015 depending) and then downloaded the matching TFPT. Still seemed to work but it’s a bit of a mess around. There are some third party extensions I have seen for doing similar things but they get mixed reviews and I haven’t personally played with them. I have updated this page to reflect.