How to Push a Project to GitHub: A Step-by-Step Guide

    Pushing your project to GitHub is an essential step for sharing your code, collaborating with others, and maintaining version control. This guide will walk you through the process using Git and SSH authentication.

    Prerequisites

    1. Git Installed: Ensure Git is installed on your system. You can download it from Git’s official website.
    2. GitHub Account: Have an active GitHub account. If you don’t have one, you can sign up at GitHub.
    3. A Local Project Directory: Have your project ready in a local directory.

    1. Configure Git with Your User Details

    Set up your username and email address for Git, which will associate your commits with your identity. Replace Ankush More and ankushitguy@gmail.com with your details:

    git config --global user.name "Ankush More"
    git config --global user.email "ankush.more.email@gmail.com"
    git config --global init.defaultBranch main


    2. Generate an SSH Key

    An SSH key enables secure authentication without the need for a password. Run the following command to generate an SSH key:

    ssh-keygen -t rsa -C "ankush.more.email@gmail.com"

    When prompted, press Enter to save the key in the default location (~/.ssh/id_rsa). This generates two files:

    • id_rsa: Your private key (keep this secure).
    • id_rsa.pub: Your public key.

    To display your public key, type:

    cat ~/.ssh/id_rsa.pub

    Copy the output of the public key.

    3. Add SSH Key to Your GitHub Account

    • Log in to your GitHub account.
    • Navigate to Settings > SSH and GPG Keys.
    • Click New SSH Key.
    • Add a label (e.g., My Laptop) and paste the copied SSH key into the text box.
    • Click Add SSH Key.

    4. Test SSH Connection

    Verify the SSH connection with GitHub:

    ssh -T git@github.com

    You should see a confirmation message similar to:

    Hi ankushitguy! You've successfully authenticated, but GitHub does not provide shell access.

    This indicates that SSH authentication is set up correctly.

    5. Initialize a Git Repository

    Navigate to your project directory and initialize a Git repository:

    cd /path/to/your/project
    git init

    This creates a .git directory in your project folder, which tracks version control.

    6. Stage and Commit Your Files

    Add Files to Git: Use git add to stage files for commit. To add all files in your project directory:

    git add .

    Create a .gitignore File: If there are files or directories you want Git to ignore (e.g., node_modules, .env), create a .gitignore file. Example:

    node_modules/
    .env

    Add it to the repository:

    git add .gitignore

    Commit Changes: Record your changes in the local repository:

    git commit -m "Initial commit"

    7. Connect to a Remote Repository

    Create a repository on GitHub. Note the repository URL (e.g., git@github.com:ankushitguy/blog.altgr.in.git).

    Add the remote repository to your local project:

    git remote add origin git@github.com:moreskylab/blog.altgr.in

    8. Push Your Project to GitHub

    Push your local repository to the GitHub remote repository:

    git push -u origin main

    The -u flag sets the origin repository as the default remote, and master is the branch you are pushing to. For subsequent pushes, you can simply type:

    git push

    Summary of Commands

    CommandDescription
    git config --global user.nameSets your Git username globally.
    git config --global user.emailSets your Git email globally.
    ssh-keygen -t rsa -C "email"Generates an SSH key for authentication.
    ssh -T git@github.comTests your SSH connection with GitHub.
    git initInitializes a local Git repository.
    git add .Stages all files for commit.
    git commit -m "message"Commits staged changes with a message.
    git remote add origin [url]Links the local repository to a remote repository.
    git push -u origin mainPushes changes to the remote repository and sets the upstream.




    Ref: https://kbroman.org/github_tutorial


    Discover more from Altgr Blog

    Subscribe to get the latest posts sent to your email.

    Leave a Reply

    Your email address will not be published. Required fields are marked *