Can I change the username on a mercurial changeset?

If you've not published your repository then this shouldn't be too hard. You need to use the Convert extension to Mercurial, which will let you 'filter' your existing repository to create a new one. the --authors switch lets you edit the author for each commit as it is filtered.

If you have published your repository, please consider the impact on your users, the mercurial wiki has some reasons not to edit history.

Enable the extension by adding these lines to your .hgrc:

[extensions]
hgext.convert=

Write a file to map the old name to the new name (authors.convert.list):

user@[email protected]

Run the conversion:

hg convert --authors authors.convert.list SOURCE DEST

I just checked it, it works for me :).


If you have a single outgoing changeset, there is a very simple way to do this:

$ hg ci --amend --user "My Name <[email protected]>" -X "**"

The -X "**" option can be omitted if you don't have any local changes.


I've tried a couple of different methods (including the Convert Extension, which I found created an unrelated repository). The Mercurial wiki instructions for editing history using MQ were the ones I found most helpful. (There are of course the usual caveats about editing any publicly-known history being a bad idea, but local-changesets that only you have are OK to edit).

I'll summarise the crucial steps here, and clarify the mechanics of changing the author. Assuming the first wrong author commit is at revision BAD (and you haven't published your changes anywhere of course), you should be able to do the following (I'll assume you're at the repository root):

Enable MQ by adding this to $HOME/.hg/hgrc

[extensions]
hgext.mq=

Convert the recent changesets into patches:

$ hg qimport -r BAD:tip

(They can now be found at .hg/patches)

"Unapply" all the patches (assume they've been applied, and reverse them), to get your repository into the state of the revision before BAD:

$ hg qpop -a

If you look at your patches, you'll see that the author is encoded in a kind of comment line in all the patches:

$ grep User .hg/patches/*
.hg/patches/102.diff:# User Firstname Lastname <[email protected]>

Now use your favourite search/replace tool to fix the patches (I'm using Perl here). Let's assume you want the commit name to be [email protected]:

$ perl -pi -e 's/f\.lastname\@oops\.wrongurl\.example\.com/f.lastname\@righturl.example.com/' .hg/patches/*.diff

Now check that you have successfully changed the author name, and re-apply the patches:

$ hg qpush -a

Then convert the applied patches into proper changesets:

$ hg qfinish -a

And you're done. Your repository is still listed as related, so you won't get any complaints about pushing.