GitHub (Out of our syllabus but good to know)

 A GitHub repository (repo) is a cloud-based storage location where your project's code, files, and version history are stored. GitHub allows you to collaborate with others, track changes, and manage your project.


Creating a GitHub Repository

  1. Go to GitHub:
    Visit https://github.com and log in to your account.

  2. Create a New Repository:

    • Click the "+" button (top right corner) and select "New repository."
    • Enter a repository name (e.g., my-project).
    • Add an optional description.
    • Choose to make it public (anyone can see) or private (only you and collaborators can see).
    • Optionally, add a README file, .gitignore, or a license.
    • Click "Create repository."

Cloning a GitHub Repository

To get a copy of a GitHub repository on your local machine:

  1. Copy the Repository URL:
    Go to your repository page on GitHub and click "Code" to copy the URL (HTTPS or SSH).

  2. Clone via Git Command:

    git clone <repository-url>

    Example:

    git clone https://github.com/username/my-project.git

Basic GitHub Workflow

1. Make Changes Locally

Add or edit files in your local repository.

2. Track Changes with Git

git add <filename> # Add specific file git add . # Add all changes

3. Commit Changes

git commit -m "Descriptive message"

4. Push to GitHub

git push origin <branch-name>

Example for the main branch:

git push origin main

Collaborating on a GitHub Repo

1. Forking a Repo

  • Fork: Create a personal copy of someone else's repository.
    • Go to the repository page and click "Fork."
    • This creates a copy under your GitHub account.

2. Creating a Pull Request (PR)

  • After making changes in your forked repo, you can submit a Pull Request to the original repository.
    • Click "New Pull Request."
    • Add a title and description for your changes.
    • Submit the PR for review.

3. Merging a PR

If you're the repo owner, you can review and merge pull requests submitted by others.


Useful GitHub Commands

1. Check Remote Repositories

git remote -v

2. Add a Remote Repo

git remote add origin <repository-url>

3. Fetch and Pull Changes

git fetch # Fetch latest changes git pull origin <branch> # Fetch and merge changes

GitHub Best Practices

  1. Use Meaningful Commit Messages:
    Write clear, descriptive messages for commits.

  2. Create a README:
    Document your project in a README.md file for others to understand.

  3. Use Branches:
    Develop new features or fixes on separate branches (e.g., feature/login).

  4. Add a License:
    Include a LICENSE file to define how others can use your code.

  5. Collaborate:
    Use pull requests for code review and discussion.

Comments