Search code examples
version-controlmercurialbitbuckettortoisehggit-fork

Mercurial or Bitbucket - push set of commits to a separate fork


Let's say a repository A has some history

 commit4
    ^
 commit3
    ^
 commit2
    ^
 commit1
    ^
 commit0

If I now wanted to "split" repository A at commit2, i.e. I want a repository B to be a fork of A, where

history of A:

 commit2
    ^
 commit1
    ^
 commit0

history of B:

 commit4
    ^
 commit3
    ^
 commit2
    ^
 commit1
    ^
 commit0

How would I best do that using either bitbucket or the Mercurial Workbench?


Solution

  • Using TortoiseHg Workbench you could do the following:

    1. Right click on A in the Repository Registry in the Workbench and click Clone.
    2. Set the destination to B and click Clone.
    3. Double click on A in the Repository Registry to view it's commits.
    4. Right click on commit3, select Modify History then select Strip. This will delete every commit after commit3 and including commit3.
    5. You now have repositories A and B with the desired structure.

    Using BitBucket you could do the following:

    1. Go to the Overview page for A and click Fork in the menu on the left to create a fork of A called B.
      A and B will now both have the same full set of commits as A.
    2. Again go back to the Overview for A and click Settings at the bottom of the menu on the left.
    3. In the Settings menu click on Strip Changesets.
    4. Enter commit3 into the box and click Preview Strip.
    5. This will show you the commits that will be deleted, double check that it shows the expected commits, in this case commit3 and commit4.
    6. Click Confirm Strip to delete the commits.

    Problems to watch out for with these strategies are:

    1. Make sure you have a full backup of A somewhere because the strip action is destructive.
    2. If there are existing clones of A out there and one of them gets pushed to A on BitBucket then the stripped changes will be pushed in again. You need to make sure that all the clones also have the strip carried out on them, or that fresh clones are made.
    3. If you push from B to A then the stripped changes could be pushed back in since they're both from the same root.