Getting started with GitHub and how to push new project to GitHub
By Pravin•
Summary of commands for pushing local project to GitHub:
git init . git commit -m "first commit" git branch -M main git remote add origin git@github.com:<gitusername>/<gitreponame>.git git push -u origin main
Here's a detailed step-by-step guide to pushing a locally created project to GitHub:
Step 1: Create a GitHub Repository
- Go to GitHub.
- Click on the "+" sign in the top right corner and select "New repository".
- Enter a repository name and choose whether it will be public or private.
- Click "Create repository".
- Copy the repository URL (you’ll need it later).
Step 2: Initialize Git in Your Local Project
- Open a terminal or command prompt.
- Navigate to your project folder using:
cd /path/to/your/project - Initialize Git:
This creates agit init.gitfolder inside your project, indicating it is now a Git repository.
Step 3: Add and Commit Files
- Add all project files to Git:
git add . - Commit the changes:
git commit -m "Initial commit"
Step 4: Connect to GitHub Repository
-
Add the GitHub repository as a remote origin:
git remote add origin <your-repository-url> git remote add origin https://github.com/your-username/your-repository.git #example -
Verify the remote URL:
git remote -v
Step 5: Push to GitHub
- If this is the first push, set the upstream branch and push:
git branch -M main git push -u origin main - For subsequent pushes, simply use:
git push
Step 6: Verify on GitHub
- Go to your repository page on GitHub.
- Refresh the page to see your uploaded files.
Optional: Handling Authentication
- If prompted for credentials, enter your GitHub username and personal access token (not your password).
- Alternatively, use SSH authentication by setting up SSH keys (Guide).
What if I had added readme.md file while creating repository.
In this case, you will get following error
To https://github.com/cyour-username-/your-repo-name.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/your-username-/your-repo-name.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
To override github repository you can use --force parameter
git push origin main --force
Comments
No comments yet. Be the first to comment!
Leave a Comment
Your comment will be visible after approval.