Git subtree - Detach move subdirectory into separate Git repository

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

http://en.wikipedia.org/wiki/Git_%28software%29 http://en.wikipedia.org/wiki/Git_%28software%29

How can I do this while keeping the history of the files within the subdirectory?

I guess I could make a clone and remove the unwanted parts of each clone, but I suppose this would give me the complete tree when checking out an older revision etc. This might be acceptable, but I would prefer to be able to pretend that the two repositories doesn t have a shared history.

Just to make it clear, I have the following structure:

XYZ/
    .git/
    XY1/
    ABC/
    XY2/

But I would like this instead:

XYZ/
    .git/
    XY1/
    XY2/
ABC/
    .git/
    ABC/

Answers

http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository/17864475#17864475 http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository/17864475#17864475


You want to clone your repository and then use git filter-branch to mark everything but the subdirectory you want in your new repo to be garbage-collected.

    • To clone your local repository:
    git clone /XYZ /ABC
    
    (Note: the repository will be cloned using hard-links, but that is not a problem since the hard-linked files will not be modified in themselves - new ones will be created.)
    • Now, let us preserve the interesting branches which we want to rewrite as well, and then remove the origin to avoid pushing there and to make sure that old commits will not be referenced by the origin:
    cd /ABC
    for i in branch1 br2 br3; do git branch -t $i origin/$i; done
    git remote rm origin
    

    or for all remote branches:

    cd /ABC
    for i in $(git branch -r | sed "s/.*origin///"); do git branch -t $i origin/$i; done
    git remote rm origin
    
    • Now you might want to also remove tags which have no relation with the subproject; you can also do that later, but you might need to prune your repo again. I did not do so and got a WARNING: Ref refs/tags/v0.1 is unchanged for all tags (since they were all unrelated to the subproject); additionally, after removing such tags more space will be reclaimed. Apparently git filter-branch should be able to rewrite other tags, but I could not verify this. If you want to remove all tags, use git tag -l | xargs git tag -d.
    • Then use filter-branch and reset to exclude the other files, so they can be pruned. Let s also add --tag-name-filter cat --prune-empty to remove empty commits and to rewrite tags (note that this will have to strip their signature):
    git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC -- --all
    

    or alternatively, to only rewrite the HEAD branch and ignore tags and other branches:

    git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC HEAD
    
    • Then delete the backup reflogs so the space can be truly reclaimed (although now the operation is destructive)
    git reset --hard
    git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
    git reflog expire --expire=now --all
    git gc --aggressive --prune=now
    
    and now you have a local git repository of the ABC sub-directory with all its history preserved.

Note: For most uses, git filter-branch should indeed have the added parameter -- --all. Yes that s really dash dash space dash dash all. This needs to be the last parameters for the command. As Matli discovered, this keeps the project branches and tags included in the new repo.

Edit: various suggestions from comments below were incorporated to make sure, for instance, that the repository is actually shrunk (which was not always the case before).

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils