How to Add Code to GitHub

GitHub is a powerful platform for hosting code, collaborating with others, and managing version control. This guide walks you through adding code to GitHub using Git.


๐Ÿงฐ Prerequisites

  • โœ… Git installed
  • โœ… A GitHub account
  • โœ… An existing project folder with your code

๐Ÿ› ๏ธ Step-by-Step Instructions

๐Ÿ”น 1. Create a Repository on GitHub

  1. Log in to GitHub
  2. Click New repository
  3. Fill in:
    • Repository name
    • Description (optional)
    • Visibility: Public or Private
  4. DO NOT initialize with README, .gitignore, or license (since youโ€™ll push an existing project)

Click Create repository.


๐Ÿ”น 2. Open Terminal and Navigate to Your Project

cd path/to/your-project

๐Ÿ”น 3. Initialize Git (if not already done)

git init

๐Ÿ”น 4. Add and Commit Your Files

git add .
git commit -m "Initial commit"

๐Ÿ”น 5. Connect to GitHub Repository

Copy the GitHub repo URL (e.g., https://github.com/your-username/your-repo.git) and run:

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

๐Ÿ”น 6. Push Code to GitHub

If your default branch is main:

git branch -M main
git push -u origin main

Replace main with master if your repo uses that as the default.


๐Ÿง  Optional Tips

  • Use .gitignore to exclude unnecessary files (e.g., node_modules, .env)
  • Create a README.md for project documentation
  • Use git status and git log to track changes

๐Ÿ“Œ Summary

TaskCommand/Action
Create GitHub repoOn github.com
Initialize Gitgit init
Stage filesgit add .
Commit changesgit commit -m "Initial commit"
Connect to GitHubgit remote add origin <repo-url>
Push to GitHubgit push -u origin main
Sharing Is Caring:

Leave a Comment