Valentin V
Valentin V

Reputation: 25759

How do I delete all untracked files from my working directory in Mercurial?

Is it possible to delete all untracked files from my working directory? Let's say I added a bunch of files to my working directory, didn't add them via 'hg add' and now want to get rid of those new files entirely?

I'm on windows, although I'm using PowerShell, so a combined solution is also possible here.

Upvotes: 229

Views: 82442

Answers (11)

pthomaid
pthomaid

Reputation: 177

A quick/hacky way, if you do not have local changes, is to delete the folders you want from the file manager (Windows explorer for example) and then use "hg revert" which restores only the tracked files.

Upvotes: 0

Dave Cousineau
Dave Cousineau

Reputation: 13158

This works from Windows 10 command line (used cautiously of course):

for /f %g in ('hg status -un') do @echo %g & @del %g

Upvotes: 1

simplyharsh
simplyharsh

Reputation: 36373

Add the Mercurial Extension called purge. It is distributed by Mercurial.

This extension adds a “purge” command to “hg” that removes files not known to Mercurial. i.e. untracked Files. So your command would be,

hg purge

It is not enabled by default, maybe to avoid accidentally removing files that you forgot to add.

To install this extension, add this to your mercurial settings file (.hgrc on Unix, Mercurial.ini on Windows)

[extensions]
purge = 

To enable this extension temporarily you can use

hg purge --config extensions.purge= 

Upvotes: 310

Alex L
Alex L

Reputation: 1089

You can use

hg purge --all

to remove all the ignored and untracked files

(first you need to install the purge extension as explained in some answers)

Upvotes: 4

Lev
Lev

Reputation: 951

if you don't want to use purge:

rm $(hg st | grep ^? | awk '{print $2}')

Upvotes: 2

Nikolaos Georgiou
Nikolaos Georgiou

Reputation: 2874

Thanks! This worked for me also in Powershell:

hg st -un | rm

Upvotes: 20

Alex
Alex

Reputation: 8483

rm $(hg st -u)

...where -u stands for "untracked" you can also pick another state.

Upvotes: 3

tonfa
tonfa

Reputation: 24491

The proper way without purge is:

hg st -un0 | xargs -0 rm

Upvotes: 129

Alexei Tenitski
Alexei Tenitski

Reputation: 9360

Try following:

hg st -un | xargs rm

Upvotes: 3

Heinrich Filter
Heinrich Filter

Reputation: 6158

Assuming that you are using a *nix system you could run something like this:

rm `hg st | awk '/\?/ {print $2}'`

from the root of the mercurial repository.

I don't know of a standard mercurial command to achieve the same but I believe there are many more command-line options to do this. I'm sure there are "better" solutions and would be interested to hear any other suggestions.

Please use this command with caution as it was not thoroughly tested.

Upvotes: 0

Kristian
Kristian

Reputation: 6477

This should do the trick:

hg status | grep '^\?' | sed 's/^\? //' | xargs rm -rf

Upvotes: 2

Related Questions