11

Is it possible to have a local Mercurial ignore file? Apparently the .hgignore is a file versioned as any other file. Can I have such a file next to the versioned one?

Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
  • why don't you want to version it? – Daniel A. White Oct 08 '12 at 13:57
  • Because it's for instance a local folder to do some proof of concepts... But you're right, I could just version it. For instance, I have a couple of bat scripts in the root of my project with local paths in it... I do not want these scripts to be versioned. But if everyone has a few of those bat scripts, the ignore file will get stuffed... – Lieven Cardoen Oct 08 '12 at 14:01
  • 1
    Anyway, just wanted to know if it was possible to have a local ignore file. – Lieven Cardoen Oct 08 '12 at 14:02
  • possible duplicate of [hg local ignore](http://stackoverflow.com/questions/996562/hg-local-ignore) – dimo414 Jun 10 '15 at 03:25

3 Answers3

23

Yes you can configure a local, per-user ignore file. The location and name of this file is defined in the user-specific .hgrc configuration file (usually located in the home directory of the user) under the [ui] section, e.g.:

[ui]
ignore = ~/.myhgignore

This file should be in the same format as the repository-wide .hgignore file.

Reference: http://www.selenic.com/mercurial/hgrc.5.html#ui

David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • Also on the [Tips and Tricks](http://mercurial.selenic.com/wiki/TipsAndTricks#Ignore_files_in_local_working_copy_only) page. – dimo414 May 04 '15 at 04:40
12

There are several cases here, so depending on the case you'll have different possible solutions:

  1. If the file or directory hasn't been added yet and if there is no .hgignore file, you can:
    • use .hgignore to filter it, and
    • you can commit .hgignore if this is also useful for others (e.g., *.o, etc.) or you can leave the .hgignore as local without commiting/pushing it, and in this case, you can even specify .hgignore in .hgignore as mentioned by msw so that your local version will also be ignored.
  2. If the file or directory hasn't been added yet and if there is an .hgignore file that for some reason you do not want to use (commit and push), you can:
    • declare an untracked ignore file which you will use just like .hgignore except it will stay local. You can declare this untracked ignore file either in your repo .hg/hgrc or you global $HOME/.hgrc (see the UI section doc).
  3. Finally, if the file/directory has been added to the list of tracked files, then you can't filter it with .hgignore: any file that is already committed cannot be ignored! You can either:
    • "hg forget" the file (the opposite of "hg add"), then do as the two first cases, or use the "-X" option as a default for status/diff/commit in your .hg/hgrc configuration file, e.g.,
      [defaults]
      status = -X file1 -X file2
      diff = -X file -X file2
      commit = -X file -X file3
      which will tell hg not to include these files in the use of status, diff, and commit.

Hope it'll help.

Christophe Muller
  • 4,850
  • 1
  • 23
  • 31
  • 3
    But DO NOT use forget on files shared across repos. Push's or pulls from your working repo will delete the files on the other end. – Erik Reppen Apr 10 '13 at 20:55
1

You can also ignore .hgignore in your local repository:

$ hg clone some_repository
$ cd some_repository
$ cat > .hgignore
syntax: glob
.hgignore
$ touch foo.c bar.c
$ hg status
? foo.c
? bar.c

Note the conspicuous absence of the local .hgignore in the output of hg status.

msw
  • 42,753
  • 9
  • 87
  • 112