Advanced git

Block 2.1: Collaboration & Merge Requests

Jan Simson

What is GitLab?

  • GitLab is a FOSS project to host your own website for git repositories
  • GitLab.com is their SaaS offering
  • By having your git repository online, you can easily collaborate with others
  • There are many other similar, alternative websites
    • Codeberg 🇩🇪
    • GitHub
    • 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

Connecting git and GitLab

Authenticating with GitLab.com 🗝️

  • We will use a guide to set up the authentication with GitHub
  • Please go to the website and follow the steps to authenticate with GitHub
  • A PDF version of the guide can be found here

https://simson.io/advanced-git/output/resources/ssh/how-to-setup-gitlab-ssh.html

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/intro-to-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>

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

merge requests: Overview

Overview of the merge requests page in a GitLab repository

merge requests: Detailed View

View of a single merge request on GitLab

Why Issues & merge Requests are Necessary

Overview of the issues page of the gitlab repository.

When Should you start using Issues & MRs?

  • Maybe your project doesn’t need these features yet
  • Often it makes sense to use PR / MRs even when collaborating with just a few people
    • To avoid merge conflicts
    • Also for reviewing each others changes
  • Many companies require code reviews and using MRs

Questions?

Practical: Collaborating via GitLab

  1. Fork the Website Template Repository: https://gitlab.com/jansim/website-template
  2. Clone your forked repository
  3. Add an alumni page by following the instructions in docs/alumni/index.md
  4. Push your changes to GitLab and create a MR to add yourself as an alumni

End of Section 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Section]