Forking, cloning, and browsing

In this episode, we will look at an existing repository to understand how all the pieces work together. Along the way, we will make a copy (by forking and/or cloning) of the repository for us, which will be used for our own changes.

Objectives

  • See a real Git repository and understand what is inside of it.

  • Understand how version control allows advanced inspection of a repository.

  • See how Git allows multiple people to work on the same project at the same time.

  • See the big picture instead of remembering a bunch of commands.

GitHub, VS Code, or command line

We offer three different paths for this exercise:

  • GitHub (this is the one we will demonstrate)

  • VS Code (if you prefer to follow along using an editor)

  • Command line (for people comfortable with the command line)

Creating a copy of the repository by “forking” or “cloning”

A repository is a collection of files in one directory tracked by Git. A GitHub repository is GitHub’s copy, which adds things like access control, issue tracking, and discussions. Each GitHub repository is owned by a user or organization, who controls access.

First, we need to make our own copy of the exercise repository. This will become important later, when we make our own changes.

Illustration of forking a repository on GitHub

Illustration of forking a repository on GitHub.

Illustration of cloning a repository to your computer

Illustration of cloning a repository to your computer.

Illustration of cloning a forked repository to your computer

It is also possible to do this: to clone a forked repository to your computer.

At all times you should be aware of if you are looking at your repository or the upstream repository (original repository):

  • Your repository: https://github.com/USER/planets

  • Upstream repository: https://github.com/workshop-material/planets

How to create a fork

  1. Go to the repository view on GitHub: https://github.com/workshop-material/planets

  2. First, on GitHub, click the button that says “Fork”. It is towards the top-right of the screen.

  3. You should shortly be redirected to your copy of the repository USER/planets.

Instructor note

Before starting the exercise session show how to fork the repository to own account (above).

Exercise: Copy and browse an existing project

Work on this by yourself or in pairs.

Exercise preparation

In this case you will work on a fork.

You only need to open your own view, as described above. The browser URL should look like https://github.com/USER/planets, where USER is your GitHub username.

Exercise: Browsing an existing project (20 min)

Browse the example project and explore commits and branches, either on a fork or on a clone. Take notes and prepare questions. The hints are for the GitHub path in the browser.

  1. Browse the commit history: Are commit messages understandable? (Hint: “Commit history”, the timeline symbol, above the file list)

  2. Compare the commit history with the network graph (“Insights” -> “Network”). Can you find the branches?

  3. Try to find the history of commits for a single file, e.g. simulate.py. (Hint: “History” button in the file view)

  4. Which files include the word “position”? (Hint: the GitHub search on top of the repository view)

  5. In the simulate.py file, find out who modified the “gravitational constant” last and in which commit. (Hint: “Blame” view in the file view)

  6. Can you use this code yourself? Are you allowed to share modifications? (Hint: look for a license file)

The solution below goes over most of the answers, and you are encouraged to use it when the hints aren’t enough - this is by design.

Solution and walk-through

(1) Basic browsing

The most basic thing to look at is the history of commits.

  • This is visible from a button in the repository view. We see every change, when, and who has committed.

  • Every change has a unique identifier, such as 244c993. This can be used to identify both this change, and the whole project’s version as of that change.

  • Clicking on a change in the view shows more.

Click on the timeline symbol in the repository view:

Screenshot on GitHub of where to find the commit history

(2) Compare commit history with network graph

The commit history we saw above looks linear: one commit after another. But if we look at the network view, we see some branching and merging points. We’ll see how to do these later. This is another one of the basic Git views.

In a new browser tab, open the “Insights” tab, and click on “Network”. You can hover over the commit dots to see the person who committed and how they correspond with the commits in the other view:

Screenshot on GitHub of the network graph

(3) How can you browse the history of a single file?

We see the history for the whole repository, but we can also see it for a single file.

Navigate to the file view: Main page → simulate.py. Click the “History” button near the top right.

(4) Which files include the word “position”?

Version control makes it very easy to find all occurrences of a word or pattern. This is useful for things like finding where functions or variables are defined or used.

We go to the main file view. We click the Search magnifying class at the very top, type “position”, and click enter. We see every instance, including the context.

Searching in a forked repository will not work instantaneously!

It usually takes a few minutes before one can search for keywords in a forked repository since it first needs to build the search index the very first time we search. Start it, continue with other steps, then come back to this.

(5) Who modified a particular line last and when?

This is called the “annotate” or “blame” view. The name “blame” is very unfortunate, but it is the standard term for historical reasons for this functionality and it is not meant to blame anyone.

From a file view, change preview to “Blame” towards the top-left. To get the actual commit, click on the commit message next to the code line that you are interested in.

(6) Can you use this code yourself? Are you allowed to share modifications?

  • Look at the file LICENSE.

  • On GitHub, click on the file to see a nice summary of what we can do with this:

    Screenshot on GitHub summarizing license terms

Summary

  • Git allowed us to understand this simple project much better than we could, if it was just a few files on our own computer.

  • It was easy to share the project with the course.

  • By forking the repository, we created our own copy. This is important for the following, where we will make changes to our copy.