Search code examples
gitgit-pushgit-subtree

git parent tree accidentally pushed into subtree


I split one repository into 3, a tracking repository with git subtree, and two repositories I was splitting into modules. Somehow I managed to push the version with the subtrees on to one of the children repositories, which messed up the structure.

Here they are for reference:

  • child1, which is now messed up

  • child2, this seems ok

  • parent, this should hold the other two

How can I restore the repositories?


Solution

  • First, make a backup since you will be nuking history.

    In the local child1 repository, determine the hash of a version before the problem started, then blow away all of history up to that point:

    cd path/to/separate/broken/child1
    git reset --hard 7b202bfc915042c714aeca9516daf67d81c36b61
    

    force push those changes to the remote

    git push --force
    

    The issues since the switch should now be gone. Switch to the parent, and then push the changes correctly

    cd path/to/parent1
    git subtree push --prefix path/to/child1/in/parent remotechildname master
    

    The problem should then be solved