
Push To Github in Git
🚀 How to Push Code to GitHub
Pushing to GitHub means uploading your local commits to a remote repository. Follow these steps to push your changes.
📌 Steps to Push Code to GitHub
1️⃣ Initialize a Git Repository (If Not Done)
If your project is not already a Git repository, initialize it:
git init
2️⃣ Add a Remote Repository (If Not Set)
If your repository is not linked to GitHub, add a remote:
git remote add origin https://github.com/your-username/repository-name.git
Check if a remote is already set:
git remote -v
3️⃣ Check Your Branch
Ensure you are on the correct branch:
git branch
Switch branches if needed:
git checkout main
4️⃣ Add and Commit Your Changes
Stage all changes:
git add .
Commit with a message:
git commit -m "Initial commit"
5️⃣ Push to GitHub
✅ If pushing for the first time (new repository or branch):
git push -u origin main
(-u
sets the upstream, so next time you can just use git push
.)
✅ If pushing updates to an existing branch:
git push origin main
✅ If pushing a new branch:
git push -u origin new-branch
6️⃣ Verify on GitHub
- Go to your GitHub repository.
- Refresh the page to see your changes.
🔥 Example Workflow
git initgit remote add origin https://github.com/your-username/repository-name.gitgit add .git commit -m "First commit"git push -u origin main
🎯 Summary
✅ Initialize Git (if needed) → git init
✅ Add remote (if not set) → git remote add origin URL
✅ Stage & commit changes → git add . && git commit -m "msg"
✅ Push to GitHub → git push origin branch-name