# How to split out a piece of a package into a separate package

**URL:** <https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010>\
**Category:** General Usage\
**Tags:** package, git\
**Created:** [April 2, 2017, 2:56pm UTC](https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010 "2017-04-02T14:56:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 2, 2017, 2:56pm UTC](https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010/1 "2017-04-02T14:56:16Z")

</div>

I need to split out a piece of functionality into a separate package. What’s the best way to do this while preserving the git history?

Since I know there are several success stories, I thought it would be useful to codify this here (even though it’s more a git issue than a Julia one).

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 2, 2017, 6:23pm UTC](https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010/2 "2017-04-02T18:23:16Z")

</div>

To answer my own question:

1. Make a _new copy_ of the package using `git clone OLD NEW`

2. Change to the `NEW` directory

3. To keep only certain subdirectories in the new package, use the following single-line `git` command, listing those subdirectories that you wish to _keep_ (in this example, `src/root_finding` and `test/root_finding_tests`):  
`git filter-branch --index-filter 'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- src/root_finding test/root_finding_tests' --prune-empty -- --all`

4. Make a new remote repository (e.g. using `hub create`) and push the new package there.

5. In the repository for the old package, delete the files that have been moved, using `git rm`.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [December 14, 2017, 9:23pm UTC](https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010/3 "2017-12-14T21:23:42Z")

</div>

I have a question.

Wouldn’t this mean that users that `Pkg.add` both packages will also download both identical histories twice (and also take twice as much memory in disk)?

Is splitting without keeping history allowed?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 14, 2017, 9:56pm UTC](https://discourse.julialang.org/t/how-to-split-out-a-piece-of-a-package-into-a-separate-package/3010/4 "2017-12-14T21:56:53Z")

</div>

> [@Datseris](#):
>
> Wouldn’t this mean that users that Pkg.add both packages will also download both identical histories twice (and also take twice as much memory in disk)?

Will not be a problem in a while (Pkg3) since using git repos will not be the common case.
