Robert Wagner
Robert Wagner

Reputation: 17793

TFS: How can you Undo Checkout of Unmodified files in a batch file

We use a batch file to generate code, and it automatically checks out the generated files from Team Foundation Server (TFS) so that it can regenerate them. The majority of these files are not modified, but the generator does not know this ahead of time.

The "tfs undo" command undoes the checkout, but prompts if some have been modified (which we don't want to do). We also do not want to check in the generated files right away.

Is there a command (or series of commands) to undo checkout of all unmodified files without prompting the user?

Upvotes: 180

Views: 76584

Answers (12)

YazanGhafir
YazanGhafir

Reputation: 884

The safest and fastest way I found which neither causes Visual Studio to crash nor takes a long time is:

1- Open the developer command prompt. You can do that from Visual Studio, type developer command prompt in the search field at the top.

2- get the status of all checkouted files using the following command:

tf status "$/" /s:tfs /recursive /format:detailed

3- Then you'll get a list of similar items to the following: enter image description here

Then copy the path of the directory you want to batch undo everything under from 1 in the img. Fill in that in the command below between the double quotations instead of PUT_1_HERE. Do the same for 2 and 3 without double quotations.

tf undo "PUT_1_HERE" /workspace:PUT_2_HERE;PUT_3_HERE /recursive

4- Execute the command and Voila it should take some seconds regardless how large the number of files to undo is.

Upvotes: 0

Karlovsky120
Karlovsky120

Reputation: 6362

I'm a bit late to the party, but if you have control over the batch script you could simply not check out any files during the generation process and then run something along the lines of:

tf reconcile [path-to-local-folder] /promote /adds /deletes /recursive

to make sure you check out only the files you intend to check-in. You can read more about the reconcile command and its syntax here:

https://learn.microsoft.com/en-us/azure/devops/repos/tfvc/reconcile-command

Upvotes: 0

Schwarzie2478
Schwarzie2478

Reputation: 2286

Updated this question with an answer when working with TFS2017 and VS2017/VS2019 only.

The power tools does not exist for TFS 2017 and the old ones can't work well together with it, but apparently most of the functionality has been moved to VS2017 itself or plugins (see below).

Visual Studio 2017/2019 Extension

Some actions like undo unchanged files have moved to an

extension for VS2017

extension for VS2019

"Undo Unchanged" button location:

undo unchanged button location

Known bug

You have to open the 'Source Control Explorer' (and leave it open) so that the 'Undo Unchanged' is displayed in the Action menu of the Pending Changes view. reported here.

Windows Shell Integration Extension

Also, you can still set up Windows shell integration through an separate installer which is no longer linked to TFS Power Tools.

The windows shell integration do not work exactly the same as the powertools before, but the most important actions worked for me.

Upvotes: 24

Ray Lionfang
Ray Lionfang

Reputation: 697

  1. Right-click on your project
  2. Select undo checkout, then just click okay, or whatever confirmation is left...
  3. Then, while undoing checkout, for every file that has REAL changes in it, a prompt will ask you to confirm the check out for that file... simply click "No to All"

Visual studio will know if the checked out file has changes or none.

WARNING: This method also removes new files, i.e. files that are not yet checked in to TFS. If you want to keep these files then simply exclude them from the set of files you "undo".

Upvotes: 30

Ash
Ash

Reputation: 6035

I can see Ray LionFang's approach above. Can't comment there since I don't have the rep. While I like this approach since there's no changes required to Tools etc........

  1. Right-click on your project
  2. select undo checkout, then just click okay, or whatever confirmation is left...
  3. then, while undoing checkout, for every file that has REAL changes in it, a prompt will ask you to confirm the check out for that file... simply click "No to All"
  4. Visual studio will know if the checked out file has changes or none. Be aware that this method also removes added files that are not yet checked in from TFS...

.......there's a problem with that approach in that hitting "No To All" retains a few files which are not modified. It seems to do something like Undoing unmodified files until it hits the first file that's actually modified and then ignores the rest of the unmodified files, if that makes any sense. I've only seen this effect once in a while.

A potential work-around is to follow the above process but instead of hitting "No To All", hit "No" for each file. Since this can take a while depending on the number of files you're working with, what I normally do is to hold down "ALT + N", and it just speeds through all the files while undoing ALL unmodified files.

Upvotes: 4

Scott Munro
Scott Munro

Reputation: 13606

There are a couple of points regarding the uu option for tfpt (recommended in most of the other answers) that were not clear to me at first. Firstly, this is the command line help that can be accessed with the command tfpt uu /?

Undoes redundant pending changes. If the state of an item with a pending change is the same as on the server, then the change is undone.

Usage: tfpt uu [/changeset:changesetnum] [/recursive] [/noget] [filespec...]

  • /changeset Compare the workspace to item states at the changeset version specified instead of the latest version
  • filespec... Only check the listed filespecs for redundant changes
  • /recursive Check the specified filespecs with full recursion
  • /noget Do not run get before checking

The /changeset option may not be used with filespecs or /recursive.

Now let me break down the command that is recommended in the other answers.

tfpt uu . /noget /recursive
  • tfpt uu specifies that we wish to use the 'Undo Unchanged' command.
  • . indicates (I guess) that the current working directory should be used as the filespec.
  • /noget ensures that 'get latest version' is not called before undoing the unchanged files.
  • /recursive ensures that not just the filespec will be considered but all recursive child folders and files. This seems to be dependent on the filespec - if there is none provided then the whole workspace is processed.

So there are a couple of things to note here regarding the command from above...

  • It is dependent on the working directory.
  • It does not process the entire workspace.

I have found that the following command works best for me - it will process the entire workspace.

tfpt uu /noget

Note that it is still dependent on the working directory in that tfpt uses it to determine which workspace should be processed. But as long as you provide a path to a file or folder within the workspace, you are good to go.

Upvotes: 4

Ofir
Ofir

Reputation: 2194

Thank you @mike & @ray,

I wish to make it easier.

In VS, at Tools menu, click on "External Tools".

External Tools

Click Add.

Enter title.

Command: tfpt.exe

Arguments: uu . /noget /recursive

Initial Directory: [you can choose from the arrow button].

Undo unchanged in solution

Undo unchanged in project

Two new command are added to Tools menu.

Use them when needed.

Enjoy,

Ofir

Upvotes: 123

Mike Chaliy
Mike Chaliy

Reputation: 26708

Take a look on Undo Unchanged command of the Team Foundation Server Power Tools August 2011

c:\myProject> tfpt uu . /noget /recursive

Thanks Matt Florence for link update.

Thanks Ray Vega for actual syntax.

Upvotes: 204

evermeire
evermeire

Reputation: 479

Beware that TFS undo will not revert back the File System's "Date Modified" value. This is very frustrating especially if you use tools like robocopy to sync up remote machines. Even after you undo your check out, if you saved the file thereby updaing the "Date Modified" value, that updated value will stick around even after an undo checkout.

Upvotes: 6

Ray
Ray

Reputation: 192486

Install Team Foundation Server Power Tools and run the following from the command line using tfpt.exe at the root of your project's workspace directory:

c:\myProject> tfpt uu . /noget /recursive

Including /noget is highly recommended since it prevents a forced 'get latest' of all your project's files which depending on the total number can take a extremely long time.

Upvotes: 127

Martin Woodward
Martin Woodward

Reputation: 11770

If you simply check all the files back in again that you checked out, TFS is smart enough to figure out which ones changes and only include them in the changeset that is recorded on the server.

TFS does this by comparing MD5 hashes of the files contents before and after check-in.

This is all assuming that your generation process is purely updating the same set of files, i.e. you will never have the case where a file that was generated in a previous generation is not needed in the next generation (i.e. you would want to pend a delete for that file) or that the files change name.

If your process could potentially need files deleting, the your best bet might be to look at the Team Foundation Power Tools command (tfpt) and use the tfpt online command that will only check out the files that have changed, and will be smart enough to pend deletes for any files that are no longer needed or changed name and pend adds.

Good luck,

Martin.

Upvotes: 8

achinda99
achinda99

Reputation: 5078

As far as I understood, in TFS if you checkout a team project, the whole project is checked out and you do not have control of which files are brought down. If you want to prevent checkins to certain files, you can lock them.

At work, we all hate TFS.

Upvotes: -21

Related Questions