Do feel free to provide any comments/feedback to @TheRichCarey on Twitter
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.
- If you are using lower than VS2017, you will also need the TFS Power tools.
- 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.
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.