{ "cells": [ { "cell_type": "markdown", "id": "8f6bbaa9", "metadata": {}, "source": [ "# Version control with Git\n", "\n", "Imagine working on a piece of code for a few days or weeks, modifying it over time, only to realize after a while that the code you wrote and deleted on the first day was actually the way to go.\n", "Wish you could go back and retrieve that code, right?\n", "\n", "In these situations, a habit of working with a **version control** program comes to your rescue!\n", "[Git](https://git-scm.com/) is the most popular software for version control - it allows you to manage the changes you make in your code.\n", "Regularly saving your code with Git is extremely useful, as it allows you to **track changes**, that is, to see all versions that you stored to Git as you were working.\n", "In practice, that means that you can trace back everything you deleted and added throughout your work on the code.\n", "\n", "```{figure} ../images/chapter3/track-changes.png\n", "---\n", "height: 400px\n", "name: track-changes\n", "---\n", "Python code with tracked changes (additions in green and deletions in red). \n", "```\n", "\n", "We recommend you to regularly use Git both while learning with this book as well as in all your future programming.\n", "It's one of the good practices of coding that is nice to make a habit from the get-go." ] }, { "cell_type": "markdown", "id": "3534b955", "metadata": {}, "source": [ "## Git in a nutshell\n", "\n", "You will work only with the basics of Git at this level. \n", "These will allow you to save your code and track its version, as well as to retrieve code from a remote repository.\n", "Let's get acquainted with the basic functioning of Git and the appropriate terminology.\n", "\n", "```{figure} ../images/chapter3/git-scheme.png\n", "---\n", "height: 300px\n", "name: git-scheme\n", "---\n", "Overview of Git and basic commands. Workspace, staging environment, and local repository are all local, while the GitLab repository is remote.\n", "```\n", "\n", "Whenever you reach a milestone in your code or finish a part of the work, you should **add** files to the staging environment (command `git add`).\n", "Staged files are then ready to be **committed** to the repository (command `git commit`).\n", "You can think of a commit as a \"save point\" to which you can then go back (as in {numref}`track-changes`).\n", "It's good practice to add a **message** to each of your commits - this will allow you (and others, if you're collaborating on a code) to easily remember what your commit was about.\n", "Finally, you can **push** (command `git push`) your changes to your remote repository on GitLab, and vice versa, get files from a remote repository with **pulling** (command `git pull`).\n", "In this context, a **repository** contains all project files, including the revision history.\n", "\n", "```{admonition} Backup\n", ":class: note\n", "Note that regularly pushing your files not only allows you to go back through the changes of your code, but it also serves as a backup. Should anything happen to your computer, your Python scripts will still be living safely in your GitLab repository.\n", "```\n", "\n", "In its entirety, Git is a rather complex software (e.g., check out this [Git glossary](https://git-scm.com/docs/gitglossary) to get an idea).\n", "Down the line, learning more about Git will allow you to do more complex things than just committing and pushing your changes." ] }, { "cell_type": "markdown", "id": "3ab4ba44", "metadata": {}, "source": [ "## Git in practice *(TU Delft-specific)*\n", "\n", "```{admonition} GitLab vs. GitHub\n", ":class: warning\n", "This section describes linking VS Code to GitLab, which is specific to the Delft University of Technology and may be applicable to other institutions who host their own GitLab.\n", "Besides GitLab, [GitHub](https://github.com/) is another very popular platform for code version control which many individuals use.\n", "If you're not using GitLab, we therefore recommend you to open a GitHub account. Note that some of the instructions below still apply, so it may still be useful to go through them.\n", "If you are currently at TU Delft, notice that you'll lose access to its GitLab after you leave the university - you can then open an account on GitHub and use it for version control and collaboration on your future coding projects.\n", "```\n", "\n", "Your remote repository will be at [TU Delft-hosted GitLab](https://gitlab.tudelft.nl/users/sign_in), to which you can sign in with your NetID and password.\n", "\n", "In order to perform actions of committing and pushing changes to GitLab, you will integrate your GitLab repositories with VS Code.\n", "In total, you will have two repositories:\n", "1. Your private repository, where you will be able to access, work, and store your exercises.\n", "2. A shared repository with one of your peers, which you will use for peer feedback.\n", "\n", "Let's set up GitLab and link your two repositories to VS Code." ] }, { "cell_type": "markdown", "id": "702aa4c4", "metadata": {}, "source": [ "### Setting up GitLab - SSH key\n", "\n", "An **SSH key** acts like a digital pass, allowing GitLab to verify that you are authorized to access the repository from your local computer. Without an SSH key, GitLab won't let you push or pull changes because it can't verify that you're a trusted user. Adding an SSH key is a one-time setup step that simplifies your workflow by allowing you to push and pull without repeatedly entering your GitLab username and password.\n", "\n", "Therefore, we will first set up the SSH key.\n", "\n", "1. Login to [TU Delft GitLab](https://gitlab.tudelft.nl/users/sign_in) using your NetID and password.\n", "2. You should be able to see your repositories - a private repository and a repository shared with your peer. A repository/project looks like this:\n", "\n", "```{figure} ../images/installation/git/git-project.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```\n", "\n", "3. If you click on the name of your project, you will see a page like this:\n", "\n", "\n", "```{figure} ../images/installation/git/ssh1.png\n", "---\n", "height: 450px\n", "name: \n", "---\n", "```\n", "\n", "You will notice the warning regarding the SSH key at the top of the screen. \n", "\n", "4. To check if you already have an SSH key, open your terminal and run `ls ~/.ssh`. Look for files named `id_rsa` and `id_rsa.pub`, or similar. If you don't see them, you need to generate a new SSH key.\n", "5. To generate a new SSH key, run `ssh-keygen -t rsa -b 4096 -C \"your_email@example.com\"` in your terminal. When prompted, you can press `Enter` to accept the default file location, and optionally set a passphrase for extra security. Select and copy the entire output, staring with `ssh-rsa`.\n", "6. In GitLab, in the upper left corner, find your user profile picture, click on it, and select Preferences.\n", "\n", "```{figure} ../images/installation/git/ssh2.png\n", "---\n", "height: 300px\n", "name: \n", "---\n", "```\n", "\n", "In the new left sidebar, find SSH Keys and click on it.\n", "\n", "7. Paste your SSH Key into the Key field. Add a title for the key (for example, \"My Laptop SSH Key\") and click Add key.\n", "8. Run this command to test the SSH connection to GitLab: `ssh -T git@gitlab.tudelft.nl`. You should see a message like \"Welcome to GitLab, @username!\" which means the connection is working." ] }, { "cell_type": "markdown", "id": "c6566928", "metadata": {}, "source": [ "### Git integration in VS Code\n", "\n", "We will clone/download GitLab repositories to your computer from within VS Code. You will first clone your private repository from GitLab. Then you can repeat the process for the shared repository. \n", "\n", "The process consists of 2 parts: finding the links (URLs) for your repositories and linking them to your VS Code. \n", "\n", "**Part 1. GitLab repository link**\n", "\n", "1. If you're not already logged in, login to [TU Delft GitLab](https://gitlab.tudelft.nl/users/sign_in) using your NetID and password.\n", "2. You should be able to see your repositories - a private repository and a repository shared with your peer. \n", "3. If you click on your project, you will see a page like this:\n", "\n", "```{figure} ../images/installation/git/git-link.png\n", "---\n", "height: 350px\n", "name: \n", "---\n", "```\n", "\n", "4. Click on the blue button \"Code\" (circled red on the image). In the dropdown menu, press the copy icon next to \"**Clone with HTTPS**\" to copy the URL to your repository to your Clipboard. You are now ready to go to VS Code for Part 2.\n", "\n", "**Part 2. Cloning repository in VS Code**\n", " \n", "1. In VS Code, go to **View** and open the **Command Palette**. You will find View at the very top of your screen in the menu bar, next to other options such as File, Edit, Selection, etc.\n", "2. Type `Git: Clone` and select this command. If your VS Code does not display `Git: Clone` as an option, close your VS Code, open it again, and try steps 1 and 2 one more time.\n", "3. In the dialog that appears, paste the GitLab repository URL that you copied to your Clipboard in Part 1. If VS Code starts asking you about linking to Git**Hub**, don't allow it. We're not linking to GitHub but rather to Git**Lab**.\n", "4. You will be asked to select a local folder where the repository should be cloned. For this purpose, open a **new empty folder** in a location of your choice. Choose something meaningful and easily findable, e.g., a subfolder of your studies in the `Documents` directory. When you make this selection, cloning will start. It may take several minutes.\n", "5. During this process, a new window will pop up and ask for your GitLab **credentials**. You should fill in your NetID and password.\n", "6. Once the repository is fully cloned, VS Code will ask if you want to open the project. You can decide not to do this right now.\n", "7. In VS Code's built-in terminal, navigate to the your GitLab repository's folder that you selected in step 4 and then run this command: \n", "```\n", "git remote add origin git@gitlab.tudelft.nl:username/repository-name.git\n", "```\n", "where you replace `username` with your username and `repository-name` with your repository name. This step sets the remote URL to use SSH instead of HTTPS.\n", "\n", "Now repeat Part 1 and Part 2 for your shared folder. You can then start working on your Python projects and **manage Git directly in VS Code**.\n" ] }, { "cell_type": "markdown", "id": "ffc3aa76", "metadata": {}, "source": [ "```{admonition} SSH vs. HTTPS\n", ":class: dropdown, note\n", "\n", "Using SSH instead of HTTPS has a few practical advantages:\n", "\n", "- **Easier authentication**: With SSH, once you've set up your SSH key and added it to your GitLab profile, you don’t have to enter your username and password each time you push or pull. SSH keys provide secure, password-less access to your GitLab repositories, which is particularly helpful if you’re frequently pushing or pulling changes (which you are encouraged to do).\n", "\n", "- **Enhanced security**: SSH keys are generally more secure than password-based authentication. When you use an SSH key, you are authenticated through a key pair (a public and a private key) instead of through a password. This reduces the risk of unauthorized access, especially for sensitive repositories.\n", "\n", "- **Avoiding rate limits or two-factor authentication prompts**: Some Git platforms, including GitLab, enforce rate limits on HTTPS requests or require re-authentication due to two-factor authentication (2FA) policies. With SSH, once your key is configured, these restrictions won’t interfere with your workflow.\n", "\n", "```" ] }, { "cell_type": "markdown", "id": "1af9d119", "metadata": {}, "source": [ "### Working with Git in VS Code\n", " \n", "#### Checking Git status in VS Code\n", "1. On the left sidebar, click the Source Control icon (represented by a branch icon).\n", "2. VS Code will show the current state of the repository:\n", " - **Untracked files**: New files added.\n", " - **Modified files**: Files that have been changed.\n", "\n", "\n", "#### Making changes, staging, and committing\n", "1. **Make changes** to the Python code in the editor.\n", "2. In the Source Control tab, hover over the file and click the `+` icon to **stage** the changes.\n", "3. Write a **commit message** in the input box at the top of the Source Control tab (e.g., \"Added function to calculate sum\").\n", "4. Click the checkmark icon (`✔`) to **commit** the changes.\n", "\n", "\n", "#### Pushing changes to GitLab\n", "1. After committing, push the changes to GitLab:\n", " - Click the **three dots** at the top of the Source Control tab, then choose **Push**.\n", " - If prompted, enter the GitLab credentials (username and personal access token, which can be generated from GitLab under **Settings** → **Access Tokens**).\n", "2. The changes will now be uploaded to your repository on GitLab.\n", "\n", "\n", "#### Pulling updates from GitLab\n", "If someone else (teachers or peers/collaborators) makes changes in your repository, you can pull the latest updates:\n", "1. Go to the **Source Control** tab.\n", "2. Click the **three dots** at the top and choose **Pull** to fetch the latest changes.\n", "\n", "#### Collaboration\n", "\n", "You can collaborate on GitLab by:\n", " - Committing your changes.\n", " - Pushing your changes to the shared repository.\n", " - Pulling the changes made by others from GitLab.\n", "\n" ] }, { "cell_type": "markdown", "id": "8fd344a4", "metadata": {}, "source": [ "\n", "```{admonition} Merging conflicts\n", ":class: note, dropdown\n", "With Git, it is possible to collaborate on the same piece of code. In that case, the changes made by different people are merged once they push them to Git.\n", "\n", "VS Code handles merging changes automatically, but in case of conflicts (e.g., when two people modify the same file, which in principle shouldn't happen during peer feedback), Git will flag the conflict, and VS Code will help you resolve it.\n", "```\n" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }