Git Basics - Push Project to Repository

Jeremy Wood
4 min readOct 9, 2021

--

Photo by Roman Synkevych on Unsplash

Git offers us a wonderful way to share and store code, and makes version control a breeze. But if it’s new to you, the different commands can be a little unclear. Here we’ll do a quick rundown adding a project to an online repository.

First you have to pick a hosting service for your code. The most popular is Github, which is what we’ll be using for this tutorial. That being said there are certainly other options to check out.

So let’s assume we already have a project locally on our computer, and we want to “push” that project to Github. This brings us to our first command, init.

  1. First you want to be in the root directory of your project, so open up your terminal and $ cd on over to the root folder your project is located in. Once there, simply enter $ git init , and a local Git folder will be created in your root directory. This folder is an empty Git repository, or repo for short. It’s based off your current terminal location and it’s what will store your code. Note that this folder is hidden, so if you’re looking for it make sure your file explorer is set to view hidden files and folders.
  2. Now you have your local repo ready to go, but none of the code you’ve written is there yet. The next step is a $ git add command. This will stage the code you specify. It basically tells Git that any new files, or changes in the files you’ve selected, should be added to your next “commit”, or “push”. You must specify a file, or a flag with add. Some common flag options are $ git add ., which stages all new and changed files , $ git add -u , which will stage only files that were previously added to the repo, and will also delete any files that have been removed locally, and $ git add -A which is a combination of the two. Or, if you’d like, you can just add a single file with $ git add FILENAME .
  3. Now the files have been staged, but we’re still not ready to push the code to a repo on Github. Next we have to $ git commit . Here we add a message specific to this commit. When our code is on Github, we’ll be able to go back in time and see what our code looked like at any commit. This is. what gives us “version control”, as it allows us to roll back to a previous version of our code. It’s also why it’s important to leave good messages with any commit. Knowing what to write in a commit message is an important skill to have, as good notes will make debugging code much easier down the road. The code will look like this $ git commit -m "Added some new functionality" .
  4. Now that our code is staged, and has a message it’s ready to be pushed to Github. But our repo doesn’t know where its supposed to push the files. We need to add a remote that points to an origin . The origin is the path where we’re sending the repository. So you’ll need to go to Github, and create a new repository for your project to live in. Once you’ve created the repo, you’ll want the clone path. On Github, this is in the green Code dropdown menu on the repository’s main page. The path you’re looking for should look something like this, git@github.com:Username/your-repository.git . You will want to use the SSH path, as it offers added security over the HTTP path. So copy the pathway, and back in your terminal write $ remote add origin git@github.com:Username/your-repository.git .
  5. Now your path is set, and your code is all ready to go. Just enter $ git push , and your code will be sent up to your Github repository!

Keep in mind, you have a lot of options when staging, committing, and ultimately saving your repo. There are plenty of other commands you can use to customize your repo. Not to mention all the commands have their own flags that customize the data, and the way it’s sent. It’s also very important to set up a .gitignore file if you have any files secret keys or sensitive data in your repo. Just add the .gitignore file to the top level of your projects folder, and add in the names of the files you don’t want to be pushed online. Here’s a quick guide.

Using Git commands can be a little confusing at first, and they definitely take some practice to truly grasp. I highly recommend setting up a test project and practicing your with it.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jeremy Wood
Jeremy Wood

Written by Jeremy Wood

I’m a full stack engineer who loves to learn, solve problems, and fix things! When I’m not working on my code, you’ll usually find me working on my motorcycles.

No responses yet

Write a response