· Development · 3 min read
Move Existing Code Into a New Repository
Avoid pitfalls, save time and make your repository complete.
I’ve lost precious time repeatedly when trying to move existing code into a new source code repository. Nothing could be easier, you might guess. And this is true, unless you use repository templates that auto-generate some files in your repo to get started, which can lead to nasty merge conflicts even before you’ve synced your code with the repo.
Remote Setup
When creating a new remote source code repository for an existing local project, it’s best to create the new repository without any initial files. So skip the .gitignore
, README.md
, and License
files when setting up the repo. It must be completely empty.
Next, ensure that the folder name where the local project resides matches the name of the new repository, as this is what will happen when you git clone
the project afterward.
Once that’s done, the new code can be simply added to the new repository with the command-line steps below. For these, you need to know:
- The naming convention for the main or master branch. Here, I will assume it is
main
. - The URL of the new code repository, denoted [GIT URL] below. This can usually be copied from the web interface of the remote repository via the
Code
button, either as an HTTPS or SSH link.
Without Pre-existing Local Git Repo
$ git init
$ git add .
$ git commit -m "move code to new repository"
$ git branch -M main
$ git remote add origin [GIT URL]
$ git push -u origin main
With Pre-existing Local Git Repo
$ git remote add origin [GIT URL]
$ git branch -M main
$ git push -u origin main
Making It Even Better
The files from the code repository template that we skipped earlier should not be ignored, however. Take some additional time to have these files locally before the first commit.
.gitignore
The .gitignore
file contains several file and folder name patterns that must not be added to the remote repository, such as:
- Local build folders
- System files
- Generated files
- IDE settings
It’s a must to have a .gitignore
before the first commit to avoid polluting the repository with unwanted files.
A good starting point when beginning from scratch is to download a file from the collection at https://github.com/github/gitignore. Otherwise, you can just copy a file from a previous project.
License
If you open-source your project, you must apply a license type. This is absolutely critical for the adoption of your library since many companies will only allow their development teams to use open-source projects that fit within their open-source license policy, usually by allow-listing a number of licenses such as MIT, BSD-3, etc.
If you’re unsure which license type to use, it’s advisable to gain a basic understanding of open-source licensing first. Look up some information on the internet about permissive vs. copyleft licenses, copyright protection, permissions granted, and license compatibility when combining multiple sources into a software product.
License file texts for most open-source license types can be found at https://opensource.org/licenses.
Readme
The README.md
file contains the welcome page that’s shown in the repository web interface. This is less critical before you make your first commit. You can start with a short description of the code project and take your time to write a useful README, maintaining it as the project evolves.
Summary of Actions
- Create a new remote repository without any initial files.
- Ensure the local project folder name matches the new repository name.
- Create a
.gitignore
file to exclude unwanted files from the repository. - Choose and apply an appropriate open-source license if applicable.
- Create a
README.md
file to provide a welcome page with the project description. - Follow the command line steps above to create the first commit, add the new remote, and push the code to the new repository.