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:
You created a new branch and merged changes to your default branch.
🚀 Optional: You created and resolved a merge conflict.
Please keep the recipes folder! We will continue to use it in the following sessions.
đź’ˇ 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:
At the end of this session, you should have accomplished the following:
You connected your GitHub account to your local Git.
You created a new GitHub repository and uploaded (i.e., “pushed”) your recipes project.
You collaborated on a shared project by adding and committing changes to a partner’s repository.
Optional:
You cleaned up your project by deleting unnecessary files and branches.
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
Generate an SSH key.
Copy the SSH key to your clipboard.
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 the recipes repository using the command line.
Set the remote URL of your local repository to your remote repository.
Push the changes on your main branch to your remote repository.
Exercises 2
“Private” collaboration with pull requests (using GitHub Flow)
Add your exercise partner as a collaborator to your recipes repository.
Clone your partner’s repository.
Create a new branch in your collaborator’s repository.
Add a recipe to your collaborator’s recipes.txt file.
Add and commit the changes.
Push the changes on the new branch to the remote repository.
Create a Pull / Merge Request.
Review the PR your collaborator made in your repository.
🚀 Optional: Add additional changes on the branch pushed by your collaborator.
Merge the PR into your 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.
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.
Solutions
Connect to remote repositories using SSH
Code
#!/bin/bashssh-keygen-t ed25519 -C"your_email@example.com"# <1>cat ~/.ssh/id_ed25519.pub # <2># Copy the SSh key to your account # <3>
In the command line, create a new SSH key. Make sure to change the example email to your email address. Optionally, provide a passphrase.
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.
Add the SSH key to your remote repository account.
Solutions
Upload your local repository to a remote repository
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.
Optional: Navigate into the recipes repository using cd (or a similar path).
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).
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>
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.
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 (~).
Clone your partner’s repository using git clone. Make sure that you not cloning into an existing repository.
Create a new branch in your partner’s repository.
Add a recipe to your partner’s recipes.txt file.
Add and commit the changes using a descriptive commit message.
Push the changes on the new branch to GitHub.
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.
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.
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
In your browser, go to your remote repository (for example, on GitHub), click Add file, and select Create new file.
Name the file README.md. Add a brief description of your project. Provide a descriptive commit message at the bottom.