How to Create a Git Repository and Push It to GitHub: A Step-by-Step Guide

Ayushmaan Srivastav
3 min readOct 5, 2024

--

In this blog post, we’ll walk through the process of creating a new folder, initializing it as a Git repository, adding a file, committing changes, and pushing those changes to a new GitHub repository. We’ll also cover how to connect your local Git to your GitHub account.

Prerequisites

  • A GitHub account. If you don’t have one, sign up here.
  • Git installed on your local machine. You can download it from git-scm.com.

Step 1: Set Up Your GitHub Account with Git

Before you create a new Git repository, you need to connect Git to your GitHub account. Here’s how:

1. Configure Your Git Username and Email

Open your terminal or command prompt and run the following commands, replacing "Your Name" and "your_email@example.com" with your actual name and email address:

git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

2. Generate SSH Key (Optional but Recommended)

This step is optional, but using SSH keys adds an extra layer of security.

Generate a new SSH key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Follow the prompts and save the key in the default location. After generating the key, add it to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

3. Add Your SSH Key to GitHub

Copy the SSH key to your clipboard:

cat ~/.ssh/id_rsa.pub
  • Log in to your GitHub account, navigate to Settings > SSH and GPG keys, and click on New SSH key. Paste your key and save.

Step 2: Create a New Folder and Initialize Git Repository

  1. Create a New Directory

Open your terminal or command prompt and create a new folder:

mkdir my-git-repo
cd my-git-repo

2. Initialize Git

Initialize the directory as a Git repository:

git init

Step 3: Add a New File

  1. Create a New File

You can create a new file using any text editor. For example, to create a simple README.md file, you can run:

echo "# My Git Repository" >> README.md

2. Check the Status of the Repository

Check the status to see your untracked files:

git status

Step 4: Add and Commit Changes

  1. Add the File to the Staging Area

Add the file to the staging area:

git add README.md

Commit the Changes

Commit the changes with a meaningful message:

git commit -m "Initial commit: Add README file"

Step 5: Create a New Repository on GitHub

  1. Log in to GitHub
  2. Go to GitHub and log in to your account.
  3. Create a New Repository
  • Click the + icon in the top right corner and select New repository.
  • Fill in the repository name (e.g., my-git-repo) and description (optional). You can choose to make it public or private.
  • Click Create repository.

Step 6: Push Local Repository to GitHub

  1. Add the Remote Repository

After creating the repository on GitHub, you’ll be given a URL. Copy that URL and run the following command in your terminal, replacing USERNAME and REPO with your GitHub username and the name of your repository:

git remote add origin git@github.com:USERNAME/REPO.git

2. Push Your Changes

Push your local commits to the GitHub repository:

git push -u origin master

If you receive a message about the branch name, you might want to use main instead of master:

git push -u origin main

Conclusion

Congratulations! You have successfully created a new Git repository, connected it to your GitHub account, and pushed your changes. This workflow is fundamental for collaborating on projects and keeping your code versioned. Feel free to customize the process as per your project requirements.

--

--

No responses yet