Perform an empty commit with mercurial

HighCommander4 picture HighCommander4 · Aug 30, 2013 · Viewed 7.4k times · Source

With with Mercurial queues extension, I can make an empty commit with some commit message like so:

hg qnew patch_name -m "message"

Is there a way to do this without Mercurial queues? I tried simply:

hg commit -m "message"

but hg just says "nothing changed" and doesn't do the commit, and I don't see any "force" option that would override that.

If you're wondering about my motivation for doing this: we have testing infrastructure where you push to a special repository and it will trigger automated tests to run. You need to put a special string into the commit message of the tipmost commit that says which tests to run. Obviously, I don't want this string in there when I push to the actual repository. Rather than amending the commit twice (once to add the special string, and a second time to remove it), I would find it cleaner to just add an empty commit, and then roll it back -- and I can do this with mq, but I'd like to find a way to do it without mq.

Answer

Josef Eisl picture Josef Eisl · Nov 12, 2014

You can use hg commit --amend to create empty commits.

Just create an arbitrary commit and backout the change. Afterwards fold both commits together.

Example:

touch tmp                               # create dummy file
hg add tmp                              # add file and...
hg commit -m "tmp"                      # ... commit
hg rm tmp                               # remove the file again and ...
hg commit --amend -m "empty commit"     # ... commit