Core Concepts

  • Repository (Repo): A project’s main folder on GitHub containing code, files, and the entire version history.
  • Branch: A parallel version of your project, enabling multiple lines of development.
  • Pull Request (PR): A request to merge changes from one branch into another, with space for discussions, code reviews, and testing.
  • Fork: A copy of someone else’s repository under your GitHub account, used for independent changes.
  • GitHub Actions: GitHub’s CI/CD tool for automating workflows, like running tests and deploying code.

GitHub Workflow Basics

Create a New Repository

  1. On GitHub, go to your profile > Repositories > New.
  2. Fill in name, description (optional), and choose public/private.
  3. Optionally add a README, .gitignore, or license.
  4. Click Create repository.

Clone a Repository

git clone [URL]
  • Downloads the repository to your local machine.

Create and Switch Branches

# Create a new branch
git checkout -b [branch-name]
 
# Switch to an existing branch
git checkout [branch-name]

Commit Changes

# Stage all changes
git add .
 
# Commit with a message
git commit -m "Your message here"

Push Changes to GitHub

git push origin [branch-name]

Working with Pull Requests (PRs)

Open a Pull Request

  1. Push your branch to GitHub.
  2. Go to your repo on GitHub, and it will suggest a new pull request.
  3. Fill in the title and description, referencing issues if necessary (Fixes #issue-number).
  4. Add reviewers, labels, and set any linked issues.
  5. Click Create Pull Request.

Review and Merge a Pull Request

  1. Go to the Pull Requests tab > click on the PR.
  2. Review changes: You can leave comments, approve, or request changes.
  3. Once approved, click Merge pull request.
  4. Choose Delete branch to clean up after merging.

Managing Issues

Creating and Linking Issues

  1. Go to the Issues tab and click New Issue.
  2. Fill in the title and description.
  3. Assign it, add labels (bug, enhancement, etc.), and link PRs if relevant.
  4. Reference an issue in a commit or PR by using #issue-number (e.g., Fixes #42).

Closing Issues

  • Automatically close an issue by including keywords in PR or commit messages (e.g., Fixes #issue-number).

GitHub Commands Cheat Sheet

Basic Git Commands for GitHub

CommandDescription
git clone [URL]Clone a GitHub repo locally.
git checkout -b [branch-name]Create and switch to a new branch.
git checkout [branch-name]Switch to an existing branch.
git add [file-name] or git add .Stage changes for commit.
git commit -m "message"Commit staged changes with a message.
git push origin [branch-name]Push changes to GitHub.
git pull origin [branch-name]Update local branch with GitHub changes.
git fetch --allFetch updates from all branches without merging.
git merge [branch-name]Merge another branch into the current one.
git log --onelineView commit history in a single line per commit.
git statusSee changes staged, unstaged, and untracked.

GitHub Shortcuts and Tips

  • @mentions: Tag people in issues and comments using @username.
  • Autoclose Issues: Reference an issue in a PR with Fixes #issue-number to close it automatically.
  • GitHub Pages: Go to Settings > Pages to host a website from your repo.
  • Keyboard Shortcuts:
    • g i: Go to Issues.
    • g p: Go to Pull Requests.
    • g b: Go to Code (repo home).
    • t: Search files in the repository.

GitHub Actions (CI/CD)

Basic Workflow

  1. Create a .github/workflows directory in your repo.
  2. Add a .yml file for each workflow (e.g., ci.yml).
  3. Define the steps, such as building and testing.
name: CI
 
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
 
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'
      - run: npm install
      - run: npm test

Common GitHub Action Triggers

  • push: Run a workflow when pushing changes.
  • pull_request: Run a workflow for PR events.
  • schedule: Set up cron jobs (e.g., daily or weekly tests).

Forking and Contributing to Others’ Repos

Forking a Repository

  1. Click Fork on the original repository.
  2. Clone your fork to make changes locally:
    git clone [forked-repo-URL]

Contributing Changes

  1. Make changes in a new branch and push to your fork.
  2. Go to the original repository and open a pull request from your forked branch.
  3. Describe your changes, reference issues, and create the PR.

GitHub Best Practices

  1. Commit Often: Make frequent, small commits with clear messages.
  2. Use Branches: Separate features and bug fixes by using branches.
  3. Code Reviews: Request code reviews to catch issues early and improve quality.
  4. Automate with Actions: Use GitHub Actions for continuous testing and deployment.
  5. Write Descriptive PRs: Clearly describe the changes, link issues, and add context.