When working on a local project, we usually create the repository on GitHub first, copy the remote URL, and then connect it to our local project.
But there is a cleaner way to do it directly from the terminal using GitHub CLI.
1. Initialize Git in Your Project
Go to your project folder:
cd your-project-name
Initialize Git:
git init
2. Authenticate with GitHub CLI
If you have not logged in yet, run:
gh auth login
During the process, you can choose:
GitHub.com
SSH
Login with a web browser
3. Create the Remote Repository from Local
To create the repository on GitHub and add it as origin, run:
gh repo create repository-name --public --source=. --remote=origin
Example:
gh repo create IA_ML_DL_python_fundamentos --public --source=. --remote=origin
For a private repository:
gh repo create repository-name --private --source=. --remote=origin
4. Verify the Remote
Check that origin was added correctly:
git remote -v
You should see something like:
origin git@github.com:username/repository-name.git (fetch)
origin git@github.com:username/repository-name.git (push)
5. Create the First Commit
Add your files:
git add .
Create the first commit:
git commit -m "Initial commit"
6. Push the Project to GitHub
Make sure your main branch is named main:
git branch -M main
Push your project:
git push -u origin main
Full Command Summary
git init
gh auth login
gh repo create repository-name --public --source=. --remote=origin
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main
Conclusion
Using gh repo create allows you to create a GitHub repository directly from the terminal, connect it as origin, and push your local project without manually creating the repository from the browser.
It is a faster, cleaner, and more professional workflow for starting new projects on GitHub.
Top comments (0)