How to Create a New GitHub Repo and Push a Local Project π
When you start a new project locally (Java, DSA notes, website, etc.), the next step is usually to push it into GitHub. This guide explains the complete workflow to:
β
Create a new GitHub repository
β
Initialize Git in your local project
β
Commit your code
β
Push it to GitHub
This is a reusable step-by-step process you can follow for any future project.
Prerequisites
Before starting, make sure you have:
- A GitHub account
- Git installed on your system
Check Git installation:
git --version
Step 1: Create a New Repository on GitHub
- Open GitHub and click β+β β New repository
- Enter a repository name
Example:JavaFoundry - Choose repo visibility:
- β Public (recommended for portfolio)
- π Private (if needed)
- Important: Do NOT check:
- βAdd a READMEβ
- βAdd .gitignoreβ
- βAdd licenseβ
(We will create them locally.)
- Click Create repository
GitHub will show the repo URL like:
- HTTPS:
https://github.com/<username>/<repo>.git - SSH:
git@github.com:<username>/<repo>.git
Step 2: Open Terminal in Your Local Project Folder
Navigate into your project folder:
cd path/to/your/project
Verify files:
ls
Step 3: Initialize Git in Your Local Folder
Run:
git init
Now your folder becomes a Git repository.
Step 4: Add a .gitignore File (Recommended)
A .gitignore tells Git what NOT to push to GitHub.
Create .gitignore:
touch .gitignore
For Java + IntelliJ projects, add:
# IntelliJ IDEA
.idea/
*.iml
# Java build output
out/
target/
*.class
# OS files
.DS_Store
Thumbs.db
Step 5: Stage Your Code
Stage everything:
git add .
Check status:
git status
Step 6: Commit Your Changes
Commit with a meaningful message:
git commit -m "Initial commit"
Step 7: Link Local Repo with GitHub Repo (Add Remote)
Add the GitHub remote:
β Using HTTPS
git remote add origin https://github.com/<username>/<repo>.git
Verify remote:
git remote -v
Step 8: Push Your Code to GitHub
Rename branch to main (GitHub standard):
git branch -M main
Push code:
git push -u origin main
β Done β your local project is now available on GitHub.
Step 9: Verify on GitHub
Go to your repo page and refresh. You should see:
- all your files
- latest commit message
- branch
main
Common Errors and Fixes
1) β remote origin already exists
Fix:
git remote remove origin
git remote add origin https://github.com/<username>/<repo>.git
2) β Authentication Failed (HTTPS)
GitHub no longer allows password authentication for git pushes.
β Fix options:
- Use Personal Access Token (PAT) as password
- OR use SSH
- OR use GitHub Desktop (easy UI)
Recommended Workflow for Future Updates
Whenever you make changes:
git add .
git commit -m "Updated notes: Constructors + GC"
git push
Bonus: Best Repo Structure for Learning Projects
If youβre building a learning repo like JavaFoundry:
JavaFoundry/
βββ src/
βββ notes/
β βββ constructors.md
β βββ methods.md
β βββ memory-management-gc.md
βββ diagrams/
β βββ *.puml
βββ README.md
βββ .gitignore
This makes your repo clean and scalable.
Summary
β
Create repo on GitHub
β
git init locally
β
git add .
β
git commit -m "..."
β
git remote add origin <url>
β
git push -u origin main