Jan*_*rer 7 git gitlab google-colaboratory
我的问题是我想将 Google Colab 实例与 Gitlab 项目连接,但 SSH 和 HTTPS 似乎都不起作用。从错误消息中,我怀疑 Colab 中存在与设置相关的问题。也许我必须允许 Colab 连接到 Gitlab 并将其放在某个地方的白名单中?
在 Colab 中的 Notebook 中运行以下 shell 命令,同时位于“/content”目录中
git config --global user.name "mr_bla"
git config --global user.email "bla@wbla.bla"
git clone https://gitlab.com/mr_bla/mr_blas_project.git
Run Code Online (Sandbox Code Playgroud)
导致以下错误消息:
Cloning into 'mr_blas_project'...
fatal: could not read Username for 'https://gitlab.com': No such device or address
Run Code Online (Sandbox Code Playgroud)
我已经按照习惯生成了 SSH 密钥,但是 SSH 检查
ssh -vvvT git@gitlab.com:mr_bla/mr_blas_project.git
Run Code Online (Sandbox Code Playgroud)
失败,导致以下错误:
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "gitlab.com:mr_bla/mr_blas_project.git" port 22
ssh: Could not resolve hostname gitlab.com:mr_bla/mr_blas_project.git: Name or service not known
Run Code Online (Sandbox Code Playgroud)
尝试使用 SSH 方式克隆项目也不起作用:
git clone git@gitlab.com:mr_bla/mr_blas_project.git
Run Code Online (Sandbox Code Playgroud)
结果是:
Cloning into 'mr_blas_project'...
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Run Code Online (Sandbox Code Playgroud)
Google Colab 实例运行以下操作系统:
cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
Run Code Online (Sandbox Code Playgroud)
我已经检查了许多其他问题,但没有成功:
Gon*_*olo 12
以下是我使用GitLab我的Google Colab 笔记本进行持续版本控制的工作流程(我想使用 GitHub 会非常相似)。
我使用来自 GitLab 的个人访问令牌,以便能够在私有存储库中使用它们
在 GitLab 中创建个人访问令牌
然后,为了让 Colab 与 GitLab 交互,您必须将存储库的.git 文件夹存储在Google Drive 文件夹中,以便它在 Colab会话之间保持不变
假设您在 Gdrive 中有一个文件夹,其中包含一些您想要使用 Git 进行版本控制的文件:
在 GColab 容器文件系统中挂载GoogleDrive 。假设您将其安装在Colab 容器文件系统内的/content/myfiles上。您必须在笔记本中执行此行(这会输出一个 URL,您必须访问该 URL,才能为 Colab 实例提供对 Google Drive 的 OAuth2 访问权限)。在单元格中只需运行:
from google.colab import drive
drive.mount(/content/myfiles)
Run Code Online (Sandbox Code Playgroud)
安装后更改目录,使用 %cd执行魔法命令(使用 !cd 不起作用,每个 shell 命令都在临时子 shell 中执行,因此它不是持久的)
%cd "/content/myfiles/MyDrive/Folder1/Folder2"
!pwd
Run Code Online (Sandbox Code Playgroud)
一旦你初始化了 git 存储库(这只是第一次,因为所有这些都是在你的 Google Drive 中完成的,这意味着它是一个将在会话之间持续存在的存储库,如果不是一旦你离开 Google Colab 会话的话)将被删除)。
!git init
Run Code Online (Sandbox Code Playgroud)
现在,您必须在本地配置推/拉时所需的典型 git 参数(因此它存储在 .git 文件夹中)(同样,这必须在第一次完成):
!git config --local user.email your_gitlab_mail@your_domain.com
!git config --local user.name your_gitlab_name
Run Code Online (Sandbox Code Playgroud)
现在使用之前创建的 PAT添加遥控器(这也是第一次完成):
关键点:远程 URL 格式(必须通过 HTTPs)取决于 Gitlab 项目(存储库)是否位于组/子组下:
在一个组下(可能是 /group/subgroup1/subgroup2/.../project.git 或只是 /group/projec.git)
!git remote add origin https://<pat_name>:pat_code>@gitlab.com/group_name/subgroup1/project_name.git
Run Code Online (Sandbox Code Playgroud)
不在一个组下
!git remote add origin https://<pat_name>:pat_code>@gitlab.com/your_gitlab_username/project_name.git
Run Code Online (Sandbox Code Playgroud)
现在,git 存储库是在Google Drive 文件夹中配置的 ,而不仅仅是在文件系统容器中,因此除了所有常用的 git 命令之外,您还可以拉/推
!git add .
!git commit -m"First commit"
!git push -u origin master
Run Code Online (Sandbox Code Playgroud)
第一次完成此操作后,为了使用 Git 和 GitLab 保持“版本控制”(我再次猜测它与 GitHub 非常相似,因为 GitLab 的组功能对我来说非常有价值)MyDrive/Folder1/ 中的文件在Folder2中,您应该创建一个笔记本,在编辑该文件夹中的其他文件时安装您想要的Google Drive和git命令。
我想说最好的方法是有一个参数化笔记本来检查这是否是第一次进行 git 初始化等,如果不是只是添加/提交/推送到 GitLab 存储库。
对于仅克隆到 Container FS(或 Google Drive,如果已安装),只需使用上面解释的与 git clone 相同的远程:
一组下
!git clone https://<pat_name>:<pat_code>@gitlab.com/group_name/project_name.git
Run Code Online (Sandbox Code Playgroud)
不在一个组下
!git clone https://<pat_name>:<pat_code>@gitlab.com/gitlab_user_name/project_name.git
Run Code Online (Sandbox Code Playgroud)
编辑:我添加了我创建的笔记本,这样您就可以使用它在 Colab 和 GitLab 之间进行交互(称为Gitlab_Colab_Interaction.ipynb),这样您就可以直接从 Colab 使用它:
进口
import os
from pathlib import Path
Run Code Online (Sandbox Code Playgroud)
参数
# Paths
container_folder_abspath = Path('/content/myfiles')
gdrive_subfolder_relpath = Path('MyDrive/Colab Notebooks/PathTo/FolderYouWant') # No need to scape the space with pathlib Paths
gitlab_project_relpath = Path('/group_name/subgroup1/YourProject.git')
# Personal Access Token
PAT_name = 'my_pat_name'
PAT_code = 'XXXX_PAT_CODE_XXXXX'
Run Code Online (Sandbox Code Playgroud)
安装驱动器
from google.colab import drive
drive.mount(str(container_folder_abspath))
fullpath = container_folder_abspath / gdrive_subfolder_relpath # Path objects with the operator /
%cd $fullpath
!pwd
Run Code Online (Sandbox Code Playgroud)
初始化(或不初始化)
initialization = True
for element in fullpath.iterdir():
if element.is_dir():
if element.name == '.git':
initialization = False
print('Folder already initialized as a git repository!')
gitlab_url = 'https://' + PAT_name + ':' + PAT_code + '@gitlab.com/' + str(gitlab_project_relpath)
if initialization:
!git init
!git config --local user.email your_gitlab_mail@yourmail.com
!git config --local user.name your_gitlab_user
!git remote add origin $gitlab_url # Check that PATs are still valid
!echo "GitLab_Colab_Interaction.ipynb" >> ".gitignore" # To ignore this file itself if it is included in the folder
else:
print("### Current Status ###")
!git status
print("\n\n### Git log ###")
!git log
Run Code Online (Sandbox Code Playgroud)
Git 命令
# Git Add
!git add *.ipynb # For example to add just the modified notebooks
# Git Commit
!git commit -m "My commit message"
# Git Push
!git push -u origin master # As of now Gitlab keeps using the name master
Run Code Online (Sandbox Code Playgroud)
如果是私人回购。您可以使用GitLab 部署令牌,也可以使用GitLab 个人访问令牌。然后你就可以
git clone https://<deploy_username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git
Run Code Online (Sandbox Code Playgroud)
请注意,您可能不希望上述代码<deploy_token>在您的笔记本中公开,您可以通过将其放入安装在驱动器上的可执行脚本中来隐藏它作为示例,或者我认为您可以隐藏代码。