180

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?

CJBS
  • 15,147
  • 6
  • 86
  • 135
Robert Wagner
  • 17,515
  • 9
  • 56
  • 72
  • Note that manually undoing all changes with the expectation of choosing "No to all" when prompted about files that *have* changed will not work correctly, as it will undo `ADD` changes without prompting (see [Ray's answer](https://stackoverflow.com/a/31741239/2799848)). – Elaskanator Nov 28 '18 at 21:28

12 Answers12

204

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.

Community
  • 1
  • 1
Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105
  • 1
    This should be edited / merged to contain the usage information as part of the answer content. – mcw Aug 31 '11 at 13:39
  • 4
    You really want to read this post: http://aaubry.net/undo-checkout-on-unchanged-files-tfs.html – Byron Whitlock Dec 09 '11 at 07:28
  • 19
    hmm, somehing that almost everyone does several times daily: "Lets put it in a separate installer called Power Tools"... sigh – vidstige Mar 06 '12 at 13:16
  • @vidstige , this his how it was years ago(answer is Feb 2009). May be now better solution exists. – Mike Chaliy Mar 10 '12 at 14:52
  • 1
    @Mike Chaliy you would think. I'm trying out TFS right now, and was searching on how to do this and ended up here. – vidstige Mar 10 '12 at 17:38
  • FYI: VS 2012 Power Tools is here: http://visualstudiogallery.msdn.microsoft.com/b1ef7eb2-e084-4cb8-9bc7-06c3bad9148f Meaning it's still not baked into the core toolset. *sadtrombone* – Jeremy Wiebe Oct 11 '12 at 16:31
  • 3
    It doesn't work if your changes are merges. It seems tfpt uu always considers a merge to be a real change, even if there is no file change, and there's no option to flip that behaviour. It's ok as a default behaviour, but sometimes it's not what you want. – user1164178 Sep 17 '14 at 00:44
  • 1
    any idea how to force this command so i can throw it in a powershell script and run it as a scheduled task every few minutes? – matthewdaniel Jun 01 '16 at 18:50
  • 1
    2017 checking in, would laos like to know about a /force switch. I have to confirm it every time :( – Squirrelkiller Mar 15 '17 at 11:50
  • @matthewdaniel The switch `/noprompt` is what you are looking for (as mentioned by @johnny-kauffman in another answer's comment) – andersand Mar 22 '17 at 09:42
  • Why do people check out everything? You can just checkout the files you need to change, you can do get's on things without checking them out. – Ryan Mann Jan 22 '18 at 19:39
  • @RyanMann one use case is migrating changes between branches, because the TFS Power Tools `/migrate` command is bugged for file rename changes (it deletes the file with the old name and creates one with the new name, which cuts the TFS history on the file). – Elaskanator Nov 21 '18 at 22:35
  • This command no longer works in 2015, as it says I have no redundant changes *no matter what* (assuming the workspace mappings are even configured). – Elaskanator Nov 21 '18 at 22:37
  • Getting error 1001 when trying to install. – Anders Lindén Feb 01 '23 at 10:23
127

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.

Ray
  • 187,153
  • 97
  • 222
  • 204
  • 12
    This should be the accepted answer, it has all the relevant details (as proved by the higher score). – rikoe Jul 01 '11 at 15:03
  • 7
    Cool. Also, why not add this as a permanent 'External Tool' to Visual Studio. Taking this as an example: http://blog.kurtschindler.net/post/visual-studio-external-tools-for-git-and-more Command: [your installation dir]\TFPT.EXE Arguments: uu . /noget /recursive Initial directory: $(SolutionDir) – rohancragg Oct 31 '11 at 15:35
  • 12
    The question specifies doing this operation **without the prompt**, but none of the answers seem to address this. If you need this, the additional **/noprompt** is the option you're looking for. The PowerTools documentation is missing this! It lists this option for a few of the commands, but not this one (UU). Beware! – Johnny Kauffman Feb 14 '12 at 22:30
  • 1
    @user20358 either `cd` to where your workspace directory is (for example, mine was `c:\myProject`) or explicitly include it after `uu` (replacing the period `.` with it) – Ray Oct 27 '14 at 14:54
  • Does this have a force switch, like /Y or /force? I'm always asked if really want to undo it, and I'd like to have that automated. Put /Y there, but it tells me The Option Y requires a value. – Squirrelkiller Mar 15 '17 at 11:55
  • @user20358 Also note that using TFPT in VS2012 actually runs from VS2010, so you need to configure your TFS connection in VS2010 for it to work. – Elaskanator Nov 28 '18 at 21:31
  • We Want TFS Power Tools Back For VS2019! – Christian Jun 15 '20 at 11:34
123

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

Ofir
  • 2,174
  • 1
  • 16
  • 15
  • 3
    Very cool. You can go one step further too and add a shortcut. Note how far down your added command is (e.g. it may be the fourth). Then to to Tools -> Options -> Environment -> Keyboard -> and type in the 'Show commands containing' textbox "Tools.External" and assign a shortcut. For instance I selected Tools.ExternalCommand4 since my newly added undo command was the fourth one down. I then assigned shortcut keys Ctrl + Alt + U, Ctrl + Alt + U – Mario Feb 04 '13 at 22:52
  • 1
    Could we please have an updated version of this? If found tf.exe but what is uu command? it tells me Unrecognized command: uu. Thanks for help – Shay Jun 13 '16 at 17:03
  • 1
    @Shay : Works for me in VS2015 with the latest TFS power tools installed. – Igor Malin Jun 19 '16 at 22:26
30
  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".

Michael Richardson
  • 4,213
  • 2
  • 31
  • 48
Ray Lionfang
  • 689
  • 6
  • 17
  • 14
    `Be aware that this method also removes added files that are not yet checked in from TFS.` ... yeah, that's a pretty immediate disqualifier. – shortstuffsushi Apr 26 '16 at 21:56
  • 1
    It works for mine *Baseless* branches merge. Thanks. Usually in this case VS marked All files in solution as changed. – KoViMa Sep 23 '16 at 09:34
  • 2
    this is a hell when nuget do checkout for a 10000 files that are not required to be updated. – Ievgen Mar 16 '18 at 10:30
23

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.

itsho
  • 4,640
  • 3
  • 46
  • 68
Schwarzie2478
  • 2,186
  • 26
  • 30
  • I noticed in the question, that they want to script it, for that I don't have a solution, I'm afraid – Schwarzie2478 Oct 16 '18 at 15:39
  • 1
    I can't find the "Undo Unchanged" option in VS2017 after installing the linked extension. Did I miss something ? – Jonathan F. Feb 07 '19 at 09:01
  • I haven't used it very much. When I used it to test it for my customer, I had to click around a bit in Team Explorer and make sure that there were changes to undo. It's possible that it only appears when you only have pending changes queued for a commit that this option appears. ( or one branch changes only)... – Schwarzie2478 Feb 08 '19 at 11:45
  • 2
    @Erzékiel The option appears only when Solution Explorer is open and ready. – MatrixRonny Feb 25 '19 at 08:55
  • Well I tried what you guys said but nothing appeared. – Jonathan F. Mar 04 '19 at 15:49
  • 1
    This extension works *sometimes* and its presence **is** linked to having the TFS **Source Control Explorer** open and not Solution Explorer (as some wrongly pointed out) - do not mix up the two different control windows. However; when I say sometimes, its because it comes and goes. Right now I dont have it, I did a month ago. Its a tiny bit buggy and I fear other extensions is the culprit, such as Resharper and the like. – Christian Jun 07 '19 at 06:42
  • This extension works, however it still touches files modified date which makes VisualStudio recompile everything -_- – Kiruahxh Jan 07 '22 at 08:29
8

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.

Martin Woodward
  • 11,770
  • 31
  • 45
  • 6
    Thanks for that, however we do not want to check in the modified files at that time. The modified ones should not be checked in till the feature is done, but the unmodified ones should be removed so the dev can review the changes. – Robert Wagner Feb 14 '09 at 03:40
  • @RobertWagner If you have a gated build, then you can cancel (if it doesn't abort upon identical file detection) when you get the popup alert that the changes have to be built first. – Elaskanator Nov 28 '18 at 21:34
6

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.

evermeire
  • 479
  • 3
  • 7
  • 1
    Either use robocopy or a VCS, using a mixture is always going to give inconsistencies. Much better would be to populate each machine from VCS directly. – Richard Mar 11 '12 at 08:12
4

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.

Scott Munro
  • 13,369
  • 3
  • 74
  • 80
4

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.

Ash
  • 5,786
  • 5
  • 22
  • 42
  • please be advised as this cannot be emphasised enough. Newly added files to the project are also removed with this approach. This is mentioned on serveral of the other answers as well. – Christian Jun 07 '19 at 06:46
0

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

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
0

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.

YazanGhafir
  • 464
  • 3
  • 8
-21

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.

achinda99
  • 5,020
  • 4
  • 34
  • 42