1 Last session: First steps with Git & Git Essentials
Last session: First steps with Git & Git Essentials
After the last session, you should now be able to answer the following questions / do the following:
First steps with Git
đź’ˇ 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.
Git essentials
đź’ˇ You know how to explore the commit history. đź’ˇ You can compare different commits. đź’ˇ You know how to use and create a .gitignore file. đź’ˇ You can discuss which files can (not) be tracked well with Git and why. đź’ˇ You know how to track empty folders in Git repositories.
Last session: Cheatsheet
First steps with Git
Command
Description
git init
Initializes a folder as a Git repository
git status
Shows Git tracking status of files in the repository
git add
Adds file(s) to the staging area
git commit
Commits staged files
git commit -m "commit message"
Commits staged files with a commit message
Last session: Cheatsheet
Git essentials
Command
Description
git log
Views past commits
git diff
Views made changes compared to the last commit
git mv
Renames or moves files and automatically stages the changes
Last session: recipes project
First steps with Git
At the end of this session, you should have accomplished the following:
You set up Git.
You initialized your recipes folder as a Git repository.
You committed your first file to the recipes repository.
Please keep the recipes folder! We will continue to use it in the following sessions.
Last session: recipes project
Git Essentials
At the end of this session, you should have accomplished the following:
Commit at least three changes in recipes.txt.
Make at least one commit using git commit --amend.
For example, add a a new entry without a title first, commit, then add a title and use git commit --amend to add the title change to the same commit.
Create a .gitignore file.
Everyone: Create a random file that you want to ignore, for example image.jpg.
All macOS users: Let your repository ignore .DS_Store.
🚀 Optional: Commit .gitkeep in an otherwise empty directory.
Please keep the recipes folder! We will continue to use it in the following sessions.
Common questions from last session
“How can I make changes in a text (.txt) file?”
Remember: It’s just a regular text file on your computer.
Option 1: Use a regular text editor (e.g., TextEdit on macOS, Notepad++ on Windows, or others).
Option 2: Use vim directly from the command line (see chapter / session on “Setup”).
Option 3: Use command line commands like echo "My text" >> recipes.txt.
“I ran git commit but nothing happened?”
Maybe you forgot to run git add before?
“Remember to cd into your recipes project folder!”
Every time you open the command line, it starts at a default location (likely your home directory).
Check in which folder you are, using pwd (“print working directory”).
Use cd to move to your recipes project folder before you start running the Git commands.
Important note: Git repository in /user folder?
Please make sure that your recipes folder is in a suitable place (for example, in the Desktop, Documents folders or where you keep your course-related files, …) and not in your user directory!
You can check your current working directory by running pwd in your recipes folder.
Also: Always remember to cd into your repository before you execute Git commands!
If your recipes folder is in your user directory, please tell us and we can fix it.
Tip: To prevent ever creating a Git repository inside your user folder, you can use:
Code
touch ~/.git
Running git init in the user directory now results in a (desired) error:
đź’ˇ 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.
At the end of this session, you should have accomplished the following:
You created a GitHub account.
You connected your GitHub account to your local Git using SSH.
You created a new private(!) GitHub repository.
You uploaded (i.e., “pushed”) the default branch of your recipes repository to GitHub.
Collaboration Option 1/2 (Basic / Advanced):
You invited a partner from the course as a collaborator to your recipes on GitHub.
You collaborated on a shared project by adding and committing changes to your partner’s repository.
Collaboration Option 3 (Alternative):
You clone your own repository to a different location on your computer.
Stage and commit changes in the repository in this new location.
Push the changes to GitHub and pull them in the original repository location on your computer.
Optional:
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.
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
Exercises (1)
Mandatory for everyone!
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 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.
#!/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.
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.