Ashura Blogs LogoAshura
Blogs

How to Push Your Code on GitHub Using Terminal

profile

AshuraMajestic

Jul 17, 2024

book

4 min

This guide will make you learn how you can push your project on the github using cmd

Prerequisites

  • Git installed on your system.

Step 1: Check Git Installation

  1. Open the Command Prompt (cmd) or terminal.

  2. Check if Git is installed:

    git --version
    

    If Git is installed, this command will return the version number. Now close the cmd or terminal.

Step 2: Initialize Your Project for GitHub

  1. Open your project directory in cmd, VS Code terminal, or any terminal you prefer:

    cd path/to/your/project
    
  2. Check if Git is already initialized in your project:

    git status
    

    If Git is not initialized, follow these steps:

    • Initialize Git:

      git init
      
    • Add all files in the folder you want to push:

      git add .
      

    If you have files or directories you do not want to push (e.g., node_modules), create a .gitignore file and specify these paths:

    echo "/node_modules" > .gitignore
    git add .gitignore
    
    • Commit the added files:

      git commit -m "Initial commit"
      
  3. Create a new repository on GitHub. Copy the repository URL.

  4. Add the remote repository:

    git remote add origin https://github.com/yourusername/your-repo.git
    

Step 3: Setting Up SSH for Secure Connection

  1. Open Git Bash and generate an SSH key:

    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    Press Enter to accept the default file location. You will be prompted to enter a passphrase (optional).

  2. Start the SSH agent:

    eval $(ssh-agent -s)
    
  3. Add your SSH key to the agent:

    ssh-add ~/.ssh/id_rsa
    
  4. Copy your SSH key to the clipboard:

    cat ~/.ssh/id_rsa.pub
    

    Copy the output.

  5. Go to your GitHub account settings. Navigate to "SSH and GPG keys," click "New SSH key," add a title, and paste your SSH key. Click "Add SSH key."

  6. Test the connection:

    ssh -T git@github.com
    

    You should see a message confirming the successful connection.

Step 4: Pushing the Code

  1. Push your code to GitHub:

    git push -u origin master
    

    (If you are using a branch other than master, replace master with your branch name.)

Step 5: Pushing Subsequent Changes

  1. After making changes to your project, add and commit your changes:

    git add .
    git commit -m "Describe your changes"
    
  2. Push your changes:

    git push
    

Now, if you visit your GitHub repository, you should see your code.