How to use `hg amend` with HgGit to avoid `unknown revision` errors

93 Views Asked by At

Whenever I use hg amend or other commands that change history (for example hg rebase -s ... -d ...), certain commands, including hg push start to fail with abort: unknown revision error:

$ hg push -B my-branch
pushing to git+ssh://[email protected]:...
searching for changes
abort: unknown revision 'xxxxx'!
1

There are 1 best solutions below

0
On

When using hg amend or other history-altering commands in Mercurial (Hg), especially when working with the HgGit extension to interact with Git repositories, you might encounter abort: unknown revision 'xxxxx'! errors: often, this is because these operations rewrite history, which can confuse the mapping between Mercurial changesets and Git commits.

To avoid this issue, follow these steps when you need to amend a commit or rebase:

  • use hg commit --amend carefully: Make sure you only amend commits that have not been pushed to a Git repository. Amending already pushed commits can create discrepancies between your local repository and the remote Git repository.

  • Push changes immediately after amending: If you are amending a commit that is part of a series of commits not yet pushed to the remote Git repository, push your changes immediately after amending to minimize discrepancies.

  • Recreate the mapping: If you have already encountered the unknown revision error, you may need to recreate the mapping between your Mercurial repository and the Git repository. That can involve pulling from the Git repository again, or using hg strip to remove the problematic commits locally and then re-applying your changes.

If you are aware of the implications and are certain that no other users will be affected (for example, in a personal project), you could use hg push --force to override the remote repository. Be very cautious with this approach, as it can lead to lost work for others.

# Option 1: Force push (caution)
$ hg push -B my-branch --force

# Option 2: Fix the mapping (more complex, might involve pulling and merging)

You can also test schacon/hg-git/issues/298

A Problem can be solved by follow command:

hg rebase -b --keepbranch

or add in .hgrc

[default]
  rebase= --keepbranch