Shashank Sawant
Shashank Sawant

Reputation: 1140

Mercurial: How to add a file from an ignored folder?

I have added a folder to the file .hgignore and hence the files in it (and inside it subdirectories) are ignored. However, I just added a file to one of the subdirectories of the ignored folder. How do I add that file to list of tracked files?

I guess this is one of the downsides of using a GUI for version control. I am using SourceTree and have little direct exposure to Hg commands (although I have enabled the option to show the commands when they are executed and do observe what Hg is doing in the background). I tried googling the question but couldn't find any relevant result.

Upvotes: 0

Views: 1015

Answers (2)

Reimer Behrends
Reimer Behrends

Reputation: 8720

You can individually track an ignored file simply by adding it explicitly. From the .hgignore man page:

An untracked file X can be explicitly added with hg add X, even if X would be excluded by a pattern in .hgignore.

Your express intent to add this file is presumed to override your preferences in .hgignore.

The purpose of .hgignore is primarily to not have ignored files included when you add a directory in its entirety.

Upvotes: 4

Christophe Muller
Christophe Muller

Reputation: 5090

You need to edit your repository's ".hgignore" file and specify that you want the directory tree to be ignored except this file. For example if you have the following structure:

└── a
    ├── b.txt
    └── c.txt

and you would like to ignore everything but not "c.txt", then depending on the .hgignore file contents you can have:

Without any .hgignore file:

$ hg status
? a/b.txt
? a/c.txt

With a line to ignore everything under "a":

$ cat .hgignore 
syntax: regexp
^a/
$ hg status
? .hgignore

And with an other line to ignore everything under "a" but not the "c.txt" file:

$ cat .hgignore 
syntax: regexp
^a/(?!c.txt).*
$ hg status
? .hgignore
? a/c.txt

Hope it'll help.

Upvotes: 1

Related Questions