Save a notebook to GitHub

To back up your user-managed notebooks instance and make it available to others, save the contents of your user-managed notebooks instance to GitHub.

Create a GitHub repository

If you don't already have a GitHub repository, you must create one.

When you create your GitHub repository make sure that your GitHub repository can be cloned by selecting the Initialize this repository with a README checkbox.

Initialize a GitHub repository with a README file.

Clone your GitHub repository in your user-managed notebooks instance

To clone your GitHub repository in your user-managed notebooks instance, complete the following steps:

  1. In your GitHub repository, click the Code button, and then click the Local tab.

  2. Copy the HTTPS URL.

  3. In the Google Cloud console, go to the User-managed notebooks page.

    Go to User-managed notebooks

  4. Click Open JupyterLab to open your user-managed notebooks instance.

  5. In the JupyterLab  File Browser, select the folder where you want to clone the GitHub repository. For example, the home folder.

    Notebook home folder.

  6. In JupyterLab, select Git > Clone a Repository.

  7. If prompted, enter your credentials.

    • If you use a GitHub username and password, enter your GitHub username and password.

    • If you use two-factor authentication with GitHub, create and use a personal access token.

  8. In the Clone a repo dialog, paste the HTTPS URL for your GitHub repository.

    Paste repository URL and clone.

  9. Click Clone.

Configure your user-managed notebooks instance with your GitHub user information

  1. In JupyterLab, select Git > Open Git Repository in Terminal to open a Git terminal window.

  2. In the Git terminal window, enter the following commands to configure your Git username and email:

    git config --global user.name "YOUR_NAME"
    git config --global user.email "YOUR_EMAIL"
    
  3. If your GitHub account requires SSH authentication, complete the following steps to connect your account:

    1. From your Git terminal in your user-managed notebooks instance, follow GitHub's instructions for generating a new SSH key.

    2. Follow the instructions for adding that SSH key to your GitHub account.

  4. Close the Git terminal window.

Add your committed files to your GitHub repository

  1. Your user-managed notebooks instance shows your repository as a new folder. If you don't see your cloned GitHub repository as a folder, click the Refresh File List button.

    Refresh File List button.

  2. Double-click your repository folder to open it.

  3. Add a new notebook to your user-managed notebooks instance.

    To add a notebook file, you can use the menu or the Launcher.

    1. To add a new notebook file from the menu, select File > New > Notebook.

      Add a notebook file.

    2. In the Select kernel dialog, select the kernel for your new notebook, for example, Python 3, and then click Select.

      Your new notebook file opens.

    Launcher

    1. To add a new notebook file from the Launcher, select File > New > Launcher.

    2. Click the tile for the kernel you want to use.

      Add a notebook file.

      Your new notebook file opens.

  4. Rename your new notebook file.

    1. Select File > Rename notebook. The Rename file dialog opens.

    2. In the New name field, change Untitled.ipynb to something meaningful, such as install.ipynb.

    3. Click Rename.

    Launcher

    1. Right-click the Untitled.ipynb tab and then click Rename notebook. The Rename file dialog opens.

    2. In the New name field, change Untitled.ipynb to something meaningful, such as install.ipynb.

    3. Click Rename.

  5. Select the Git tab. Your new notebook is listed in the Untracked grouping.

  6. To add the new notebook as a file for your GitHub repository, right-click the new notebook and select Track. On the Git tab, your notebook is now added to the Staged grouping.

  7. To commit your new notebook to your GitHub repository, on the Git tab, add a commit comment and click Commit.

  8. To open a Git terminal window, select Git > Open Git repository in terminal.

  9. In the Git terminal window, enter the git push command.

    • If you use a GitHub username and password, when prompted, enter your GitHub username and password.

    • If you use two-factor authentication with GitHub, create a personal access token to use.

    When the git push command completes, your committed files are in your GitHub repository.