{ "cells": [ { "cell_type": "markdown", "id": "944edc8e-6c33-4789-8d17-3353c02d8b6d", "metadata": {}, "source": [ "# Connecting Git by SSH\n", "If you have an HTTPS connection to your git repository it is safe, but the user name and password have to be entered for every synchronisation. SSH offers the opportunity the get a direct connection without your credentials, but it needs an initial set up. This Jupyter Notebook supports the set up for adding an SSH key to your repository and change the connection from HTTPS to SSH." ] }, { "cell_type": "markdown", "id": "3d292cc2-d12e-4c70-8110-e8671aa1fa77", "metadata": {}, "source": [ "### Generate new SSH key\n", "With the following code you will generate a new SSH key which is needed for the direct connection to your git repository. Run the following code by pressing `Shift`+`Enter` within the cell." ] }, { "cell_type": "code", "execution_count": null, "id": "d7cb65f4-7d4b-4ee9-8223-9738bfd0b2b6", "metadata": { "tags": [] }, "outputs": [], "source": [ "!ssh-keygen -t rsa -N \"\" -f \"$HOME/.ssh/id_rsa\"" ] }, { "cell_type": "markdown", "id": "4f00fda2-993a-4f1a-b10e-d4928b3cd53c", "metadata": {}, "source": [ "### Copy SSH key to git repository\n", "Now run the following cell, copy the output and add it as a new git SSH key in your git repository:" ] }, { "cell_type": "code", "execution_count": null, "id": "9ca73c49-0289-4834-872e-600a2964926d", "metadata": { "tags": [] }, "outputs": [], "source": [ "!cat \"$HOME/.ssh/id_rsa.pub\"" ] }, { "cell_type": "markdown", "id": "744138dc-9e7d-4605-acf7-0129ac83368b", "metadata": {}, "source": [ "### Switch from HTTPS to SSH\n", "Now we will change the connection type from HTTPS to SSH." ] }, { "cell_type": "code", "execution_count": null, "id": "a2f2bbb4-f017-43a1-82b2-306249959a01", "metadata": { "tags": [] }, "outputs": [], "source": [ "import os\n", "projectdir = input(\"Git project directory (start in your root directory without slash): \")\n", "projectpath = \"/home/jovyan/\" + projectdir\n", "os.chdir(projectpath)\n", "!git remote set-url origin $(git remote get-url origin --push | sed 's/https:\\/\\//git@/' | sed 's/\\//:/')\n", "print(\"OK\")" ] }, { "cell_type": "markdown", "id": "6dd918d8-820f-4e50-920f-1fb20dc6b72f", "metadata": {}, "source": [ "### Add Fingerprint\n", "As a last step you have to open up Terminal within Jupyter, navigate to your git project folder and initiate a first git push or pull. You'll be asked to add the fingerprint of the host to your known hosts. This has to be done only once.\n", "\n", "1. Open the Jupyter Launcer and start a new Terminal.\n", "1. Change to your git project directory with: `cd your_project_dir` (e.g. `cd jupyter-notebooks`)\n", "1. Now initiate a first git push or pull: `git push`\n", "1. You'll be asked if you accept the fingerprint. This has to be confirmed by typing `yes`.\n", "\n", "Now you are all set and you should be able to use the git UI from within your JupyterLab." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 5 }