Learn / Lessons · GitHub

GitHub intermediate

How do several people change one project without treading on each other? Branches and pull requests. A branch is a separate line of work.

Chapters

  1. 0:00Introduction
  2. 0:08A safe lane: What's a branch?
  3. 0:18Make one: A new branch
  4. 0:26Ask to merge: The pull request
  5. 0:37Good reviews: Make it easy
  6. 0:47Keep track: Use issues
  7. 1:00Stay current: Update your branch
  8. 1:07When lines clash: Merge conflicts
  9. 1:20Not your repo?: Branch or fork
  10. 1:30Interrupted?: Set work aside
  11. 1:39The merge button: Three ways to merge
  12. 1:52Wrap up
Read the transcript

Introduction

How do several people change one project without treading on each other? Branches and pull requests.

A safe lane: What's a branch?

A branch is a separate line of work. The main branch holds the shared version. Your branch holds your change while you build it. Main stays steady in the meantime.

Make one: A new branch

Create a branch and switch to it with git switch. Commit your work there as usual, then push the branch to GitHub.

git switch -c fix-login
git commit -am "Fix the login button"
git push -u origin fix-login

Ask to merge: The pull request

On GitHub, open a pull request. It shows exactly what your branch changes. Others read it and comment. When it's approved, merge it into main.

Good reviews: Make it easy

Help your reviewer. Keep the pull request small. Describe what changed, and how you tested it. New commits pushed to the branch join the same pull request.

Keep track: Use issues

Issues track bugs and ideas. Label them, and assign someone. Write closes, then the issue number, in a pull request description, and merging into the default branch closes the issue.

Stay current: Update your branch

Main moves while you work. Fetch the latest changes, and merge main into your branch.

git fetch origin
git switch fix-login
git merge origin/main

When lines clash: Merge conflicts

A conflict means two changes touched the same lines. Git marks both versions inside the file. Edit it to the result you want, and remove the markers. Then add the file, commit, and test.

Not your repo?: Branch or fork

If you can write to the repository, use a branch. If you can't, fork it: your own copy on GitHub. Then open a pull request from your fork.

Interrupted?: Set work aside

Need to switch jobs with half finished work? Git stash puts your changes safely to one side. Git stash pop brings them back.

git stash
git switch main
git switch fix-login
git stash pop

The merge button: Three ways to merge

GitHub offers up to three ways to merge a pull request. A merge commit keeps every commit. Squash and merge turns them into one. Rebase and merge adds them one by one, with no merge commit.

Wrap up

Branch, push, review, merge. That's teamwork on GitHub.

Sources

More lessons