Mercurial (hg) commit only certain files

68,171

Solution 1

You can specify the files on the command line, as tonfa writes:

$ hg commit foo.c foo.h dir/

That just works and that's what I do all the time. You can also use the --include flag that you've found, and you can use it several times like this:

$ hg commit -I foo.c -I "**/*.h"

You can even use a fileset to select the files you want to commit:

$ hg commit "set:size(1k - 1MB) and not binary()"

There is no setting that will turn off the auto-add behavior and make Mercurial work like Git does. However, the mq extension might be of interest. That's an advanced extension, but it allows you do to

$ hg qnew feature-x     # create new patch
$ hg qrefresh -s foo.c  # add a file to the current patch
$ hg qrefresh -s bar.c  # add another file to the patch
$ hg qfinish -a         # convert applied patches to normal changesets

I don't really use MQ for this purpose myself, though, since I think it's enough to just specify the filenames on the command line.

Solution 2

If you want to commit a few files, and exclude many others, explicitly list the files you want to commit. -I is only needed if you want to use patterns instead of files.

If you want to commit many files, and exclude only a few files, using -X is more convenient.

E.g. given a repository containing "file_1", "file_2" and "file_3", the following are equivalent, but the latter is easier / faster to type:

hg commit file_1 file_2
hg commit -X file_3

Solution 3

As of 3.8 you can also use hg commit --interactive to select the files (before 3.8 you can use crecord extension for similar functionality). The --interactive (or just -i) flag will cause hg to prompt you for the files you want to include in the commit. As an added bonus you can even include/exclude chunks within the files.

Here is an example of what the interface looks like. Note this is an old screenshot (of crecord actually) so interface has changed slightly (but basics are the same). crecord screenshot

Note to get that interface you need the curses interface enabled. You can do that for single run with hg commit --config ui.interface=curses --interactive or generally by adding the the following to your .hgrc:

[ui]
interface = curses
Share:
68,171
Admin
Author by

Admin

Updated on August 29, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to commit only certain files with Mercurial. Because of of hg having auto-add whenever I try to commit a change it wants to commit all files. But I don't want that because certain files are not "ready" yet.

    There is

    hg commit -I thefile.foo
    

    but this is only for one file. The better way for me would be if I can turn off auto-add as in Git. Is this possible?

  • mpen
    mpen almost 6 years
    What "auto-add behavior" behaviour? Did hg used to auto-add files? Because now you have to do so explicitly or they will be untracked.
  • simpleuser
    simpleuser almost 6 years
    @mpen I think he means how gh automatically adds every modified file to a commit, unlike git in which you have to manually add modified files to the next commit, and can leave some modified files uncommitted