Introduction to GitHub and Working with GitHub

 

1. Introduction to GitHub

What is GitHub?

GitHub is an online platform that allows developers to host and manage their Git repositories. It's like a social network for developers where they can share their code, collaborate on projects, and contribute to others' work. GitHub provides tools for version control, project management, and collaboration, making it an essential platform for any software development project.

GitHub vs. Git

Creating a GitHub Account

Before you can start using GitHub, you'll need to create an account. Here's how:

  1. Go to github.com.
  2. Click on the Sign up button.
  3. Enter your email address and click Continue.
  4. Create a password and click Continue.
  5. Choose a username that will be visible to others on GitHub.
  6. Verify your account by solving the puzzle or CAPTCHA.
  7. Select the plan you want (you can choose the free plan).
  8. Complete the setup by following the prompts.

Once you have your GitHub account set up, you're ready to start creating and managing repositories!

 

2. Working with GitHub

Creating a Repository on GitHub

A repository is where your project's files and version history are stored. Here's how to create a new repository on GitHub:

  1. Log in to your GitHub account.
  2. On the GitHub homepage, click on the + icon in the upper-right corner and select New repository.
  3. Enter a name for your repository.
  4. Optionally, add a description to explain what the project is about.
  5. Choose whether the repository will be public (anyone can see it) or private (only you and collaborators can see it).
  6. (Optional) Add a README file, a .gitignore file, or a license to your repository.
  7. Click Create repository.

You now have a new GitHub repository ready for your project!

Cloning a Repository

Cloning a repository means making a local copy of a remote GitHub repository on your computer. Here's how to do it:

  1. Go to the GitHub repository you want to clone.
  2. Click the Code button.
  3. Copy the repository's URL by clicking the clipboard icon.
  4. Open your terminal or Git Bash on your computer.
  5. Navigate to the directory where you want to store the cloned repository.

'cd path/to/your/directory

  1. Use the git clone command followed by the URL you copied:

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

  1. Git will download the repository to your local machine.

Connecting Git to GitHub

If you have an existing local Git repository and want to link it to a GitHub repository, follow these steps:

  1. Go to GitHub and create a new repository (as explained above).
  2. Copy the repository's URL.
  3. Open your terminal and navigate to your local Git repository.
  4. Link the local repository to GitHub using the following command:

git remote add origin https://github.com/username/repository-name.git

  1. Verify that the remote repository is added:

git remote -v

Pushing Changes

Once your local repository is connected to GitHub, you can push your commits to the remote repository:

  1. Make sure your changes are committed:

git commit -m "Your commit message"

  1. Push the changes to GitHub:

git push origin main

Replace main with the name of your branch if it's different.

Pull Requests

Pull requests (PRs) are a way to propose changes to a project. When you submit a pull request, the project maintainers can review your changes, discuss them, and merge them into the main codebase.

  1. Fork the repository you want to contribute to (see "Collaborating on GitHub").
  2. Clone the forked repository to your local machine.
  3. Make changes and commit them.
  4. Push the changes to your forked repository on GitHub.
  5. Go to the original repository on GitHub.
  6. Click Pull Requests and then New pull request.
  7. Select the branches you want to compare (your fork and the original repository).
  8. Review your changes and submit the pull request.

Collaborating on GitHub

When working on open-source projects or collaborating with others, you may want to contribute to a project that you don't have write access to. Here's how:

  1. Fork a Repository: Forking is creating your own copy of someone else's repository.
  2. Clone Your Fork: Clone your fork to your local machine (as explained earlier).
  3. Make Changes: Work on the project locally, commit your changes, and push them to your forked repository.
  4. Submit a Pull Request: Submit a pull request to the original repository (see "Pull Requests").

 

3. Hands-on Practice

Creating a Personal Repository

Now that you know how to work with GitHub, try creating your own repository:

  1. Create a new repository on GitHub (as explained earlier).
  2. Clone the repository to your local machine.
  3. Add some code or files to your local repository.
  4. Use git add, git commit, and git push to push your changes to GitHub.

Collaborative Project

Form small groups and set up a collaborative project using GitHub:

  1. One person creates a new repository on GitHub and adds the others as collaborators.
  2. Each group member clones the repository to their local machine.
  3. Work on different features or parts of the project in separate branches.
  4. Use pull requests to merge changes into the main branch.
  5. Discuss and resolve any merge conflicts that arise.

 

4. Common Git/GitHub Workflows

Feature Branch Workflow

In this workflow, each feature or bug fix is developed in a separate branch. Here's how it works:

  1. Create a New Branch:

git checkout -b feature-branch-name

  1. Work on the Feature: Make changes and commit them to the feature branch.
  2. Push the Branch:

git push origin feature-branch-name

  1. Submit a Pull Request: Once the feature is complete, submit a pull request to merge it into the main branch.

Fork and Pull Workflow

This workflow is commonly used in open-source projects:

  1. Fork the repository.
  2. Clone your fork to your local machine.
  3. Create a new branch for your changes.
  4. Push your changes to your fork.
  5. Submit a pull request to the original repository.

GitHub Issues and Projects

GitHub provides tools to track tasks, bugs, and project progress:

 

5. Best Practices

Commit Messages

Good commit messages are essential for maintaining a clear project history. Here are some tips:

Regular Commits

Commit your changes frequently. This helps you keep track of your progress and makes it easier to identify and revert specific changes if needed.

Branching Strategy

Use branches effectively to manage different features or versions of your project:

By following these best practices and using GitHub effectively, you'll be well-prepared to manage your final projects and collaborate successfully with your classmates.