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
- On GitHub, go to your profile > Repositories > New.
- Fill in name, description (optional), and choose public/private.
- Optionally add a README, .gitignore, or license.
- 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
- Push your branch to GitHub.
- Go to your repo on GitHub, and it will suggest a new pull request.
- Fill in the title and description, referencing issues if necessary (
Fixes #issue-number
).
- Add reviewers, labels, and set any linked issues.
- Click Create Pull Request.
Review and Merge a Pull Request
- Go to the Pull Requests tab > click on the PR.
- Review changes: You can leave comments, approve, or request changes.
- Once approved, click Merge pull request.
- Choose Delete branch to clean up after merging.
Managing Issues
Creating and Linking Issues
- Go to the Issues tab and click New Issue.
- Fill in the title and description.
- Assign it, add labels (bug, enhancement, etc.), and link PRs if relevant.
- 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
Command | Description |
---|
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 --all | Fetch updates from all branches without merging. |
git merge [branch-name] | Merge another branch into the current one. |
git log --oneline | View commit history in a single line per commit. |
git status | See 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
- Create a
.github/workflows
directory in your repo.
- Add a
.yml
file for each workflow (e.g., ci.yml
).
- 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
- Click Fork on the original repository.
- Clone your fork to make changes locally:
git clone [forked-repo-URL]
Contributing Changes
- Make changes in a new branch and push to your fork.
- Go to the original repository and open a pull request from your forked branch.
- Describe your changes, reference issues, and create the PR.
GitHub Best Practices
- Commit Often: Make frequent, small commits with clear messages.
- Use Branches: Separate features and bug fixes by using branches.
- Code Reviews: Request code reviews to catch issues early and improve quality.
- Automate with Actions: Use GitHub Actions for continuous testing and deployment.
- Write Descriptive PRs: Clearly describe the changes, link issues, and add context.