jabley
jabley

Reputation: 2222

git surgery - splitting a repository and modifying the directory structure

I have the following single repository with this structure:

my-repo/
  foo/
    contents-of-foo
  bar/
    contents-of-bar
  baz/
    contents-of-baz
  other-contents

I wish to get the following structure, ideally without losing any history / tags:

new-combined-repo/
  contents-of-foo
  bar/
    contents-of-bar
  baz/
    contents-of-baz

and

my-other-repo
  other-contents

I'm a little stuck on how to proceed. I've tried this:

$ pushd path/to/my-repo/
$ git mv bar foo/
$ git mv baz foo/
$ git commit -m "Move bar and baz prior to repository extraction"
$ cd ..
$ git clone --no-hardlinks my-repo new-combined-repo
$ pushd new-combined-repo
$ git remote rm origin
$ git filter-branch --tag-name-filter cat --subdirectory-filter foo \
      --prune-empty -- --all
$ git log --follow foo/bar/some-file

That has dropped all history for some-file prior to the git mv command.

Better suggestions? TIA.

Upvotes: 1

Views: 655

Answers (2)

JB.
JB.

Reputation: 42094

git filter-branch --tree-filter 'mv bar baz foo; rm *'

Do save a copy of your repository before you mess around that rm *, please.

Upvotes: 1

Jo Liss
Jo Liss

Reputation: 32945

Why not write a tree filter (for git filter-branch --tree-filter) that removes all directories but the ones you want to keep? Like this one (untested):

#!/bin/bash
shopt -s extglob dotglob
rm -r !(bar|baz|.gitignore)

This of course won't preserve histories of files that were moved into bar or baz, but apart from that uncommon case, the history will be kept intact.

Upvotes: 1

Related Questions