32

I have a hypothetical Mercurial repository on disc. I'm most of the way through creating a new feature, when I realise I've made a complete mess of the file I'm working on, and want to revert that file back to its last commit state.

I can use hg update to refresh the working copy from the repository, but that updates every file.

Is there a mercurial command that can update just a single file?

Niall C.
  • 10,878
  • 7
  • 69
  • 61
richzilla
  • 40,440
  • 14
  • 56
  • 86

3 Answers3

39

There is a mercurial command to revert files. hg revert this should revert any changes. You can also pass a file name to it e.g hg revert fileName.

Sam
  • 414
  • 4
  • 9
15

hg revert fileName will revert that file back to the revision you are at. If you want to revert all changes you can run hg revert --all.

Both of these will produce fileName.orig files so you can keep the changes you are wanting to revert just in case. If you want to revert the file and not have all the .orig files you can add the -C option: hg revert fileName -C hg revert --all -C

Nicholas Tuck
  • 551
  • 2
  • 11
  • 1
    I find it better to add `*.orig` to my `.hgignore` file and then just leave the `.orig` files around. Maybe I'll need them one day :) If they bother you, then you can use the purge extension to clean them up. – Martin Geisler Apr 27 '12 at 10:36
4

I believe you can type hg revert /path/to/file/<file name> and it will just update that file.

Kodi
  • 767
  • 7
  • 20
  • 1
    Nope, that's Git that has this weird behavior for `git checkout` (in Mercurial, `hg checkout` is an alias for `hg update`). – Martin Geisler Apr 25 '12 at 07:20