Updated on 2024-10-29 GMT+08:00

Using Git to Clone the Code Repository in JupyterLab

In JupyterLab, you can use the Git plug-in to clone the GitHub open-source code repository, quickly view and edit data, and submit the modified data.

Prerequisites

The notebook instance is running.

Starting the Git Plug-in of JupyterLab

In the notebook instance list, locate the target instance and click Open in the Operation column to go to the JupyterLab page.

Figure 1 shows the Git plug-in of JupyterLab.

Figure 1 Git plug-in

Cloning a GitHub Open-Source Code Repository

Access a GitHub open-source code repository at https://github.com/jupyterlab/extension-examplesitHub. Click , enter the repository address, and click OK to start cloning. After the cloning is complete, the code library folder is displayed in the navigation pane of JupyterLab.

Cloning a GitHub Private Code Repository

  1. When you clone a GitHub private code repository, a dialog box will be displayed, asking you to enter your personal credentials. In this case, enter the personal access token in GitHub.

  2. To obtain a personal access token, perform the following operations:
    1. Log in to GitHub and open the configuration page.
    2. Click Developer settings.
    3. Choose Personal access tokens > Generate new token.
    4. Verify the login account.
    5. Describe the token, select permissions to access the private repository, and click Generate token to generate a token.
    6. Copy the generated token to CloudBuild.
    • Save the token securely once it is generated. It will be unavailable after you refresh the page. If it is not obtained, generate a new token.
    • Enter a valid token description so that it can be easily identified. If the token is deleted by mistake, the building will fail.
    • Delete the token when it is no longer used to prevent information leakage.
    Figure 2 Cloning a GitHub private code repository (only authorization using a personal access token is supported)
    Figure 3 Obtaining a personal access token

Viewing a Code Repository

In the list under Name, double-click the folder you want to use and click the Git plug-in icon on the left to access the code repository corresponding to the folder.

Figure 4 Opening the folder and starting the Git plug-in
You can view the information current code repository, such as the repository name, branch, and historical submission records.
Figure 5 Viewing a code repository

By default, the Git plug-in clones the master branch. To switch another branch, click Current Branch to expand all branches and click the target branch name.

Viewing Modifications

If a file in the code repository has been modified, you can view the modified file under Changed on the Changes tab page. Click Diff this file on the right of the file name to view the modifications.

Figure 6 Viewing modifications

Committing Modifications

After confirming that the modifications are correct, click Stage this change on the right of the file name, which is equivalent to running the git add command. The file enters the Staged state. Enter the message to be committed in the lower left corner and click Commit that is equivalent to running the git commit command.

Figure 7 Committing modifications

On the History tab page, view the committing status.

Figure 8 Checking whether the committing is successful

Click the push icon, which is equivalent to running the git push command, to push the code to the GitHub repository. After the pushing is successful, the message "Successfully completed" is displayed. If the token used for OAuth authentication has expired, a dialog box is displayed asking you to enter the user token or account information. Enter the information as prompted. This section describes the authorization using a personal access token. If you use a password for authorization but the password becomes unavailable, perform the operations described in What Do I Do If the Git Plug-in Password Is Invalid?

Figure 9 Pushing code to the GitHub repository

After the preceding operations are complete, on the History tab page of the JupyterLab Git plug-in page, you can see that origin/HEAD and origin/master point to the latest push. In addition, you can find the corresponding information in the committing records of the GitHub repository.