List of exercises
Full list
This is a list of all exercises and solutions in this lesson, mainly as a reference for helpers and instructors. This list is automatically generated from all of the other pages in the lesson. Any single teaching event will probably cover only a subset of these, depending on their interests.
Collaborating within the same repository
Practicing code review
Exercise: Practicing code review (25 min)
Technical requirements:
If you create the commits locally: Being able to authenticate to GitHub
What is familiar from the previous workshop days:
Creating a branch (previous lesson)
Committing a change on the new branch (previous lesson)
Opening and merging pull requests (previous lesson)
What will be new in this exercise:
As a reviewer, we will learn how to ask for changes in a pull request.
As a reviewer, we will learn how to suggest a change in a pull request.
As a submitter, we will learn how to modify a pull request without closing the incomplete one and opening a new one.
Exercise tasks:
Create a new branch and one or few commits: in these improve something but also deliberately introduce a typo and also a larger mistake which we will want to fix during the code review.
Open a pull request towards the main branch.
As a reviewer to somebody else’s pull request, ask for an improvement and also directly suggest a change for the small typo. (Hint: suggestions are possible through the GitHub web interface, view of a pull request, “Files changed” view, after selecting some lines. Look for the “±” button.)
As the submitter, learn how to accept the suggested change. (Hint: GitHub web interface, “Files Changed” view.)
As the submitter, improve the pull request without having to close and open a new one: by adding a new commit to the same branch. (Hint: push to the branch again.)
Once the changes are addressed, merge the pull request.
How to contribute changes to repositories that belong to others
Recovery
But if you want to keep your local changes, you can change the remote URL to point to your fork.
Check where your remote points to with git remote --verbose
.
It should look like this (replace your-user
with your GitHub username):
$ git remote --verbose
origin git@github.com:your-user/forking-workflow-exercise.git (fetch)
origin git@github.com:your-user/forking-workflow-exercise.git (push)
It should not look like this:
$ git remote --verbose
origin git@github.com:cr-workshop-exercises/forking-workflow-exercise.git (fetch)
origin git@github.com:cr-workshop-exercises/forking-workflow-exercise.git (push)
In this case you can adjust “origin” to point to your fork with:
$ git remote set-url origin git@github.com:your-user/forking-workflow-exercise.git
Collaborating within the same repository
Centralized-1: Clone a repository, add a file, push changes as a branch, and create a pull request
Before we start with the exercise, instructor points to the preparation (above).
Then work on steps A-H.
There are also optional exercises.
Before and after each action you take, run the following informational commands:
git graph
- almost every timegit status
- when you modify files
Centralized-2: Merge the pull requests (together)
We do step 2A and 2B together (instructor demonstrates, and everybody follows along in their repositories).
(optional) Centralized-3: Cross-referencing issues using “#N”
We will submit another change by a pull request but this time we will first create an issue.
Open an issue on GitHub and describe your idea for a change. This gives others the chance to give feedback/suggestions. Note the issue number, you will need it in step 3.
Create a new branch and switch to it.
On the new branch create a commit and in the commit message write what you did, but also add that this “closes #N” (replace N by the actual issue number from step 1).
Push the branch and open a new pull request. If you forgot to refer to the issue number in step 3, you can still refer to it in the pull request form (add a “closes #N” to the title or description).
Note how now commits, pull requests, and issues can be cross-referenced by including
#N
.Notice how after the pull request is merged, the issue gets automatically closed. This only happens for certain keywords like
closes
orfix
.Discuss the value of cross-referencing them and of auto-closing issues with commits or pull requests.
See also the GitHub documentation for more examples.
(optional) Centralized-4: Why did we create a feature branch “yourname-somefeature”? (exercise/discussion)
Pushing directly to the main branch is perfectly fine for simple personal projects - the pull-request workflows covered here are for larger projects or for collaborative development. Guidelines for simpler workflows are given in the how much Git is necessary? episode of the git-intro lesson.
In collaborative development, whenever we update our repository we create a new branch and create a pull-request. Let’s now imagine that everyone in your group (or one person on two different clones) makes a new change (create a new file) but without creating a new branch.
You all create a new file in the main branch, stage and commit your change locally.
Try to push the change to the upstream repository:
$ git push origin main
You probably see something like this:
$ git push To https://github.com/user/repo.git ! [rejected] main -> main (non-fast-forward) error: failed to push some refs to 'https://github.com/user/repo.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details.
The push only worked for one participant (one clone).
Discuss why push for everybody else in this group was rejected?
Solution
The push for everyone except one person fails because they are missing one commit in their local repository that exists on the remote. They will first need to pull the remote changes before pushing their own, which will usually result in a merge commit.
Forking workflow
Distributed-1: Fork a repository and create a pull request
As an example we will collaboratively develop a cookbook for taco recipes, inspired by tacofancy.
Objectives:
Learn how to fork, modify the fork, and open a pull request towards the central repository.
Learn how to update your fork with changes that others have already made to the forked repository.
Exercise:
Maintainer prepares an exercise repository (see above; this will take 5-10 minutes).
Learners work on steps A-F (15-20 minutes).
There are two optional steps after step E for those who want more.
After step E you take a break or help others. Please ask questions both during group work and in the collaborative document.
We will review the pull requests together and then update forks.
(optional) Distributed-2: Send a conflicting pull request
If you complete parts A-E much earlier than others, try to open another pull request where you anticipate a conflict with your first pull request.
(optional) Distributed-3: Making changes to your pull request after it has been opened.
You can do that by pushing additional commits to the same branch where you opened the pull request from. Observe how they end up added to your pull request.
(optional) Distributed-4: Squash merge a pull request
If you complete this exercise much earlier than others, create a new pull request with two or more commits.
Then, when reviewing the change as maintainer, accept these with “Squash and merge” and later compare the source and target repositories/branches how they differ after the small commits got squashed into one.