Session 6: Integration with GitHub / GitLab

Track, organize and share your work: An introduction to Git for research

Course at General Psychology Lab at the University of Hamburg

Slides | Source

License: CC BY 4.0 DOI

13:30

1 Last session: Branches

Last session: Learning objectives

After the last session, you should now be able to answer the following questions / do the following:

đź’ˇ You understand the purpose and benefits of using branches in Git.
đź’ˇ You can create and switch between branches.
đź’ˇ You can merge branches and resolve merge conflicts.
đź’ˇ You can name at least three best practices when working with branches.

Last session: recipes project

At the end of this session, you should have accomplished the following:

  1. You created a new branch and merged changes to your default branch.
  2. 🚀 Optional: You created and resolved a merge conflict.

Please keep the recipes folder! We will continue to use it in the following sessions.

2 This session: Integration with GitHub / GitLab

Integration with GitHub / GitLab

Image from Techdobz

This session: Pull Requests

Link to Pull Request

Reading

https://lennartwittkuhn.com/version-control-book/chapters/remotes-intro.html

Learning objectives

đź’ˇ You can create a remote repository.
đź’ˇ You can connect your local Git to a remote repository service.
đź’ˇ You can pull and push changes to and from a remote repository.
đź’ˇ You can clone a repository from a remote repository service.

Cheatsheet

Command Description
git remote Manages remote repositories
git clone Creates a local copy of a repository
git pull Fetches and merges the latest changes from a remote repository into the current branch
git fetch Updates remote tracking branches
git push Uploads local commits to a remote repository

Tasks

In this session, you will work on the following tasks:

  1. Reading: Read the chapter “Remotes - Introduction” in the Version Control Book.
  2. Implementation: Try out the commands in the chapter.
  3. Exercises: Work on the exercises for the recipes project.
  4. Quiz: Test your knowledge with the quiz.

As always:

  1. Try out the commands of this session and play around with them.
  2. Check whether you have achieved the learning objectives.
  3. Ask questions!
  4. Let’s git started!

recipes project

At the end of this session, you should have accomplished the following:

  1. You connected your GitHub account to your local Git.
  2. You created a new GitHub repository and uploaded (i.e., “pushed”) your recipes project.
  3. You collaborated on a shared project by adding and committing changes to a partner’s repository.

Optional:

  1. You cleaned up your project by deleting unnecessary files and branches.
  2. You created a short README.md file in your repository.

Please keep the recipes folder! We will continue to use it in the following sessions.

Exercises 1

Connect to remote repositories using SSH

  1. Generate an SSH key.
  2. Copy the SSH key to your clipboard.
  3. Add the SSH key to the remote repository (for example, GitHub or GitLab).

Upload your local repository to a remote repository

  1. Create an empty repository on the remote repository hosting platform, for example GitHub or GitLab. Make sure to not initialize the repository with any files!
  2. If needed, navigate to the recipes repository using the command line.
  3. Set the remote URL of your local repository to your remote repository.
  4. Push the changes on your main branch to your remote repository.

Exercises 2

“Private” collaboration with pull requests (using GitHub Flow)

  1. Add your exercise partner as a collaborator to your recipes repository.
  2. Clone your partner’s repository.
  3. Create a new branch in your collaborator’s repository.
  4. Add a recipe to your collaborator’s recipes.txt file.
  5. Add and commit the changes.
  6. Push the changes on the new branch to the remote repository.
  7. Create a Pull / Merge Request.
  8. Review the PR your collaborator made in your repository.
  9. 🚀 Optional: Add additional changes on the branch pushed by your collaborator.
  10. Merge the PR into your repository.

🚀 Bonus exercises

Add a README.md

  1. Find the option to create a new file on your remote repository in the browser.
  2. Name the file README.md, add a brief description, and provide a commit message.
  3. 🚀 Optional: Play around with Markdown syntax.
  4. Save the README.md file to the repository.
  5. Pull the changes to your local repository.

Clone and sync your repository

  1. Move to a location on your computer where you want to clone a repository.
  2. Clone your remote repository to a different location on your computer.
  3. Stage and commit changes in the new location (consider using a new branch).
  4. Push these new changes to GitHub.
  5. Pull the changes to the repository in the original location.
  6. Delete your newly cloned repository.

Solutions

Connect to remote repositories using SSH

Code
#!/bin/bash

ssh-keygen -t ed25519 -C "your_email@example.com" # <1>
cat ~/.ssh/id_ed25519.pub # <2>
# Copy the SSh key to your account # <3>
  1. In the command line, create a new SSH key. Make sure to change the example email to your email address. Optionally, provide a passphrase.
  2. Copy the SSH key to your clipboard. Here, we use cat to print the contents of the SSH key to the command line. Copy the contents displayed in the Terminal to your clipboard.
  3. Add the SSH key to your remote repository account.

Solutions

Upload your local repository to a remote repository

Code
#!/bin/bash

# create an empty remote repository # <1>
cd recipes # <2>
git remote add origin https://github.com/your-username/your-repo-name.git # <3>
git push -u origin main # <4>
  1. To create an empty repository on GitHub: (1) Go to GitHub and click the + icon in the upper-right corner, then select New repository. (2) Name your repository. (3) Do not select Initialize this repository with a README. (4) Click Create repository.
  2. Optional: Navigate into the recipes repository using cd (or a similar path).
  3. Set the remote URL of the local repository to the repository using git remote add origin <URL>. Remember to use the correct <URL> depending on whether you authentication method (typically SSH or PAT).
  4. Push the changes on the main branch to the remote repository using git push -u origin main.

Solutions

“Private” collaboration with pull requests (using GitHub Flow)

Code
#!/bin/bash

# Add your exercise partner as a collaborator to your recipes repository # <1>
cd ~ # <2>
git clone https://github.com/partner-username/partner-repo-name.git  # <3>
git checkout -b new-branch-name # <4>
echo "New Recipe" >> recipes.txt # <5>
git add recipes.txt  # <6>
git commit -m "Add new recipe to recipes.txt" # <6>
git push origin new-branch-name # <7>
# Create a Pull / Merge Request. # <8>
# Review the PR your partner made in your repository. # <9>
# Merge the PR into your repository. # <10>
  1. Add your exercise partner as a collaborator to your recipes repository: (1) Go to your repository on GitHub. (2) Click on Settings. (2) Click on Manage access in the left sidebar. (3) Click Invite a collaborator and enter your partner’s GitHub username.
  2. Move to the location on your computer where you would like to clone your partner’s repository into, using cd in the command line. Here, we cd into the user’s home directory (~).
  3. Clone your partner’s repository using git clone. Make sure that you not cloning into an existing repository.
  4. Create a new branch in your partner’s repository.
  5. Add a recipe to your partner’s recipes.txt file.
  6. Add and commit the changes using a descriptive commit message.
  7. Push the changes on the new branch to GitHub.
  8. Create a Pull Request: (1) Go to your partner’s repository on GitHub. (2) Click Compare & pull request for your branch. (3) Provide a title and description, then click Create pull request.
  9. Review the PR your partner made in your repository: (19) Go to your repository on GitHub. (2) Click on the Pull requests tab. (3) Click on the PR made by your partner. (4) Review the changes and provide feedback.
  10. Merge the PR into your repository: (1) After reviewing, click the green Merge pull request button. (2) Click Confirm merge.

Solutions

Add a README.md

  1. In your browser, go to your remote repository (for example, on GitHub), click Add file, and select Create new file.
  2. Name the file README.md. Add a brief description of your project. Provide a descriptive commit message at the bottom.
  3. Play around with Markdown syntax
  4. Click the green Commit new file button to save the README.md file to the repository.
  5. Use git pull origin main to pull the changes to your local repository.

Solutions

Clone and sync your repository

Code
#!/bin/bash

cd /new/location/for/repo # <1>
git clone https://github.com/your-username/your-repo-name.git /new/location/for/repo # <2>
git checkout -b new-branch # <3>
echo "New Recipe" >> recipes.txt # <3>
git add recipes.txt  # <3>
git commit -m "Add new recipe to recipes.txt" # <3>
git push -u origin new-branch # <4>
cd /original/location/for/repo # <5>
git fetch # <5>
git switch new-branch # <5>
rm -rf /new/location/for/repo # <6>
  1. Move to the location on your computer where you would like to clone your own repository into, using cd in the command line.
  2. Clone your repository from GitHub to a different location on your computer.
  3. Stage and commit changes in the new location (consider using a new branch).
  4. Push the new changes to GitHub.
  5. Fetch these new changes to the repository in the original location.
  6. Delete your newly cloned repository.

3 Appendix

Cloning

Cloning

  • Remember to clone a repo in a sensible location (not in your own repository)
  • To rename the cloned repo you can use: git clone <URL> new-folder-name