Compartir vía


Use Team Foundation version control commands

TFS 2018

Visual Studio 2019 | Visual Studio 2022

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

Run a command

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

Note

For Visual Studio 2019 and later versions, the tf.exe binary is no longer in a fixed location in the Visual Studio install path as in some previous releases, for example, C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE. If your script uses tf.exe, don't 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's 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, use the following command:

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

Set up your development machine and manage workspaces

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

For more information, see the following resources:

Develop your app

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

For more information, see Develop your app in Team Foundation version control.

Suspend your work

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

For more information, see 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.

For more information, see Check in your work to the team's codebase.

Manage files and solve problems

Use the resources in the following sections to manage files.

View and manage version control files and folders

For more information, see Use Source Control Explorer to manage files under version control.

View and manage past versions

For more information, see View and manage past versions.

Compare folders and files

For more information, see View and manage past versions.

Resolve file conflicts

  • Resolve command: Resolves conflicts between items in your workspace and on the server.

For more information, see Resolve Team Foundation Version Control conflicts.

Work with version control locks

For more information, see Work with version control locks.

Isolate risk

Use the following commands to isolate risk by using branches:

For more information, see Use branches to isolate risk in Team Foundation Version Control.

Administer version control

Use the following commands to manage your version control system:

For more information, see Configure check-out settings.

Get help on version control commands

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

Understand command syntax

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

Required and optional arguments

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

Exclusive arguments

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

Verbatim and replaceable arguments

Items that aren't enclosed in brackets are options that you include verbatim. Items enclosed in angle brackets (< and >) 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, consider the Checkout command:

tf checkout [/lock:( none|checkin|checkout)] [/recursive] <item-spec> [/login: <username>, [<password>]]

This example includes the following arguments:

  • <item-spec>: You must replace this argument with an item specification that identifies the items that you're checking out.
  • The following arguments are optional. If you don't supply them, none of their effects apply to the command:
    • /lock:(none|checkin|checkout): If you don't specify the /lock option, the system uses /lock:none by default. Otherwise, you can specify one of the other lock options.
    • /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 and replace <username> with the name of the user. If necessary, replace <password> with the user's password.

Specify the items affected by a command

You can use item specifications and version specifications to specify which items are affected by a command.

Use an item specification argument to specify affected items

You use an item specification to specify the items affected by a command. You can specify items either on a client machine or on your Azure DevOps server. You can use wildcard characters such as * and ?.

Client item specification arguments

A client item specification 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.
  • Multiple files, for example, c:\code\SiteApp\Main\SolutionA\*.cs.
  • A universal naming convention (UNC) path, such as \\myshare\code\SiteApp\Main.

Server item specification arguments

A server item specification argument specifies a path to items on your Azure DevOps server such as:

  • A folder, for example, $/SiteApp/Main/SolutionA.
  • A file, for example, $/SiteApp/Main/SolutionA/Project1/program.cs.
  • Multiple files, for example, $/SiteApp/Main/SolutionA/*.cs.

You typically use server item specification arguments when you need to run a command on items that aren't on the client machine. For example, say you're working on a development machine. If you need to get some revision history data about some items that are in a project collection that you don't work in, you can use the following command:

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

Multiple item specification arguments

For some commands, you can specify multiple item specification arguments, for example:

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

This command checks out program.cs and program2.c.

Use a version specification argument to specify affected versions of items

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

  • Use the /version option, for example, /version:C44.

  • Append the version specification to an item specification 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/2022~D4/24/2022

Use the following syntax to specify a version specification:

Type Syntax Description Examples Result
Changeset [C]<version-number> Specifies items based on a changeset number. If an item that's in scope wasn't modified in the specified changeset, the system takes the latest version of the item that occurred before the specified changeset. You can omit C if you specify only a number. tf get readme.txt /v:C8

tf get readme.txt /v:8

tf get readme.txt;8
If readme.txt was modified in changeset 8, the example code gets that version of the file. Otherwise, it gets the most recent version of readme.txt before version 8.
Label L<label> Specifies items that a label is applied to. tf get readme.txt;LJulyHotFix

tf get /version:LLastKnownGood
The first example gets the version of readme.txt that was labeled JulyHotFix. The second retrieves the version of all labeled items (and deletes those items not labeled) in the workspace as they existed when the changeset labeled LastKnownGood was created. You might use the code in the second example as part of an automated build process.
Date and time D<yyyy-mm-ddTxx:xx>

or

D<mm/dd/yyyy>

or

Any .NET Framework-supported format.

or

Any of the date formats supported on the local machine.
Specifies a changeset that was created on a specified date at a specific time. tf get /version:D2022-03-22

tf get /version:D2022-03-22T09:00
The first example updates the workspace to match the codebase as it existed on March 22, 2022 at midnight. The second updates the workspace to match the codebase as it existed on March 22, 2022 at 9:00 AM. For more information about .NET Framework-supported date and time formats, see DateTime and Standard date and time format strings.
Current workspace W Specifies the version in your workspace. - -
Specified workspace W<workspace-name>; <workspace-owner> Specifies the version in a specified workspace. tf get /version:WResolveRIConflicts;PatW The example specifies the version in the ResolveRIConflicts workspace that PatW owns.
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 data input requests and redirect output data

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 where:

  • The command proceeds without intervention by a user.
  • The data is available for the script to perform operations on, such as parsing or capturing.

When you use this option, the system:

  • Suppresses all requests for input:

    • Questions aren't asked in the command prompt window. For example, when you use the Undo command with this option, the system doesn't prompt you to confirm whether you want to undo the changes.
    • Windows and dialog boxes aren't displayed. For example, you can use this option with the Checkin command. The system doesn't display the Check In dialog box for you to confirm items and associated work items. Instead, the system checks in the items without confirmation.
  • Redirects output data to the command prompt. For example, you can use this option with the History command. The data is displayed in the command prompt window instead of the History window.

Use the /login option to specify credentials

Use the /login option to specify the Azure DevOps server user account to run a command in. This option can be useful when you work at the machine of another team member.

For example, say you're working at your team member's development machine. You use the Lock command to unlock a file that you locked earlier:

c:\code\SiteApp\Main> tf lock /lock:none program.cs /login:<username>,<password>

If you want to avoid having your password appear in the command prompt, you can enter the command without the password:

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

After you enter this command, the system prompts you to enter your password in a dialog box that masks your input.

Use the /lock option to apply or remove a lock

Important

As a best practice, use the /lock option with discretion. Inform your teammates why you're 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 that you run another command such as Add or Edit.

/lock:(none|checkin|checkout)

The /lock command uses the following options:

  • None: No lock is placed on an item. If a lock is already in place, it gets removed.

  • Checkin or Checkout: A lock is applied. For more information, 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 fails.
  • If there's already a pending change to the specified item, the system ignores this switch. 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. At least something, or possibly everything, failed to succeed.

2

Unrecognized command.

100

Nothing succeeded.

For example, say you run the following command:

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

If one of the files you're trying to check out doesn't exist on the server, the command returns 1 to indicate partial success.