Setting Up a GitHub Repository Using Git CLI — Made Easy!

Sravya BollaSravya Bolla
2 min read

GitHub is the go-to platform for hosting and collaborating on code. If you’re new to Git or GitHub, this guide will make setting up your GitHub repository using the Git command line interface (CLI) simple and straightforward.

Whether you have an existing project or starting fresh, this step-by-step walkthrough will help you link your local project to GitHub and push your first commit with ease.

Why Use Git CLI?

Using Git via the command line gives you full control over your repositories. It’s a skill every developer should master to manage code effectively and collaborate with others.


Step 1: Create a Remote Repository on GitHub

First, you need to create a repository on GitHub to host your project remotely.

  1. Log in to your GitHub account.

  2. Click the + icon in the top-right corner and select New repository.

  3. Enter a repository name (e.g., my-project).

  4. Optionally add a description.

  5. Choose repository visibility: Public or Private.

  6. Do not initialize with README, .gitignore, or license — you will add these locally.

  7. Click Create repository.

Once created, GitHub will show you the repository URL. Copy it — you’ll use it in the next steps.

Step 2: Initialize Git Locally

Open your terminal or command prompt and navigate to your project folder:

cd path/to/your/project

Initialize Git in this folder:

git init

Now connect your local repository to the remote GitHub repository you created:

git remote add origin https://github.com/your-username/your-repo.git

Replace your-username/your-repo.git with your actual GitHub repository URL.

To verify the remote URL was added correctly, run:

git remote -v

You should see output like:

origin https://github.com/your-username/your-repo.git (fetch)

origin https://github.com/your-username/your-repo.git (push)

Step 4: Add Your Files and Commit

Add all your project files to the Git staging area:

git add .

Then commit the files with a descriptive message:

git commit -m "Initial commit"

Step 5: Push Your Code to GitHub

Push your local commits to the remote repository’s main branch:

git push -u origin main

The -u flag sets the upstream branch, so future pushes can be done with just git push.

Bonus: Set Your Git Username and Email (If Not Set)

Git tracks the author of each commit using your name and email. Configure them globally on your machine (only once) like this:

git config --global user.name "Your Name"

git config --global user.email "you@example.com"


You’re All Set!

Your local project is now connected to GitHub, and your code is live in your remote repository. From here, you can continue to commit changes and push updates easily.

0
Subscribe to my newsletter

Read articles from Sravya Bolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sravya Bolla
Sravya Bolla