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
- Log in to GitHub
- Click New repository
- Fill in:
- Repository name
- Description (optional)
- Visibility: Public or Private
- 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
withmaster
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
andgit log
to track changes
๐ Summary
Task | Command/Action |
---|---|
Create GitHub repo | On github.com |
Initialize Git | git init |
Stage files | git add . |
Commit changes | git commit -m "Initial commit" |
Connect to GitHub | git remote add origin <repo-url> |
Push to GitHub | git push -u origin main |