Partager via


Use Team Foundation version control commands

TFS 2017 | TFS 2015 | TFS 2013

Visual Studio 2019 | Visual Studio 2017 | Visual Studio 2015 | Visual Studio 2013

You can use version control commands to do nearly all tasks you can do in Visual Studio, and also several tasks that can't be done in Visual Studio. You can use the tf.exe tool to run version control commands from a command prompt or within a script.

Run a command

To launch the Visual Studio command prompt, from Windows Start, choose the Developer Command Prompt for VS2019 or earlier version shortcut.

Note

For Visual Studio 2017 and later versions, the tf.exe binary is no longer in a fixed location in the Visual Studio install path as in previous releases. (for example, C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE). Scripts using tf.exe should not hard-code a path to the file based on the Visual Studio install path.

In most cases, you run the version control command in the context of a directory that is mapped in the workspace. For example, $/SiteApp/Main/ is mapped to c:\\code\\SiteApp\\Main\\. To get the latest version of all items in the workspace:

c:\code\SiteApp\Main\SolutionA>tf get

Set up your dev machine and manage workspaces

Your workspace is a local copy of your team's codebase. Because it is a local copy on your dev machine, you can develop and test your code in isolation until you are ready to check in your work. Here are some commands to manage your workspace:

See also: Set up Team Foundation Version Control on your dev machine, Create and work with workspaces

Develop your app

Use these commands to develop your app under version control with your team:

See also: Develop your app in Team Foundation version control

Suspend your work

For a variety of reasons, sometimes you need to set aside some or all of your work in progress. To suspend and resume your work, and to manage your shelvesets, use these commands:

See also: Suspend your work and manage your shelvesets.

Contribute your work

Use the checkin command to check in your code to the team's code base:

  • Checkin command: Checks in pending changes to files or folders to the server.

See also: Check in your work to the team's codebase

Manage files and solve problems

View and Manage Version Control Files and Folders

See also: Use Source Control Explorer to manage files under version control

View and Manage Past Versions

See also: View and manage past versions

Compare Folders and Files

See also: View and manage past versions

Resolve File Conflicts

See also: Resolve Team Foundation Version Control conflicts.

Work with Version Control Locks

See also: Work with version control locks.

Isolate risk

Use the following commands to isolate risk using branches:

See also: Use branches to isolate risk in Team Foundation Version Control.

Administer Version Control

Use the following commands to manage your version control system:

See also: Configure check-out settings.

Get Help on Version Control Commands

Use the following commands to get more information about version control commands:

Understand command syntax

The syntax of each command appears at the top of each reference topic.

Required and optional arguments

Non-bracketed arguments are required. [Brackets] indicate optional arguments that are not required to complete a command. However, some optional arguments have defaults that are applied to the command even if you do not specify the option.

Exclusive arguments

When options are separated by a pipe (|), you can specify one of the options.

Verbatim and replaceable arguments

Bold items are options that you include verbatim. Italicized items are arguments that you must replace with actual characters to perform a command.

Command Shortcuts and Aliases

Some commands support shortcuts. For example, you can call the Delete command with either tf delete or tf del.

Example

For example, the Checkout command:

tf checkout [/lock:( none|checkin|checkout)] [/recursive] itemspec [/login: username,[ password]]

Let's review the arguments from this example:

  • itemspec: You must replace this argument with an itemspec that specifies the items you are checking out.

  • /lock:(none|checkin|checkout): You are not required to specify the /lock option. If you do not specify it, then the system by default specifies /lock:none. Otherwise, you can specify one of the lock options.

  • The following arguments are optional and if you do not supply them, none of their effects apply to the command:

    • /recursive: If you want to recursively check out multiple items in a folder, you must specify this option verbatim.

    • /login:username,password: If you want to run the command as another user, you must specify the /login option verbatim, replace username with the name of the user, and if necessary, you can supply the password.

Specify the items affected by a command

You can use itemspecs and versionspecs to specify which items are affected by a command.

Use an itemspec argument to specify affected items

You use an itemspec (item specification) to specify the items affected by a command. You can specify items either on a client machine or on your Team Foundation Server. You can use wildcard characters such as \* and ?.

Client itemspec arguments

A client itemspec argument specifies a path to items on a client machine such as a folder (for example, c:\code\SiteApp\Main\SolutionA\) a file (for example, c:\code\SiteApp\Main\SolutionA\Project1\program.cs or multiple files (for example, c:\code\SiteApp\Main\SolutionA\*.cs. You can also specify UNC paths such as \\myshare\code\SiteApp\Main.

Server itemspec arguments

A server itemspec argument specifies a path to items on your Team Foundation Server such as a folder (for example, $/SiteApp/Main/SolutionA) a file (for example, $/SiteApp/Main/SolutionA/Project1/program.cs or multiple files (for example, $/SiteApp/Main/SolutionA/*.cs.

You typically use server itemspec arguments when you need run a command on items not on the client machine. For example, you are working on a dev machine and need to get some revision history data about some items that are in a project collection you don't work in:

c:\>tf history /collection:http://fabrikam-3:8080/tfs/DefaultCollection
$/SiteApp/Main/SolutionA/Project1/* /recursive  
/noprompt 

Multiple itemspec arguments

For some commands, you can specify multiple itemspec arguments. For example:

c:\code\SiteApp\Main\SolutionA\Project1\>tf checkout program1.cs program2.c

Checks out program.cs and program2.c.

Use a versionspec argument to specify affected versions of items

You use a versionspec (version specification) to specify the version of items affected by a command. To provide a versionspec you can:

  • Use the /version option. For example: /version:C44.

  • Append the versionspec to an itemspec with a semicolon. For example: program1.cs;C44.

When you use the History command or the Difference Command, you can specify a range of versions by separating the versions with a tilde (~). For example:

c:\code\SiteApp\Main\SolutionA>tf history /noprompt * /recursive /v:D4/12/2012~D4/24/2012

Use the following syntax to specify a versionspec.

Type

Syntax

Description


Changeset

[C]n

Specifies items based on a changeset number. If an item that is in scope was not modified in the specified changeset, the system takes the latest version of the item that occurred before the specified changeset.

Tip

You can omit the C if you specify only a number. Examples

c:\code\SiteApp\Main>tf get readme.txt /v:C8

-- or --

c:\code\SiteApp\Main>tf get readme.txt /v:8

-- or --

c:\code\SiteApp\Main>tf get readme.txt;8

If readme.txt was modified in changeset 8, gets that version of the file. Otherwise, gets the most recent version of readme.txt before version 8.

Label

Llabel

Specifies items to which label was applied.

Examples

c:\code\SiteApp\Main>tf get readme.txt;LJulyHotFix

Gets the version of readme.txt that was labeled JulyHotFix.

c:\code\SiteApp\Main>tf get /version:LLastKnownGood

Retrieves the version of all labeled items (and deletes those items not labeled) in the workspace as they existed when the changeset labeled as LastKnownGood was created, for example, perhaps as part of an automated build process.

Date and time

Dyyyy-mm-ddTxx:xx

-or-

Dmm/dd/yyyy

-or-

Any .NET Framework-supported format.

-or-

Any of the date formats supported on the local machine.

Specifies a changeset created on a specified date and time.

Examples

c:\code\SiteApp\Main>tf get /version:D2004-03-22

Updates the workspace to match the codebase as it existed on 3/22/2004 at 00:00 (midnight).

c:\code\SiteApp\Main>tf get /version:D2004-03-22T09:00

Updates the workspace to match the codebase as it existed on 3/22/2004 at 09:00 (9 AM).

For more information about .NET Framework-supported date and time formats see DateTime and Standard Date and Time Format Strings.

Workspace (current)

W

Specifies the version in your workspace.

Workspace (specified)

Wworkspacename; workspaceowner

Specifies the version in a specified workspace.

For example: WResolveRIConflicts;PeterW

Tip

T

Specifies the most recent version.

Use options to modify how a command functions

You can use some common options to modify how a command functions.

Use the /noprompt option to suppress requests for data input and redirect output data to the command prompt window

Use the /noprompt option to suppress requests for data input and redirect output data to the command prompt window. This option can be useful when you need to use version control commands in a script because the command proceeds without intervention by a user, and the data is available for the script to perform operations such as parsing or capturing.

When you use this option, the system:

  • Suppresses all requests for input:

    • Questions are not asked in the command prompt window. For example, when you use the Undo command with this option, the system does not prompt you to confirm if you want to proceed with undoing the changes.

    • Windows and dialog boxes are not presented. For example, you use this option with the Checkin command. Instead of displaying the Check In dialog box for you to confirm your options (which items you want to check in or which work items to associate), the system proceeds with the check in without confirmation.

  • Redirects output data to the command prompt. For example, you use this option with the History command. The data is displayed in the command prompt window instead of the History window.

Use /login option to specify credentials when running a command

Use the /login option to specify the Team Foundation Server user account to run a command. This option can be useful when you are working at the machine of another team member.

For example, Julia is working with Peter at his dev machine. She uses the Lock command to unlock a file that she locked earlier:

c:\code\SiteApp\Main> tf lock /lock:none program.cs /login:JuliaI,JuliaPassword

If she wants to avoid having her password appear in the command prompt, she can enter the command without the password:

c:\code\SiteApp\Main> tf lock /lock:none program.cs /login:JuliaI

After she enters this command, the system then prompts her to type her password in a dialog box that masks her input.

Use the /lock option to apply or remove a lock

Important

As a best practice, use the /lock option with discretion and notify your teammates why you are locking an item, and when you plan to remove the lock.

Use the /lock option to apply or remove a lock at the same time you run another command such as Add or Edit.

/lock:(none|checkin|checkout)
  • None: No lock is placed on an item. If a lock is already in place, it is removed.

  • Checkin or Checkout: Applies a lock. See Understand lock types.

Note

In a few cases, the lock operation can fail:

  • If any other users have locked any of the specified items, the lock operation will fail.
  • The system ignores this switch if there is already a pending change to the item. In this case, you must use the Lock Command to change a lock on an item.

Use option shortcuts

You can abbreviate the following options.

Option

Option Alias

/comment

-C

/computer

-M

/delete

-D

/force

-P

/format

-F

/help

-?, -H

/lock

-K

/login

-Y

/newname

-N

/noprompt

-I

/owner

-O

/recursive

-R

/server

-S

/slotmode

-X

/template

-T

/user

-U

/version

-V

/workspace

-W

Understand exit codes

Version control commands return the following exit codes:

Exit Code

Definition

0

Success.

1

Partial success; this means at least something, or possibly everything, failed to succeed.

2

Unrecognized command.

100

Nothing succeeded.

For example:

c:\code\SiteApp\Main\SolutionA\Project1\>tf checkout program1.cs program2.c

If one of the files you are trying to check out does not exist on the server, the command returns 1 to indicate partial success.