Techtrekking

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

  1. Go to GitHub.
  2. Click on the "+" sign in the top right corner and select "New repository".
  3. Enter a repository name and choose whether it will be public or private.
  4. Click "Create repository".
  5. Copy the repository URL (you’ll need it later).

Step 2: Initialize Git in Your Local Project

  1. Open a terminal or command prompt.
  2. Navigate to your project folder using:
    cd /path/to/your/project
  3. Initialize Git:
    git init
    This creates a .git folder inside your project, indicating it is now a Git repository.

Step 3: Add and Commit Files

  1. Add all project files to Git:
    git add .
  2. Commit the changes:
    git commit -m "Initial commit"

Step 4: Connect to GitHub Repository

  1. 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
  2. Verify the remote URL:

    git remote -v

Step 5: Push to GitHub

  1. If this is the first push, set the upstream branch and push:
    git branch -M main git push -u origin main
  2. For subsequent pushes, simply use:
    git push

Step 6: Verify on GitHub

  1. Go to your repository page on GitHub.
  2. 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.