Skip to content

Latest commit

 

History

History
222 lines (174 loc) · 3.65 KB

File metadata and controls

222 lines (174 loc) · 3.65 KB

GitHub Commands Cheat Sheet

This document covers commonly used commands for managing GitHub repositories, setting up SSH, and more.


Table of Contents

  1. Setting Up a New Repository
  2. Cloning a Repository
  3. Using SSH with GitHub
  4. Basic Git Workflow
  5. Branching and Merging
  6. Viewing Repository Status
  7. Undoing Changes
  8. Syncing with Remote

Setting Up a New Repository

Create a New Repository on GitHub

  1. Go to GitHub and log in.
  2. Click the + button in the top-right corner and select New Repository.
  3. Fill in the repository name, description, and settings (e.g., public/private).
  4. Click Create Repository.

Initialize a Local Repository

  1. Navigate to your project folder:
    cd /path/to/your/project
  2. Initialize a Git repository:
    git init
  3. Add files to the staging area:
    git add .
  4. Commit the changes:
    git commit -m "Initial commit"
  5. Link the local repository to the remote GitHub repository:
    git remote add origin https://github.com/username/repository-name.git
  6. Push the changes to GitHub:
    git push -u origin main

Cloning a Repository

Clone Using HTTPS:

git clone https://github.com/username/repository-name.git

Clone Using SSH:

git clone git@github.com:username/repository-name.git

Using SSH with GitHub

Generate an SSH Key

  1. Check for existing SSH keys:

    ls -al ~/.ssh
  2. Generate a new SSH key (if needed):

    ssh-keygen -t ed25519 -C "your_email@example.com"

    Press Enter to accept the default file location.

  3. Set a passphrase (optional but recommended).

Add SSH Key to GitHub

  1. Copy the SSH key to your clipboard:

    pbcopy < ~/.ssh/id_ed25519.pub
  2. Go to GitHub > Settings > SSH and GPG keys > New SSH key.

  3. Paste the key and save.

Test SSH Connection

ssh -T git@github.com

If successful, you'll see: Hi username! You've successfully authenticated...


Basic Git Workflow

Check Repository Status

git status

Add Files to Staging Area

git add <file-name>

Add all files:

git add .

Commit Changes

git commit -m "Your commit message"

Push Changes to Remote

git push origin main

Pull Latest Changes

git pull origin main

Branching and Merging

Create a New Branch

git branch <branch-name>

Switch to a Branch

git checkout <branch-name>

Create and Switch to a New Branch

git checkout -b <branch-name>

Merge a Branch

Switch to the main branch:

git checkout main

Merge the branch:

git merge <branch-name>

Delete a Branch

git branch -d <branch-name>

Viewing Repository Status

View Commit History

git log

View Changes in Files

git diff

View Remote Repositories

git remote -v

Undoing Changes

Unstage a File

git reset <file-name>

Revert to the Last Commit

git reset --hard HEAD

Amend the Last Commit

git commit --amend

Syncing with Remote

Fetch Changes from Remote

git fetch origin

Rebase Local Changes

git pull --rebase origin main

Force Push (Use with Caution!)

git push --force origin main