How to Push Your First Project to GitHub

This article explains how you can add an existing project to GitHub.

Pushing Your First Project to GitHub
Pushing Your First Project to GitHub

GitHub(Git management tool) has become an essential platform for version control and collaborative development making it possible for developers to work together seamlessly.

Before you can utilize the goodness that comes with GitHub, you first need to push your project to the platform. In this guide, you will learn how to push a new project to GitHub.

Let's get started.

Prerequisites

Make sure you have the following:

  1. A GitHub account
  2. Git installed on your local machine. Follow this guide to install Git.
  3. A project that is not on GitHub yet.

Step 1: Ensure git is tracking your project locally

Using the terminal, navigate to the project you want to push to GitHub using the cd command. My project is on the desktop. So this is the command I run to get to it.

cd .\Desktop\Geekbits\

Once you are in the right directory, run the command below to initialize a Git repository

git init

This command creates a hidden .git folder that stores the version history of your project.

Stage and Commit Your Changes

Once git is initialized, you can add your untracked files to the staging area using the command below.

git add .

The next step is to commit your files to a new save point using the command below.

git commit -m "Initial commit"

Your files are now ready to push to the remote repository. Let's now prepare the remote repo.

Step 2: Create a remote repository on GitHub

Sign in to GitHub and on your homepage, click on the New icon.

Img 1.png

This will take you to a page where you can create your repository.

Img 2.png

Give your repo a name, a description, and any other relevant detail. Choose whether you want the repository to be public or private. When done, click on the Create repository button.

With the repo created, GitHub will give you a link to the repository and instructions on how to link the local repo to the remote one. If you find it confusing, keep reading.

Img 3.png

Copy this link as we will use it to link the two repositories together. The local repository will use this URL to push it's contents to the remote repository

Open your terminal and use this command to rename the branch. The -M flag is used to forcefully move/rename a branch. This step is optional.

git branch -M main

With the repository URL copied, you can link the two repositories together using the command below.

git remote add origin <Repo URL>

Make sure to replace with the link you copied. Here is how the command looks with the link

https://github.com/J4yJ3ff/geekbits.git

Finally, push the contents of the local repo to the remote repo with the command below.

git push -u -f origin main

The -u flag is used to set the remote origin as the upstream reference. This way, we can perform git push and git pull commands without specifying origin. On the other hand, the -f flag is to forcefully overwrite everything in the remote repository.

Once you are done, refresh your GitHub and you should see your files in your remote repo.

Summing Up

By following this tutorial, you will successfully add your first project to GitHub. If you found the article helpful, you will find our other tutorials about GitHub useful.

Thank you for reading : )

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.