Tutorial 12 - GitHub

View notebook on Github Open In Collab

Overview

Git is a distributed version control system (DVCS) that allows developers to track changes in their code over time. It was created by Linus Torvalds in 2005 and is widely used for managing source code for software projects. Git enables multiple developers to work on a project simultaneously and independently, and it tracks changes made by each contributor. It provides features such as branching, merging, and history tracking.

With Git, each developer has a local copy of the entire project history. Developers can work offline, commit changes locally, and then synchronize with a central repository later.

Key features of Git include:

  • Manage projects with repositories

  • Clone a project to work on a local copy

  • Control and track changes with staging and committing

  • Branch and merge for working on different parts and versions of a project

  • Pull the latest version of the project to a local copy

  • Push local updates to the main project

A repository is typically used to organize a single project. Repositories can contain folders and files, images, videos, spreadsheets, and data sets. I.e., they can contain anything your project needs. Repositories typically include a README file that provides information about the project. README files are written in plain text Markdown language.

GitHub is a web-based platform that provides hosting for Git repositories. It offers a graphical interface for managing Git repositories, collaboration features, and additional tools for project management. GitHub allows multiple developers to collaborate on a project, track issues, and manage pull requests.

Key features of GitHub are:

  • Repository hosting: GitHub provides a place to store and manage Git repositories.

  • Collaboration: Developers can collaborate on projects by forking repositories, making changes, and submitting pull requests.

  • Issue Tracking: GitHub includes an issue-tracking system for managing bugs, feature requests, and other tasks.

  • Pull Requests: Developers can propose changes to a project by submitting pull requests, which can be reviewed and merged by project maintainers.

  • Web-based interface: GitHub provides a user-friendly web interface for interacting with Git repositories.

To summarize, Git serves as the version control system enabling developers to monitor code changes, whereas GitHub functions as a web-based platform offering hosting for Git repositories along with extra collaboration tools.

Working with GitHub

Step 1: Sign up for a GitHub account: link

Step 2: Create a Repository on GitHub by clicking on the “New repository” button shown below in the upper right corner. Then, fill in the relevant details. You can set the repository as “Public” or “Private”. You also have options to add a README file, or choose a license for your repository. Click the “Create Repository” button to finish the creation process.

2d0da2af6e7140929ad5c51398246099

794deab2182a4377872d6e0527fc8318

Step 3: Create a branch. By default, a repository comes with a primary branch called "main", which serves as the authoritative branch. We have the option to establish additional branches stemming from the main branch within the repository. The creation of branches allows for the simultaneous existence of various versions of a project. This proves beneficial when incorporating new features into a project without altering the primary source code. Changes made in different branches remain isolated from the main branch until a later stage when merging is addressed. Branches provide a space for experimentation and editing before finalizing changes and committing them to the main branch.

When branching off the main branch, we essentially create a duplicate or snapshot of the main branch as it existed at that specific moment. If someone else modifies the main branch while you are developing your branch, you have the option to incorporate those updates.

To create a new Branch, type in a descriptive name in the field below, and click Create Branch. In the following figure, the name of the new branch name is test2.

5aecab149391401b80eab6851b445418

An example of a flow diagram for making changes to a branch is depicted next, showing the 'main' branch, a new branch called 'feature', and the path of the 'feature' branch before it is merged into the 'main' branch.

a096672baf5448e6861a2c0865caa6d9

Making and Committing Changes

We can make and save changes to the files in a repository. On GitHub, the saved changes are called commits. Each commit has an associated commit message, which is a description explaining why a particular change was made. Commit messages capture the history of the changes, so that other contributors can understand what you have done and why.

For instance, to edit the README.md file, we can follow these steps:

  1. Under the branch we created, click the README.md file.

  2. To edit the file, click on the Edit (pencil) button.

  3. In the editor, write some text about the repository.

  4. Click “Commit Changes….”

  5. In the “Commit Changes” box, write a commit message that describes the changes.

  6. Click “Commit Changes”.

Opening a pull request

Pull requests are the main form of collaboration on GitHub. When you open a pull request, you are proposing your changes and requesting that someone review and pull in your contribution and merge them into their branch. Pull requests show the differences of the content from both branches. The changes, additions, and subtractions are shown in different colors.

As soon as you make a commit, you can open a pull request and start a discussion, even before the code is finished. By using GitHub’s @mention feature in the pull request message, you can ask for feedback from specific people or teams, whether they are down the hall or 10 time zones away.

You can even open pull requests in your own repository and merge them yourself. It’s a great way to learn the GitHub flow before working on larger projects.

To create a pull request:

  1. Click the Pull Requests tab of the repository.

  2. Click New pull request.

  3. In the Example Comparisons box, select the branch you made to compare with the main (the original).

  4. Look over your changes in the diffs on the Compare page, make sure they are what you want to submit.

  5. Click Create Pull Request.

  6. Give your pull request a title and write a brief description of your changes. You can include emojis and drag and drop images and gifs.

  7. Optionally, to the right of your title and description, click the button next to Reviewers, Assignees, Labels, Projects, or Milestone to add any of these options to your pull request. You do not need to add any, but these options offer different ways to collaborate using pull requests.

  8. Click Create Pull Request.

Merging a Pull Request

In this final step, you will merge your branch into the main branch. After you merge your pull request, the changes on your branch will be incorporated into main.

Sometimes, a pull request may introduce changes to code that conflict with the existing code on main. If there are any conflicts, GitHub will alert you about the conflicting code and prevent merging until the conflicts are resolved. You can make a commit that resolves the conflicts or use comments in the pull request to discuss the conflicts with your team members.

To merge your branch into the main branch:

  1. At the bottom of the pull request, click Merge Pull Request to merge the changes into main.

  2. Click Confirm Merge. You will receive a message that the request was successfully merged and the request was closed.

  3. Click Delete Branch. Now that your pull request is merged and your changes are on main, you can safely delete the readme-edits branch. If you want to make more changes to your project, you can always create a new branch and repeat this process.

Working with a Local Git Repository

  1. Download and install Git from link

  2. Specify your Git information:

    git config --global user.name "username"
    git config --global user.email "youremailaddress
    
  1. Create a local Git folder:

    mkdir myproject
    
  1. Initialize Git:

    git init
    

Pull Request

  1. Pull from Github with “Fetch” and “Merge”:

    git fetch origin
    git merge origin/master
    
  2. Simple Pull request (combination of Fetch and Merge):

    git pull origin
    

Push your code from a Local folder to Github:

git push origin

References

  1. Hello world. GitHub Docs. (n.d.). https://docs.github.com/en/get-started/quickstart/hello-world

    1. F., Campion. An intro to Git and GitHub for beginners (tutorial). HubSpot Careers. https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners

  2. Ajibola.Segunemmanuel. (2022, September 27). How to use Git and GitHub – Introduction for Beginners. freeCodeCamp.org. https://www.freecodecamp.org/news/introduction-to-git-and-github/

  3. Git tutorial. (n.d.). https://www.w3schools.com/git/

BACK TO TOP