Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong>: This process is so common, that the git team made it much simpler with a new tool, <code>git subtree</code>. See here: <a href="https://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository/17864475#17864475">Detach (move) subdirectory into separate Git repository</a></p> <hr> <p>You want to clone your repository and then use <code>git filter-branch</code> to mark everything but the subdirectory you want in your new repo to be garbage-collected.</p> <ol> <li><p>To clone your local repository:</p> <pre><code>git clone /XYZ /ABC </code></pre> <p>(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.)</p></li> <li><p>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:</p> <pre><code>cd /ABC for i in branch1 br2 br3; do git branch -t $i origin/$i; done git remote rm origin </code></pre> <p>or for all remote branches:</p> <pre><code>cd /ABC for i in $(git branch -r | sed "s/.*origin\///"); do git branch -t $i origin/$i; done git remote rm origin </code></pre></li> <li><p>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 <code>WARNING: Ref 'refs/tags/v0.1' is unchanged</code> for all tags (since they were all unrelated to the subproject); additionally, after removing such tags more space will be reclaimed. Apparently <code>git filter-branch</code> should be able to rewrite other tags, but I could not verify this. If you want to remove all tags, use <code>git tag -l | xargs git tag -d</code>.</p></li> <li><p>Then use filter-branch and reset to exclude the other files, so they can be pruned. Let's also add <code>--tag-name-filter cat --prune-empty</code> to remove empty commits and to rewrite tags (note that this will have to strip their signature):</p> <pre><code>git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC -- --all </code></pre> <p>or alternatively, to only rewrite the HEAD branch and ignore tags and other branches:</p> <pre><code>git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter ABC HEAD </code></pre></li> <li><p>Then delete the backup reflogs so the space can be truly reclaimed (although now the operation is destructive)</p> <pre><code>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 </code></pre> <p>and now you have a local git repository of the ABC sub-directory with all its history preserved. </p></li> </ol> <p>Note: For most uses, <code>git filter-branch</code> should indeed have the added parameter <code>-- --all</code>. Yes that's really <kbd>-</kbd><kbd>-</kbd><kbd>space</kbd><kbd>-</kbd><kbd>-</kbd> <code>all</code>. 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.</p> <p>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).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload