322

I want mercurial to remove several files from the current state of the repository. However, I want the files to exist in prior history.

How do forget and remove differ, and can they do what I want?

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
  • 27
    Dont' worry, you cannot remove a file from prior history in Mercurial -- the history is generally immutable unless you start using extensions. – Martin Geisler Jul 09 '09 at 21:18

5 Answers5

355

'hg forget' is just shorthand for 'hg remove -Af'. From the 'hg remove' help:

...and -Af can be used to remove files from the next revision without deleting them from the working directory.

Bottom line: 'remove' deletes the file from your working copy on disk (unless you uses -Af) and 'forget' doesn't.

Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • 7
    "hg forget" is re-introduced in Mercurial 1.3 as an easier way to do "hg remove -Af". It schedules the file for deletion without removing it from the working copy. – Martin Geisler Jul 09 '09 at 21:14
  • 3
    I would add the caveat that this is a lousy way to try to ignore changes to files that are common across two repos as this will delete those files when you push or pull from the repo you did the forget in. All this does is remove files without nuking your workspace copies of them (in my case, we have configs you need to edit for local stuff in the damned remote repo which is not my call). IMO, it's horribly misleading to call this command "forget" – Erik Reppen Apr 10 '13 at 20:46
  • Is it better to use "forget" if I already deleted some files locally to test the build or should I get them back from the server and "remove" them? – The incredible Jan Jan 19 '23 at 14:22
45

The best way to put is that hg forget is identical to hg remove except that it leaves the files behind in your working copy. The files are left behind as untracked files and can now optionally be ignored with a pattern in .hgignore.

In other words, I cannot tell if you used hg forget or hg remove when I pull from you. A file that you ran hg forget on will be deleted when I update to that changeset — just as if you had used hg remove instead.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
27

From the documentation, you can apparently use either command to keep the file in the project history. Looks like you want remove, since it also deletes the file from the working directory.

From the Mercurial book at http://hgbook.red-bean.com/read/:

Removing a file does not affect its history. It is important to understand that removing a file has only two effects. It removes the current version of the file from the working directory. It stops Mercurial from tracking changes to the file, from the time of the next commit. Removing a file does not in any way alter the history of the file.

The man page hg(1) says this about forget:

Mark the specified files so they will no longer be tracked after the next commit. This only removes files from the current branch, not from the entire project history, and it does not delete them from the working directory.

And this about remove:

Schedule the indicated files for removal from the repository. This only removes files from the current branch, not from the entire project history.

Jason Catena
  • 567
  • 4
  • 7
4

If you use "hg remove b" against a file with "A" status, which means it has been added but not commited, Mercurial will respond:

  not removing b: file has been marked for add (use forget to undo)

This response is a very clear explication of the difference between remove and forget.

My understanding is that "hg forget" is for undoing an added but not committed file so that it is not tracked by version control; while "hg remove" is for taking out a committed file from version control.

This thread has a example for using hg remove against files of 7 different types of status.

Community
  • 1
  • 1
gm2008
  • 4,245
  • 1
  • 36
  • 38
4

A file can be tracked or not, you use hg add to track a file and hg remove or hg forget to un-track it. Using hg remove without flags will both delete the file and un-track it, hg forget will simply un-track it without deleting it.

Tao
  • 561
  • 5
  • 6