GitHub essentials · Lesson 3 of 4
Module 1 · Python basics · GitHub essentials
Try changes safely on a branch, propose and review them in a pull request, resolve a merge conflict, track work with issues, and contribute to someone else's project through a fork.
Science is collaborative, and so is code: a lab shares an analysis repository, a supervisor reviews a student's script, a stranger fixes a bug in a tool you use. GitHub is built for this. The three ideas in this lesson, branches, pull requests and issues, are how almost every project on GitHub is run, from a two-person lab to the biggest open-source tools.
Branches: try changes safely
A branch is a separate line of history. You make a branch, change things on it as much as you like, and main stays exactly as it was. When the work is good, you bring the branch back into main (merge); if it was a dead end, you delete the branch and nothing is lost.
git switch -c add-qc-plot # make a new branch and move onto it # ... edit files ... git add analysis/qc_plot.py git commit -m "Add quality-control plot" git push -u origin add-qc-plot # send the branch to GitHub git switch main # back to main: your new file is not here
- git switch -c name creates a branch and moves onto it. Name branches after what they do, in lower case with hyphens.
- -u on the first push links your branch to the one on GitHub, so later you can just type git push.
- git branch lists your branches; the one with a star is the one you are on.
- In GitHub Desktop, use the Current branch menu, then New branch.
Pull requests: propose, review, merge
A pull request (PR) asks to merge one branch into another, usually into main. It shows every change as a diff, and it is where people discuss and review the change before it is accepted. Even when you work alone, a pull request is a good habit: it gives you a page that explains why a change was made.
- After you push a branch, GitHub shows a yellow banner on the repository page. Choose Compare & pull request. (Or open the Pull requests tab and choose New pull request.)
- Check that base is main and compare is your branch.
- Give it a clear title and a description: what you changed, why, and how to check it. Add a screenshot of a new figure if there is one.
- Choose Create pull request.
- Reviewers look at the Files changed tab, comment on individual lines, and approve or ask for changes. To answer a comment, push more commits to the same branch: the pull request updates itself.
- When it is ready, choose Merge pull request, then Confirm merge. GitHub offers to delete the branch: you can, the work is now in main.
- On your computer, git switch main and git pull to bring the merged work down.
Reviewing well
A good review is specific and kind: point to the line, say what you see, and suggest an alternative. This filter drops samples with zero counts; is that intended for the controls? helps far more than this looks wrong.
In science, reviewing code is part of checking results. A second pair of eyes on a sample sheet or a statistical model catches the kind of mistake that ends up in a retraction.
Merge conflicts
A conflict happens when two branches change the same lines of the same file. Git cannot know which version is right, so it asks you. The file then contains both versions between markers:
<<<<<<< HEAD Samples were filtered at 10 reads per gene. ======= Samples were filtered at 20 reads per gene. >>>>>>> add-qc-plot
- The part between <<<<<<< HEAD and ======= is your current branch; the part after it, up to >>>>>>>, is the incoming branch.
- Edit the file so it says what is actually right: keep one version, combine them or write something new, and delete all three marker lines.
- Save, then git add notes.md and git commit to finish the merge.
On GitHub, simple conflicts in a pull request can be fixed in the browser with Resolve conflicts. Conflicts are normal and not a sign that anything is broken; pulling often keeps them small.
Issues: a to-do list everyone can see
The Issues tab is where a project tracks bugs, questions and planned work. Choose New issue, give it a clear title, and describe what you expected, what happened, and how to reproduce it. Issues can be assigned to people, labelled (for example bug or documentation) and discussed.
- Write Fixes #12 in a pull request description and GitHub closes issue 12 automatically when the pull request is merged.
- Before you report a problem with someone's tool, search its open and closed issues: the answer is often already there.
- A good bug report includes the exact command, the full error message, your operating system and the tool version.
Forks: work on someone else's project
You cannot push to a repository you do not own. To change one, make a fork: your own copy on GitHub, made with the Fork button at the top of the repository page. You clone your fork, make a branch, push to the fork, and open a pull request back to the original project. That is how people contribute fixes to open-source tools.
- To bring later changes from the original into your fork, use Sync fork › Update branch on your fork's page.
- Small first contributions are welcome in most projects: a typo in the documentation, a clearer error message, an extra example. Read the project's CONTRIBUTING.md file first if there is one.
Inviting collaborators
For a shared project, such as a lab repository, the owner adds people under Settings › Collaborators (on a personal account) and they accept the invitation by email. Collaborators can push directly. For a larger group, a free organisation account lets a lab own repositories together, with teams and permissions.
Branch, pull request, merge
This lesson
In lsma-practice, create a branch called improve-readme, add a section to the README describing how to run your code, push the branch, open a pull request, read your own diff in Files changed, then merge it.
Check your answer
The pull request page shows one or more commits and your README diff in green. After merging, the main page shows the new section, the pull request is marked Merged in purple, and git pull on main brings the change to your computer.
Make and fix a conflict
This lesson
On main, change the first line of notes.md and commit. On a new branch made from the commit before, change the same line differently and commit. Merge the branch into main with git merge branch-name and resolve the conflict.
Check your answer
Git reports CONFLICT (content): Merge conflict in notes.md. The file shows both lines between the markers. After you keep the right text, delete the markers, add and commit, git log --oneline --graph shows the two lines of history joining in a merge commit.
Open a useful issue
This lesson
Open an issue in your own repository describing a small improvement you would like to make, label it enhancement, then fix it in a pull request whose description says Closes #1 (use your issue's number).
Check your answer
When the pull request is merged, the issue closes by itself and links to the pull request that fixed it: a permanent record of what was done and why.
Check your understanding
1. Why make a branch before changing an analysis?
2. What does a pull request do?
3. You want to fix a typo in a tool's documentation, but you cannot push to its repository. What do you do?
Up next
GitHub 4
GitHub: sharing science