Advanced git

Block 2.1: Collaboration & Pull Requests

Jan Simson

What is GitHub?

  • GitHub is a website to host git repositories
  • By having your git repository online, you can easily collaborate with others
  • There are many other similar, alternative websites
    • Codeberg 🇩🇪
    • GitLab
    • Bitbucket
    • … (and many more)
  • Together they contain the majority of the world’s software

Why use a Website?

  • Backup of your project
  • Collaboration
    • Not just with people you know
  • Free and Open-Source Software (FOSS)
  • Visibility

Remotes

Background: https://unsplash.com/photos/EOQhsfFBhRk

Remotes

  • Link to a remote repository on a hosting website
  • There can be more than one remote, but it is uncommon
  • Just like your repository, the remote repository also has branches
  • The default name for a remote is origin
    • When you clone a repository, this remote is already linked

Adding a Remote

When you want to add an existing repository to github, you will have to add the remote yourself

git remote add <remote name> <remote URL>


For example:

git remote add origin git@github.com:jansim/advanced-git.git


To learn about all possible commands for remotes use -h

git remote -h

Syncing Changes: Overview

You first pull the latest changes from the remote and then push your own changes

Getting Changes: git pull

  • You can retrieve the latest changes from github by running git pull
  • This fetches the latest changes and merges them into your current branch
  • Typically your local branch is set up to track a remote branch with the same name
    • If not, you get a warning from git
# Pull from the tracked remote branch
git pull

# Pull from a specific branch
git pull <remote>/<branch>

Getting Changes: git pull

  • This fetches the latest changes and merges them into your current branch
    • git pull really does two things here
  • You can also run git fetch to fetch the latest changes and then git merge <remote>/branch to merge them
  • git fetch will always fetch the latest changes without doing anything with them (which can be useful)

Pushing Your Changes: git push

  • You can send your changes to github by running git push
  • This only works if there are no new changes at the remote
    • If there have been changes since your last pull you need to pull again
# Push to the tracked remote branch
git push

# Push to a specific branch
git push <remote>/<branch>

Practical: Working with remotes

  1. Create an GitHub repository for your git-example repo
  2. Link and push your repository to GitHub
  3. Use git remote -h to learn about possible commands
  4. Create a second GitHub repository (git-example-2)
  5. Add a second remote to your local repository and push to it
  6. Remove one of the two remotes again and delete the corresponding GitHub repo
  • When could this be useful?

Pull / Merge Requests

Pull / Merge Requests (PRs / MRs)

  • When collaborating with other people, it’s often good to review each other’s changes
  • This is easiest done by using pull or merge requests
    • You request for someone to pull your changes
  • Terminology
    • GitHub: Pull Requests
    • GitLab: Merge Requests

Pull requests: Overview

Overview of the pull requests page in a GitHub repository

Pull requests: Detailed View

View of a single pull request on GitHub

Why Issues & Pull Requests are Necessary

Right now, the VSCode GitHub repository has >5000 issues and almost 400 PRs

When Should you start using Issues & PRs?

  • Maybe your project doesn’t need these features yet
  • Often it makes sense to use PR even when collaborating with just a few people (or by yourself!)
    • To avoid merge conflicts
    • To run automatic checks
    • Also for reviewing each others changes
  • Many companies require code reviews and using PRs

Questions?

Using Pull Requests by Yourself

  1. Create a new issue in your GitHub repository
  2. Create a new branch feature/vegan
    • Make sure to also checkout that branch, you can check whether you’re on it with git status
  3. Add a commit (with a new vegan recipe) on that new branch
    • Make sure to use the number of the commit (#1) in the commit message
  4. Push the branch to GitHub
  5. Merge the changes by creating a Pull Request

End of Section 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Section]