ArticleMarch 17, 2025

How to Use Git to Push Code to GitHub

How to Use Git to Push Code to GitHub

How to Use Git to Push Code to GitHub

Git and GitHub are essential tools for modern software development. Git helps track changes in code, while GitHub is a cloud-based hosting service for repositories. In this guide, you'll learn how to use Git to push your code to GitHub step by step.

Prerequisites

Before you start, make sure you have:

Step 1: Set Up Git (Only Once)

If you're using Git for the first time, configure it with your name and email:

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

You can verify the settings with:

git config --list

Step 2: Initialize a Git Repository

Navigate to your project folder and initialize Git:

cd /path/to/your/project git init

This creates a hidden .git folder, turning the directory into a Git repository.

Step 3: Add and Commit Files

Once Git is initialized, add your project files to the staging area:

git add .

Then, commit the changes:

git commit -m "Initial commit"

This records the changes in your local repository.

Step 4: Connect to GitHub

To push code, you need to link your local repository to a GitHub repository. Copy the remote repository URL from GitHub, then run:

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

You can verify the remote link with:

git remote -v

Step 5: Push Code to GitHub

Now, push your code to GitHub using:

git push -u origin main

If you're using a different branch (e.g., master instead of main), modify the command accordingly:

git push -u origin master

Step 6: Verify on GitHub

Go to your GitHub repository, and you should see your files uploaded.

Additional Git Commands

Here are some useful Git commands:

  • Check the status of your repository:

git status

  • Pull changes from GitHub:

git pull origin main

  • Switch branches:

git checkout -b new-branch

  • Merge branches:

git merge branch-name

Conclusion

You have successfully pushed your code to GitHub! With Git, you can collaborate, track changes, and maintain version control efficiently. Keep practicing and explore more Git commands to enhance your workflow.

Happy coding! 🚀

Comments (0)

No comments yet. Be the first to share your thoughts!

Suggested for you

Blog

From the Blog

S.O.L.I.D Principles ( System Design)
Quick Preview

S.O.L.I.D Principles ( System Design)

SOLID is an acronym for five design principles that make software designs more understandable, flexi...

Read Article
Interview Experience (Cognizant GenC)
Quick Preview

Interview Experience (Cognizant GenC)

My Cognizant GenC Next On-Campus Hiring Experience (2025–26 Batch) Recently, I took part in the Cogn...

Read Article
How to Build a Simple To-Do App with HTML, CSS, and JavaScript
Quick Preview

How to Build a Simple To-Do App with HTML, CSS, and JavaScript

How to Build a Simple To-Do App with HTML, CSS, and JavaScript Creating a to-do app is one of the be...

Read Article
Web Developer Roadmap: A Step-by-Step Guide (2025)
Quick Preview

Web Developer Roadmap: A Step-by-Step Guide (2025)

Web Developer Roadmap: A Step-by-Step Guide Web development is one of the most in-demand tech skills...

Read Article