Skip to content

How to add an existing project to GitHub | Things I've Learned

Posted on:January 9, 2024 at 12:00 AM

I usually start developing a new project locally, then later create a git repository and add the code to it. I can never remember the sequence of commands to do that so have to Google them every time. Whilst one (possibly the simplest) approach is to create a new GitHub repository, clone it locally and then copy and paste the code from it’s existing location, the other (more correct) approach is detailed below.


First, create a new GitHub repository for the existing project. Make a note of the GitHub URL for the repository as you’ll need this later.

Next, initialise the local repository. From a command prompt in the root of your local repository use:

git init

If you haven’t already got one, now would be a good time to create a .gitignore file. For DotNet projects this can be done using dotnet new gitignore

Next, add all the files to it using:

git add .

The . tells git to add all files

Then commit all the files:

git commit -m "Initial commit"

Feel free to use a more descriptive commit message

Add a remote reference for GitHub:

git remote add origin https://github.com/{github-user-name}/{repository-name}.git

The last section of that command is the repository’s GitHub URL. If using SSH, this can be replaced with git@github.com:{github-user-name}/{repository-name}.git, though you will need to have a public SSH key set up for your GitHub account for this to work

Finally, push the first commit to GitHub:

git push -u -f origin main

The -u flag makes the remote GitHub repository the default for your existing project, whilst the -f flag forces git to overwrite any files that already exist on GitHub with your project’s files. This is useful if you created a README.md file