Exercise 1.0: Clone the Repository
1 Clone the repository
1.1 Git Basics: Why We Use Version Control
Welcome to using Git and GitHub Classroom! Git is a powerful version control system that lets you track changes to your code over time. A repository (or repo) is essentially the folder for your project. GitHub is a platform that hosts your repos, allowing you to work on them from different computers and collaborate with others. This guide will walk you through the steps to get your assignment repo from GitHub onto your local machine.
2 Using Git and GitHub Classroom for the First Time
Welcome to using Git and GitHub Classroom! Git is a powerful version control system that lets you track changes to your code, and GitHub is a platform that hosts your repositories (repos). GitHub Classroom simplifies the process of getting started with assignments. This guide will walk you through the steps to get your assignment repo onto your local machine.
2.1 🛠️ Prerequisites: Get Your Tools Ready
Before you begin, you must have the following items in place. Take a moment to verify each one to save yourself from potential headaches down the road.
Git Installed and Tested: You must have Git installed on your computer. To check this, open your terminal (on Mac/Linux) or Git Bash (on Windows) and run
git --version.- Mac: Git is often pre-installed. If not, install Xcode Command Line Tools by running
xcode-select --installin your terminal. - Windows: Download the installer from the official Git for Windows website.
- Linux (Ubuntu/Debian): Run
sudo apt-get install git-all.
- Mac: Git is often pre-installed. If not, install Xcode Command Line Tools by running
A Verified GitHub Account: Ensure your GitHub account has a verified email address. If you’re not sure, check your email settings on GitHub and resend the verification email if needed.
A Verified GitHub Classroom Repository: You must have already clicked the assignment invitation link provided by your instructor. This action automatically creates a private repository for you. If you’ve done this correctly, you’ll see the repo listed in your GitHub account.
An invite link If you have not received an invite link to the project repository, ask your Professor for the link. You’ll need to visit the link and accept the project in order to link your personal Github account to the Classroom project and to clone the repository using
git.
2.2 🔐 User Authentication
To access a private repository, you need to authenticate with GitHub. This proves you have the right to access the repo. There are two primary methods for doing this: using a Personal Access Token (PAT) via HTTPS or using an SSH key.
If you created your GitHub account using Google or another single sign-on (SSO) service, you might not have a password set. You will need one to authenticate Git on your local machine.
- To set a password, go to your GitHub Settings > Access > Password and authentication.
- Follow the prompts to create a new password. You will need this for certain operations, even if you plan to use a Personal Access Token (PAT) or SSH key.
Method A: Authenticating with a Personal Access Token (PAT)
This is the recommended method for most users. A PAT is a secure password that gives you access to your GitHub account without using your regular password.
Generate a PAT
- In your GitHub account, click your profile picture > Settings > Developer settings > Personal access tokens > Tokens (classic).
- Click “Generate new token (classic)”.
- Give your token a descriptive name (e.g., “Classroom Token”) and set a reasonable expiration date (e.g., the end of the semester).
- Under “Select scopes,” check the box next to
repo. - Click “Generate token”.
- ⚠️ Copy the generated token immediately! You will not be able to see it again. Treat this token like a password.
Method B: Authenticating with an SSH Key
SSH keys are a highly secure way to authenticate. Once set up, you won’t need to enter a password or token again. This method is often preferred by experienced developers.
Generate a New SSH Key
- First, check if you already have an SSH key by running
ls -al ~/.ssh. - If you don’t see files like
id_ed25519orid_rsa, you need to create one. The recommended key type ised25519. - Run the following command in your terminal, replacing the email with your GitHub email:
ssh-keygen -t ed25519 -C "your_email@example.com"- When prompted, press Enter to accept the default file location. You can also set a passphrase for an extra layer of security.
Add Your SSH Key to GitHub
- Copy your public SSH key to your clipboard. The public key is the one with the
.pubextension. You can print the contents of the key file to your terminal using:
cat ~/.ssh/id_ed25519.pubCopy the entire output, starting with `ssh-ed25519`.
- Log in to GitHub and go to Settings > SSH and GPG keys.
- Click “New SSH key”.
- Give it a title (e.g., “My Laptop”).
- Paste the public key you copied into the “Key” field.
- Click “Add SSH key”.
2.3 🚀 Clone Your Repository
The next step is to get the code from your GitHub Classroom repository onto your computer. This process is called cloning.
- Navigate to your assignment repository on GitHub.
- Click the green
< > Codebutton. - You’ll see two URL options: HTTPS and SSH.
Now, open your terminal or command prompt and use the git clone command with the URL you copied. The URL you choose depends on which authentication method you selected. Choose the HTTPS URL if you created a personal access token for authentication and the SSH URL if you used an SSH key.
- If you chose the HTTPS URL:
git clone https://github.com/your-org/your-repo-name.gitWhen prompted for a password, paste the PAT you generated in Method A.
- If you chose the SSH URL:
git clone git@github.com:your-org/your-repo-name.gitGit will use your SSH key to authenticate without asking for a password.
2.4 🧭 After Cloning: Your Next Steps
After successfully cloning the repository, you’ll have a new folder on your computer with the same name as the repository. Here’s what to do next:
- Change into the new directory: Use the
cdcommand to navigate into your project folder.
cd <repository-name>Check your status: Get into the habit of running
git statusto see which files have been modified.Start your workflow: The basic Git workflow for submitting changes involves three key commands:
git add <file>: Stages a file, preparing it to be committed.git commit -m "Your descriptive message": Creates a snapshot of the staged files.git push: Uploads your committed changes to the GitHub repository.
2.5 🆘 Troubleshooting Common Issues
- Error:
Permission denied (publickey)- Cause: This usually means your SSH key is not correctly set up on GitHub or your local machine.
- Solution: Double-check that you’ve added the correct public key to your GitHub account and that your SSH agent is running.
- Error:
Repository not found- Cause: This often happens due to a typo in the URL or if you are not logged in with the correct GitHub account that has access to the private repo.
- Solution: Verify the URL and ensure you’ve used the correct authentication method for the account that owns the repository.
Cache your credentials To avoid entering your PAT for every command, use the Git Credential Manager on Windows or the Git Credential Helper on Mac/Linux.
To store credentials in a file on your local machine, use the store credential helper. Note that this stores credentials in plain text.
git config --global credential.helper storeThe Git Credential Manager is included with Git for Windows and is enabled by default. It securely stores your credentials in the Windows Credential Manager. You generally don’t need to run a command to set this up.
You can use the macOS Keychain to store your credentials securely.
git config --global credential.helper osxkeychain