Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to move files from one git repo to another (not a clone), preserving history
    text
    copied!<p>Our Git repositories started out as parts of a single monster SVN repository where the individual projects each had their own tree like so:</p> <pre><code>project1/branches /tags /trunk project2/branches /tags /trunk </code></pre> <p>Obviously, it was pretty easy to move files from one to another with <code>svn mv</code>. But in Git, each project is in its own repository, and today I was asked to move a subdirectory from <code>project2</code> to <code>project1</code>. I did something like this:</p> <pre><code>$ git clone project2 $ cd project2 $ git filter-branch --subdirectory-filter deeply/buried/java/source/directory/A -- --all $ git remote rm origin # so I don't accidentally the repo ;-) $ mkdir -p deeply/buried/different/java/source/directory/B $ for f in *.java; do &gt; git mv $f deeply/buried/different/java/source/directory/B &gt; done $ git commit -m "moved files to new subdirectory" $ cd .. $ $ git clone project1 $ cd project1 $ git remote add p2 ../project2 $ git fetch p2 $ git branch p2 remotes/p2/master $ git merge p2 # --allow-unrelated-histories for git 2.9 $ git remote rm p2 $ git push </code></pre> <p>But that seems pretty convoluted. Is there a better way to do this sort of thing in general? Or have I adopted the right approach?</p> <p>Note that this involves merging the history into an existing repository, rather than simply creating a new standalone repository from part of another one (<a href="https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository">as in an earlier question</a>).</p>
 

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