Setting up a Repo for a New Project

Initialize a Git repository with proper structure and a README file

·5 min read·
GitToolingBeginner

Every project starts the same way: a clean repository with the right foundation. This tutorial walks through setting up a Git repo from scratch with all the essentials.

Prerequisites

Before you start, make sure you have:

  • A GitHub account (you can sign in and create repos)
  • Git installed (git --version)
  • A way to authenticate to GitHub from your computer
    • HTTPS: you'll sign in when pushing (often with a Personal Access Token instead of a password)
    • SSH: you've added an SSH key to your GitHub account (recommended if you do this a lot)
  • A project folder on your machine, and you can navigate to it in Terminal/PowerShell

What the heck is a repo?

A repository is just a folder that stores your stuff, that Git tracks. Usually it contains code files, in whatever language you choose to work. Often, it will also include images as well, especially when the repo is for a website or an app with a user interface. So it's natural that the first step in a new project is to create the folder which will eventually house the files we need for the final product.

Setting up the repo

Pick a name and create your project folder on your own computer, wherever you normally store project folders. So for me, my new folder would be ~/Documents/Projects/new-project-name. Keep names lowercase with hyphens -- it makes URLs and paths easier to work with.

Now we'll set up a github repo. Github is just a cloud-based platform where people store their code. I'll explain more about what that means in just a moment.

  1. Go to https://github.com and sign in.
  2. Once you're in your account home, hit the + button on the top right and select New Repository.
  3. Give it a name (again, lowercase with only dashes) and decide whether you want it to be visible to anyone or private only to you and whoever you explicitly share it with. Optionally, provide a description for anyone examining the repo among a listing of them.
  4. Hit Create Repository.

Now we have to mess with the command line interface. I realize this can be intimidating for those who have never worked with it. But I promise you, the basics are super easy to learn, and that's all you need. I suggest you google "How to navigate folders in the command line interface", and come back when you have the hang of it. For those who are already familiar, open PowerShell (PC) or Terminal (Mac) and navigate to your repo, or create it with mkdir and then cd (the command to have your terminal prompt change directory) into it. Then run:

git init
git remote add origin https://github.com/your-username/new-project-name.git

What did we just do?

  1. We told git that the folder we're in (~/Documents/Projects/new-project-name) is a code repository. Once it knows that, git starts to track every time it changes, whether that's you deleting a period or adding a GB of video files to it.
  2. Then we told git to add a remote repository - your folder's brother from another mother, if you will. In this case, it's on github.com where we created the new project to start all this. So by running this command pointing it to the url of our new github project and adding the .git at the end, we essentially link the folder on our computer with the folder stored on github's computers. Now it's known as origin and to move our local files there we "push to origin"

You now have an empty repo with no commits.

Create a README.md

A README markdown file is usually where you store basic information about the contents or purpose of the code in the repository. In this case, we just want it so that we have something we can commit and push to origin, so we'll just create an empty README.md file.

touch README.md
git add .
git branch -M main
git commit -m "Initial commit"
git push -u origin main

Windows note: if touch doesn't work, use New-Item README.md -ItemType File in PowerShell.

Ok, so what'd we do on this one?

  1. touch README.md = we created an empty README.md file. Now we have something to send to our remote repo
  2. git add . = We added all files (in this case, just the README.md) to the staging area. This is where files go when we want to send them to their brother's house, but we have to pack them all in a car first.
  3. git branch -M main = We told the driver to take the quickest way there - the main route. We're actually just renaming the default branch (version) from master to main, because for some reason every programmer decided that's what it should be called but no one has ever updated git itself to default to main.
  4. git commit -m "Initial commit" = This is where you buckle their seatbelts and hand them a message to take to their stepmom. The message can't be mean this time though, because anyone who looks at your repo will see it. We commit the files to the repo and prepare them for their journey.
  5. git push origin main = We close the car door and send the kids off to their stepmom's house. And then smile a glorious freedom smile and go make mac n' cheese to eat straight from the pot.

And that's it! You just created a repo and initialized your local codebase to link it with github. Time for a nap, my friend.