Using Git — how to add a repo to track an existing project

Tolani Benson
The Startup
Published in
3 min readJun 6, 2020
Photo by Jamie Street on Unsplash

There are a couple of options for setting up git to track projects — we can set up the repo first on GitHub, fork or clone it, open up the repo on our local computer then set up the project from inside our local copy.

However, if you’re anything like me, in the excitement at getting started you’ll probably frequently have opened up your text editor, started setting up your project, then realise you haven’t done it inside the repo. If you’ve used a library or framework to initialise your project, it will most likely have some unique ID, which can cause issues if you then try to add it to an existing repo you’ve already set up. Or maybe you just forgot to set up a repo entirely!

Worry not, there is a simple way to add a repo to track an existing project. Just follow these few steps and you’ll be set!

Create a new blank repo

Go to your GitHub account and create a new repo. Do not initialise with a README or license, as this can cause problems (you can add these in afterwards).

Screenshot from GitHub repo

If you happened to have already created a repo with the name that you wanted, and adding your project to it didn’t work, you can always delete that repo (provided there is nothing in it yet) before creating a new repo so that you can use the same name.

When you have created your repo, copy the URL:

Screenshot from GitHub Repo

Initialise your project as a git repo

Navigate to the project folder that you want to track in the CLI, then type:

$ git init

Add your repo to the project

Type the following in the CLI, pasting your repo URL into the command:

$ git remote add origin <repo-URL>

To check that this has worked you can type:

$ git remote -v

And you should get back something like:

$ origin  git@github.com:your-username/repo-name.git (fetch)
$ origin git@github.com:your-username/repo-name.git (push)

If you see this then you’re on the right track!

Add, commit and push

The final step is adding your files, committing and pushing to remote!

You have to set the upstream branch with the -u command on the first push (as you would with pushing any new branch for the first time)

$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

And that’s it! You’re done! Your project is set up and being tracked!

Don’t forget to to back and add in a README & license, which you can do from the repo on GitHub.

At this point I’d recommend switching to a new branch to develop in, to keep your master branch clean. If you don’t already know the command for creating and switching to a new branch it is:

$ git checkout -b <new-branch-name>

Remember to commit and push regularly, and give yourself descriptive commit messages so that it’s easy to track the progress of your project, and see where to go back to if you need to revert to a previous state.

To learn how to go back to a previous commit check out this post which sets out simple instructions for the process.

--

--

Tolani Benson
The Startup

Ex-financial analyst turned software engineer. Lover of learning, coding, cake and dogs. Not in that order.