After the last session, you should now be able to answer the following questions / do the following:
đź’ˇ You can initialize a Git repository. đź’ˇ You can check the status of a Git repository. đź’ˇ You understand the difference between the staging area and a commit. đź’ˇ You can stage and commit changes. đź’ˇ You understand the difference between a commit message and a description.
2 This session: Git: Remotes
Objectives
đź’ˇ You can create a remote repository. đź’ˇ You can connect your local Git repository to a remote repository service like GitHub or GitLab. đź’ˇ You can pull and push changes to and from a remote repository. đź’ˇ You can clone a repository from a remote repository.
Add the SSH key to the remote repository (for example, GitHub or GitLab).
Upload your local repository to a remote repository
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!
If needed, navigate to your project repository using the command line.
Set the remote URL of your local repository to your remote repository.
Push the changes on your default branch (main or master) to your remote repository.
Exercises (2)
Collaboration Option 1 (Basic)
Private collaboration on default branch (main or master)
Add your exercise partner as a collaborator to your project repository on GitHub.
Clone your partner’s repository.
Add a new change to your collaborator’s project file (if you are unsure, where to add the entry, ask your collaborator!)
Add and commit the changes.
Push the changes to the remote repository.
Pull your partner’s changes into your repository.
Exercises (3)
Collaboration Option 2 (Advanced)
Private collaboration with pull requests (using GitHub Flow)
Add your exercise partner as a collaborator to your project repository on GitHub.
Clone your partner’s repository.
Create a new branch in your collaborator’s repository.
Add a new entry to your collaborator’s project file (e.g., .txt or .qmd (if you are unsure, where to add the entry, ask your collaborator!)
Add and commit the changes.
Push the changes on the new branch to the remote repository.
Create a Pull Request (on GitLab: Merge Request).
Review the Pull Request that your collaborator made in your repository.
🚀 Optional: Add additional changes on the branch pushed by your collaborator.
Merge the pull request into your repository.
Exercises (4)
Collaboration Option 3 (Alternative)
Clone and sync your repository
Move to a location on your computer where you want to clone a repository.
Clone your remote repository to a different location on your computer.
Stage and commit changes in the new location (consider using a new branch).
Push these new changes to GitHub.
Pull the changes to the repository in the original location.
Delete your newly cloned repository.
🚀 Bonus exercises
Add a README.md
Find the option to create a new file on your remote repository in the browser.
Name the file README.md, add a brief description, and provide a commit message.
Figure 1: “Understanding public key private key concepts” by Blake Smith (License: Unknown; Source on GitHub)
When you set up SSH, you generate a key pair: the private key stays on your device, while the public key is copied to the servers you want access to.
Think of your public key as a padlock. You can make lots of copies of this padlock and distribute them to different places - servers, computers, or anything you want to secure (just like sharing a padlock to secure different lockers). These places install your padlock, but no one can open it because they don’t have your key.
Your private key is the actual key that opens those padlocks. You keep it safe and never share it. As long as your key stays secure, it doesn’t matter how many padlocks you’ve distributed.
When you try to connect to a server, SSH checks if the padlock (public key) on that server matches your key. If they match, the door opens.
Warning: “The authenticity of host can’t be established”
TL;DR: Enter yes and hit Enter.
The following message might appear when connecting to GitHub for the first time via SSH:
#| code-copy: falseThe authenticity of host 'github.com (140.82.121.3)' can't be established.ECDSA key fingerprint is SHA256:nThbg6GUVB7ZdnZ3iXYvgIhXZOL7NXqb7A4s9F8XY7w.Are you sure you want to continue connecting (yes/no/[fingerprint])?
This message indicates that your system is trying to connect to a remote server (i.e., GitHub) over SSH for the first time, but it doesn’t yet trust the server’s identity.
If you are trusting the server’s identity, enter yes and hit EnterEnter. This will result in the following message:
#| code-copy: falseWarning: Permanently added 'github.com' (ED25519) to the list of known hosts.
This message indicates that the GitHub server’s public key has been added to your ~/.ssh/known_hosts file permanently, so you won’t be prompted with this warning the next time you connect to GitHub over SSH.
After a successful push, you will get a message like this:
#| code-copy: falseEnumerating objects: 17, done.Counting objects: 100% (17/17), done.Delta compression using up to 8 threadsCompressing objects: 100% (12/12), done.Writing objects: 100% (12/12), 2.11 KiB | 2.11 MiB/s, done.Total 12 (delta 9), reused 0 (delta 0), pack-reused 0remote: Resolving deltas: 100% (9/9), completed with 5 local objects.To https://github.com/username/repository.git fb3efef..8f50685 main -> main
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
4 Solutions
Solutions: Connect to remote repositories using SSH
Code
#!/bin/bash1ssh-keygen-t ed25519 -C"your_email@example.com"2cat ~/.ssh/id_ed25519.pub3# Copy the SSh key to your account
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/bash1# create an empty remote repository2cd my-project3git remote add origin https://github.com/your-username/your-repo-name.git4git push -u origin main
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 project 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 default branch (here, main) to the remote repository using git push -u origin main.
Solutions: Private collaboration with pull requests (using GitHub Flow)
Code
#!/bin/bash1# Add your exercise partner as a collaborator to your recipes repository2cd ~3git clone https://github.com/partner-username/partner-repo-name.git4git checkout -b new-branch-name5echo"New Recipe">> recipes.txt6git add recipes.txtgit commit -m"Add new recipe to recipes.txt"7git push origin new-branch-name8# Create a Pull / Merge Request.9# Review the PR your partner made in your repository.10# Merge the PR into your repository.
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.