Team Foundation Server switch between branches

2019-01-23 12:37发布

问题:

Can we switch between branches in TFS

what i want is i downloaded a working copy and now I want to switch to different branch without downloading everything, because for large projects it will take lot of time since developers spend lot of time downloading

Is it possible, if not any workaround ??

回答1:

You can switch branches from the command-line client (only downloading the differences) by changing your workspace mappings and using the /remap flag to the get command:

tf workfold /map $/Branch1 C:\Work
tf get C:\Work /version:T /recursive
tf workfold /unmap $/Branch1
tf workfold /map $/Branch2 C:\Work
tf get C:\Work /remap /version:T /recursive


回答2:

In TFS branches are "physically" present in the Source Control, they're like "special folders". So you can totally choose what branch you get locally by targeting the right folder for your get.

If you have for instance:

  • Projects [folder]
    • ProjectA [folder]
      • Dev [Branch]
      • V1 [Branch]
    • ProjectB [folder]
      • Dev [Branch]
      • V1 [Branch]

and you want to get at the "Projects" level with only the content of "Dev", you can create mapping in your Workspace definition to cloack the V1 branches of ProjectA and B.



回答3:

Just for supplementing the knowledge base - my colleague Isak Savo created useful batch for such purpose. You need to do some editing inside the script (at the top) to point to the correct source code location and appropriate branches. The core is basically the same as in Edward Thomson answer, but with some interactive logic added. I made some minor changes (directory context switching for tf commands, quotes for arguments - needed if there are spaces in directories) and shared it below:

@echo off
rem Command to switch the current source tree to a new branch.
rem It's best to not have any pending changes. 
set DEVBRANCH=$/dir/src1
set RELEASEBRANCH=$/dir/src2
set SOURCEDIR=c:\sources directory\src

if exist "%SOURCEDIR%" goto ASK

echo Source code directory (%SOURCEDIR%) not found, please edit this script to point to the correct directory
pause
exit

:ASK:
set TARGET=
echo Available branches are:
echo   Dev: %DEVBRANCH%
echo   Release: %RELEASEBRANCH%
set /P ANSWER=Specify target branch? [Dev, Release]  
cls
if /I "%ANSWER%"=="Release" set TARGET=%RELEASEBRANCH%
if /I "%ANSWER%"=="Dev" set TARGET=%DEVBRANCH%
if /I "%ANSWER%"=="quit" goto END
if [%TARGET%] NEQ [] goto SWITCH

echo "%ANSWER%" unknown, please answer Dev or Release. Specify quit to cancel
GOTO ASK

:SWITCH
rem Navigate to the mapping source folder to avoid "Unable to determine the workspace..." error while invoking tf commands.
echo Changing directory context
pushd %SOURCEDIR%

echo Switching to branch %TARGET%
echo  - Creating new mapping...
tf workfold /map "%TARGET%" "%SOURCEDIR%"
echo  - Get latest version...
tf get "%SOURCEDIR%" /remap /version:T /recursive

popd
goto END

:END

Save it e.g. to switch_branch.cmd and execute from any directory from your machine.



回答4:

Team Explorer Everywhere has a "Switch to branch" command, which is probably what you're looking for.

Visual Studio, on the other hand, doesn't have the same command...



回答5:

You can switch between multiple branches, as long as you are using same workspace and the working directory contains the branches.



标签: tfs branch