How to contribute changes to repositories that belong to others
In this episode we prepare you to suggest and contribute changes to repositories that belong to others. These might be open source projects that you use in your work.
We will see how Git and services like GitHub or GitLab can be used to suggest modification without having to ask for write access to the repository and accept modifications without having to grant write access to others.
Exercise
Exercise preparation
Maintainer (team lead):
Create an exercise repository by generating from a template using this template: https://github.com/coderefinery/template-forking-workflow-exercise called
forking-workflow-exercise
In this case we do not add collaborators to the repository (this is the point of this example).
Share the link to the newly created repository with your group.
Learners in exercise team: Fork the newly created repository (not the “coderefinery” one) and then clone your fork (if you wish to work locally).
The instructors are the maintainers. All watchers are collaborators.
We create(d) these the day before hopefully. Choose only one to work with:
Not recorded: https://github.com/cr-workshop-exercises/forking-workflow-exercise (this will not be shown on stream or recorded in our videos, but is be public on the internet until it is deleted)
Recorded: https://github.com/cr-workshop-exercises/forking-workflow-exercise-recorded (this will be shown on stream and recorded, your username and comments may appear in the recorded video on YouTube)
Here we don’t need your GitHub usernames because the point of this exercise is to show that we can collaborate without granting write permissions.
Learners following on their own: Fork one of these two repositories and then clone your fork to your computer (if you wish to work locally).
Help and discussion
Opening a pull request towards the upstream repository
We have learned in the previous episode that pull requests are always from branch to branch. But the branch can be in a different repository.
When you open a pull request in a fork, by default GitHub will suggest to direct it towards the default branch of the upstream repository.
This can be changed and it should always be verified, but in this case this is exactly what we want to do, from fork towards upstream:
Pull requests can be coupled with automated testing
We added an automated test here just for fun and so that you see that this is possible to do.
In this exercise, the test is silly. It will check whether the recipe contains both an ingredients and an instructions section.
In this example the test failed:
Click on the “Details” link to see the details of the failed test:
How can this be useful?
The project can define what kind of tests are expected to pass before a pull request can be merged.
The reviewer can see the results of the tests, without having to run them locally.
How does it work?
We added a GitHub Actions workflow to automatically run on each push or pull request towards the
main
branch.
What tests or steps can you image for your project to run automatically with each pull request?
How to update your fork with changes from upstream
This used to be difficult but now it is two mouse clicks.
Navigate to your fork and notice how GitHub tells you that your fork is behind. In my case, it is 9 commits behind upstream. To fix this, click on “Sync fork” and then “Update branch”:
After the update my “branch is up to date” with the upstream repository:
How to approach other people’s repositories with ideas, changes, and requests
Contributing very minor changes
Clone or fork+clone repository
Create a branch
Commit and push change
Open a pull request or merge request
If you observe an issue and have an idea how to fix it
Open an issue in the repository you wish to contribute to
Describe the problem
If you have a suggestion on how to fix it, describe your suggestion
Possibly discuss and get feedback
If you are working on the fix, indicate it in the issue so that others know that somebody is working on it and who is working on it
Submit your fix as pull request or merge request which references/closes the issue
Motivation
Inform others about an observed problem
Make it clear whether this issue is up for grabs or already being worked on
If you have an idea for a new feature
Open an issue in the repository you wish to contribute to
In the issue, write a short proposal for your suggested change or new feature
Motivate why and how you wish to do this
Also indicate where you are unsure and where you would like feedback
Discuss and get feedback before you code
Once you start coding, indicate that you are working on it
Once you are done, submit your new feature as pull request or merge request which references/closes the issue/proposal
Motivation
Get agreement and feedback before writing 5000 lines of code which might be rejected
If we later wonder why something was done, we have the issue/proposal as reference and can read up on the reasoning behind a code change
Summary
This forking workflow lets you propose changes to repositories for which you have no access.
This is the way that much modern open-source software works.
You can now contribute to any project you can view.