Basic Github Usage

Back to Main Page


Step 1: Create a Local Repository

  • Create a local directory for your repository:

    mkdir my_repository.io
  • Navigate into your directory:

    cd path/my_repository.io

Step 2: Initialize the Local Repository

  • Initialize Git in your local directory:

    git init

Step 3: Connect Your Local Repository to GitHub

  • Add a remote repository URL to your local repository:

    git remote add origin https://github.com/Sung2021/my_repository.io

Step 4: Add and Commit Files

  • Add files to the staging area:

    git add . # Add all files
    git add filename # Add a specific file
  • Check the status of your changes:

    git status
  • Commit your changes with a message:

    git commit -m "commit message"

Step 5: Push Changes to GitHub

  • Check which branch you are on:

    git branch
  • Push your changes to GitHub:

    git push origin main # Push to the main branch
    git push -f origin main # Force push to overwrite changes in the remote

Step 6: Pull Latest Changes from GitHub

  • Pull changes from the remote repository to your local repository:

    git pull origin main

Password authentication using Token

Option 1: Using a Personal Access Token (PAT)

  1. Generate a Personal Access Token:
    • Go to your GitHub account.
    • Navigate to Settings > Developer settings > Personal access tokens.
    • Click on “Generate new token”.
    • Select the scopes or permissions you’d like to grant this token. For pushing to a repository, select repo.
    • Generate the token and make sure to copy it; you won’t be able to see it again once you navigate away from the page.
  2. Use the PAT instead of your password:
    • When prompted for a password in the terminal, enter your personal access token instead of your GitHub password.

Option 2: Using SSH Keys

  1. Generate SSH Key Pair:
    • Open your terminal and run ssh-keygen -t ed25519 -C "your_email@example.com".
    • Follow the prompts to create your keys, ideally without a passphrase for automation.
    • This will create a public and private key pair. The public key ends in .pub and is safe to share.
  2. Add your SSH public key to GitHub:
    • Navigate to your GitHub account.
    • Go to Settings > SSH and GPG keys.
    • Click on “New SSH key”, give it a title, and paste your public key.
  3. Switch your remote URL from HTTPS to SSH:
    • Check your current remote URL: git remote -v.
    • Change it to SSH: git remote set-url origin git@github.com:username/repository.git.
  4. Push again using SSH:
    • Simply run your push command again: git push origin main.