Save a notebook to GitHub

You can save the contents of a user-managed notebooks instance to GitHub to back up your notebook, as well as make it available to others to use.

To save a notebook to GitHub, follow these steps:

  1. Create a GitHub repository.

    If you do not already have a GitHub repository, you must create one. When you create your GitHub repository, initialize the repository with a README file to ensure that the repository can be cloned.

    Initialize a GitHub repository with a README file

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

    1. In the Google Cloud Console, go to the Notebooks page.

      Go to Notebooks

    2. On the User-managed notebooks tab, open your user-managed notebooks instance by clicking Open JupyterLab.

    3. Select Git -> Open Terminal to open a Git terminal window.

    4. In the Git terminal window, enter the following commands to configure your Git user name and email:

      git config --global "YOUR_NAME"
      git config --global "YOUR_EMAIL"
    5. If your GitHub account requires SSH authentication, there are a few more steps to connect your account. From your Git terminal window in your user-managed notebooks instance, follow GitHub's instructions for generating a new SSH key. Then follow the instructions for adding that SSH key to your GitHub account.

    6. Close the Git terminal window.

  1. Clone your GitHub repository in your user-managed notebooks instance.

    1. Copy the URL for your GitHub repository. You can get this from the Clone or download option in your GitHub repository.

      Get GitHub repository URL.

    2. In your user-managed notebooks instance, select the folder where you want to clone the GitHub repository. For example, the home folder as shown below:

      Notebook home folder.

    3. Click the Git clone button.

      Clone repository button.

    4. When prompted, paste in the URL for your GitHub repository that you copied earlier. Then click Clone.

      Paste repository URL and clone.

  2. Your user-managed notebooks instance shows your repository as a new folder. If you do not see your cloned GitHub repository as a folder, click the Refresh File List button.

    Refresh File List button.

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

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

    Select File -> New -> Notebook. This creates a new notebook named "Untitled.ipynb". Select the kernel for your new notebook. For example, "Python 3". You can also create a Python 3 notebook using the Launcher.

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

    Right-click the new notebook and select Track to add the new notebook as a file for your GitHub repository. Your notebook is now added to the Staged grouping in the Git tab.

  6. To commit your new notebook to your GitHub repository, add a commit comment in the Git tab and click the Commit button.

  7. Select Git -> Open terminal to open a Git terminal window.

    Enter the git push command in the Git terminal window. When prompted, enter your GitHub username and password.

    When the git push command completes, your committed files have been added to your GitHub repository.