4

Is it possible to do a commit without any changes in the current repository with Mercurial, like git's --allow-empty?

  • http://stackoverflow.com/questions/3561608/how-can-i-force-mercurial-to-accept-an-empty-commit – assylias May 06 '13 at 09:06
  • 1
    Why do you want to do this? – Mark May 06 '13 at 21:05
  • Reasons you might want to do this: (1) git does it (so someone thought the needs was present), (2) you're using mercurial as a git client and need to perform the same commit, (3) a concrete need: http://stackoverflow.com/a/12269801/70170 – Jason R. Coombs Jun 27 '13 at 20:59
  • @Mark I want to do this because I closed our default branch (because I needed to back out a set of changesets that turned out to be unnecessary) and I want to commit from an earlier changeset and then push to our repo. However, our CI needs to know that the branch is open, not closed, and so does my team. Instead of making an arbitrary change in a file, I want to push an empty commit as a signal that the branch has restarted from a specific point. –  Feb 17 '14 at 18:57
  • I wouldn't do this too much, but I've sometimes found it useful for adding information to the log. For instance, to point to a changeset that would be hard to find because of a misspelling in a pushed changeset's log entry. – Joshua Goldberg May 24 '14 at 05:09
  • Another reason to want this: I've got a stack of local commits, I'd like to push them (without modifications) to a test server, along with an empty patch that contains some server-specific instructions in its comment (e.g. which tests to run, etc.). – squelart May 05 '16 at 02:48
  • Possible duplicate of [Perform an empty commit with mercurial](http://stackoverflow.com/questions/18541660/perform-an-empty-commit-with-mercurial) – YSC May 16 '17 at 13:01
  • 1
    Another reason: You have a stack of two commits [A, B], and want to insert a new commit in between them. In git, I'd just do `git rebase -i` and then edit `A`. At this point, `A` is *committed*, and any new commits I make come after `A`. `hg histedit` works differently. When I edit `A`, it is *uncommitted*. I can't commit to add a commit after it. An easy workaround would be to add a nop commit, use histedit to move it between `A` and `B`, and then edit the nop commit. – Justin L. Oct 10 '18 at 00:10
  • There are [two new configuration options](https://stackoverflow.com/questions/18541660/perform-an-empty-commit-with-mercurial/71428853#71428853) added in Mercurial 3.5 (in 2015) and Mercurial 5.5 (in 2020) that deal with enabling empty commits. – ecm Mar 10 '22 at 18:13

3 Answers3

4

Trick I read somewhere:

touch tmp
hg add tmp
hg commit -m "tmp"
hg rm tmp
hg commit --amend -m "Real commit message"

Unfortunately it doesn't survive rebasing (and maybe other operations).

squelart
  • 11,261
  • 3
  • 39
  • 43
0

The answer from @Thomas above is correct but doesn't fully explain how to modify the file content of a commit.

A simple method to use this feature to force an commit empty is as follows:

  1. Add a new file (say, Dummy.txt) and commit.
  2. hg forget Dummy.txt
  3. hg commit --amend

This will remove Dummy.txt from the previous commit, leaving it empty.

Neil T
  • 1,794
  • 1
  • 12
  • 21
-2

With amend you can change the user, date, commit message and files content:

hg commit -m"new message" -d"$(date --rfc-2822)" -u"new user" --amend

These are probably all the reasons why you would want to commit an empty changeset.

Thomas Jung
  • 32,428
  • 9
  • 84
  • 114