# Transfering existing code to Pkg package structure

**URL:** https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594
**Category:** New to Julia
**Created:** [April 1, 2025, 12:34pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594 "2025-04-01T12:34:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)
#### Post date: [April 1, 2025, 12:34pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/1 "2025-04-01T12:34:38Z")

</div>

I have a (relatively) large Module where all the code is organized in my homegrown file and directory structure. I now want to transfer this code into the directory structure of a Pkg package (i.e project/manifest.toml, src/test folders etc). Ideally I would like to maintain the git history (unless this is a particularly bad idea).

My plan was something like this:  
-Generate and empty project with the same name as the Module (Pkg.generate)  
-Copy all the original files to this folder (including .git)  
-Move all the files to their appropriate places (i.e. src and test)  
-Change any path specifications in the code if necessary  
-activate the folder environment and add any packages the module uses.  
-commit changes, push.

Then I should be able to add and use the package in other applications by Pkg.add from the git repo.

Is this a good plan or am I missing something? Or is there a better way?

(git and julia newbie)

---

<div class="post-metadata">

### Author: ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)
#### Post date: [April 1, 2025, 12:49pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/2 "2025-04-01T12:49:07Z")

</div>

> [@Magnus\_Lilledahl](#):
>
> -Copy all the original files to this folder (including .git)

Make sure that you do not clobber the new Project.toml when you do this. As long as you do that, this seems like a good plan to me.

> [@Magnus\_Lilledahl](#):
>
> -Move all files to their appropriate place (src, test etc)

Make sure to use `git mv ...` when you do this

Also a good idea to check the newly generated Project.toml to see that version and author are to your liking.

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [April 1, 2025, 1:10pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/3 "2025-04-01T13:10:45Z")

</div>

It might be easier to do it the other way around. The only really useful things that will be generated are the `Project.toml` file and the folders `src`, `test` etc. (and perhaps the file `test/runtests.jl`).

So after generating the files in some new directory, I would copy them _into_ the existing project (keeping around the original `Project.toml` and `Manifest.toml` temporarily if something goes wrong, e.g. just rename them).

Then you can rename/move/edit all the files as needed and sort them into the right places, add the dependencies to the new (now empty, but containing the UUID and package name) `Project.toml`, and test if everything is working. At the end, you can make one big commit “reshaping” your existing repo into a package – that way you can keep all the git history without the hassle of duplicating the `.git` folder.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [April 1, 2025, 1:37pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/4 "2025-04-01T13:37:43Z")

</div>

Strong agreement on doing `Pkg.generate` in a temporary location, copying `Project.toml` into the existing repository, and moving the files as necessary to get the right directory structure.

---

<div class="post-metadata">

### Author: ![Magnus\_Lilledahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magnus_lilledahl/32/51079_2.png) [@Magnus\_Lilledahl](https://discourse.julialang.org/u/Magnus_Lilledahl)
#### Post date: [April 1, 2025, 2:13pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/5 "2025-04-01T14:13:44Z")

</div>

Thanks for good feedback. I will give it a shot then (moving the .toml files and folder structure instead of the whole repo).

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [April 1, 2025, 2:22pm UTC](https://discourse.julialang.org/t/transfering-existing-code-to-pkg-package-structure/127594/6 "2025-04-01T14:22:17Z")

</div>

Good luck 🙂

Since you already have a git repo, you can also restore files etc. if something goes wrong (assuming it’s also on some remote server). I often find myself taking version control for granted, but every now and then I feel truly amazed by how convenient some things have gotten nowadays.
