DEV Community

ASHWINTH
ASHWINTH

Posted on

How to Push Your Code to GitHub

What is GitHub?

GitHub is a platform where you can store and manage your code online. It also makes it easier to work with other developers and keep track of changes in your project.

Before pushing your code, you need:

  • A GitHub account
  • Git installed on your computer
  • A project/code folder on your computer

Step 1: Create a Repository on GitHub

First, log in to GitHub and create a new repository.

my-first-project
Enter fullscreen mode Exit fullscreen mode

You can keep the repository public or private depending on your needs.

Once the repository is created, GitHub will show you the repository URL. You will use this URL to connect your local project to GitHub.

Step 2: Open Your Project in the Terminal

Open your terminal or command prompt and move into your project folder.

For example:

cd my-first-project
Enter fullscreen mode Exit fullscreen mode

Now you are inside your project folder.

Step 3: Initialize Git

If Git hasn't been initialized in your project yet, run:

git init
Enter fullscreen mode Exit fullscreen mode

This tells Git:

"Start tracking this project."

You will usually see a message saying that an empty Git repository has been initialized.

Step 4: Check Your Files

Before adding anything, you can check the status of your project:

git status
Enter fullscreen mode Exit fullscreen mode

This command shows you which files Git has noticed and which files are not being tracked yet.

Step 5: Add Your Files

Now we need to tell Git which files we want to include in our next commit.

To add all files, run:

git add .
Enter fullscreen mode Exit fullscreen mode

The . means "add everything in this folder."

You can also add a specific file:

git add index.html
Enter fullscreen mode Exit fullscreen mode

Step 6: Commit Your Changes

Now it's time to create a commit.

A commit is basically a saved version of your changes.

Run:

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

The message inside the quotes describes what you changed.

For example:

git commit -m "Added login page"
Enter fullscreen mode Exit fullscreen mode

Try to write commit messages that clearly explain your changes.

Step 7: Connect Your Project to GitHub

Now we need to connect the local project to the GitHub repository.

Copy your GitHub repository URL and run:

git remote add origin https://github.com/your-username/my-first-project.git
Enter fullscreen mode Exit fullscreen mode

You can check whether it was added correctly using:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Step 8: Push Your Code to GitHub

This is the step where your code actually gets uploaded to GitHub.

Run:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

If your branch is called master instead of main, you may need:

git push -u origin master
Enter fullscreen mode Exit fullscreen mode

After the command finishes, refresh your GitHub repository page.

You should see your project files there!

What Do These Commands Actually Mean?

It can be confusing to remember all these commands, so here's a simple explanation:

Command What it does
git init Starts Git in your project
git status Shows the current status
git add . Adds all changes
git commit -m "message" Saves your changes
git remote add origin URL Connects your project to GitHub
git push Uploads your commits to GitHub

What If You Make Changes Later?

git add .
git commit -m "Updated project"
git push
Enter fullscreen mode Exit fullscreen mode

Change code → Add → Commit → Push

A Simple Way to Remember It

If you're just starting with Git, remember these three main commands:

git add .
git commit -m "Your message"
git push
Enter fullscreen mode Exit fullscreen mode

Think of them like:

Add → "Here are my changes."

Commit → "Save these changes."

Push → "Upload them to GitHub."

Happy coding!

Top comments (0)