首页 文章

与谷歌Colab一起使用Git的方法

提问于
浏览
14

有没有推荐的方法将git与colab集成?

例如,是否可以处理谷歌源存储库或类似的代码?

谷歌驱动器和 Cloud 存储都不能用于git功能 .

所以我想知道是否有办法继续这样做?

6 回答

  • 1

    git 已安装在计算机上,您可以使用 ! 来调用shell命令 .

    例如,要克隆 git 存储库:

    !git clone https://github.com/fastai/courses.git
    

    这是一个完整的示例,它克隆存储库并加载存储在其中的Excel文件 . https://colab.research.google.com/notebook#fileId=1v-yZk-W4YXOxLTLi7bekDw2ZWZXWW216

  • 0

    如果要克隆私有存储库,最快的方法是:

    !git clone https://username:password@github.com/username/repository.git

  • 2

    您可以使用ssh协议将您的私有存储库与colab连接

    • 在本地计算机上生成ssh密钥对,不要忘记保留
      释义为空,检查这个tutorial .

    • 将其上传至colab,请检查以下内容screenshot

    from google.colab import files
    uploaded = files.upload()

    • 将ssh kay对移动到/ root并连接到git

    • 删除以前的ssh文件
      ! rm -rf /root/.ssh/*
      ! mkdir /root/.ssh

    • 解压缩你的ssh文件
      ! tar -xvzf ssh.tar.gz

    • 将其复制到root
      ! cp ssh/* /root/.ssh && rm -rf ssh && rm -rf ssh.tar.gz ! chmod 700 /root/.ssh

    • 添加你的git服务器,例如gitlab作为ssh已知主机
      ! ssh-keyscan gitlab.com >> /root/.ssh/known_hosts
      ! chmod 644 /root/.ssh/known_hosts

    • 设置你的git帐户
      ! git config --global user.email "email"
      ! git config --global user.name "username"

    • 终于连接到你的git服务器了
      ! ssh git@gitlab.com

    • 验证您的私人存储库,请检查Per-repository deploy keys .

    • 使用 ! git@gitlab.com:{account}/{projectName}.git
      注意:要使用push,您必须为其提供写访问权限
      您使用git服务器验证的公共ssh密钥 .

  • 12

    使用git将colab与github或gitlab同步的三个步骤 .

    • 生成私钥 - 公钥对 . 将私钥复制到系统clibboard以在步骤2中使用 . 根据需要将公钥粘贴到github或gitlab .

    在Linux中,ssh-keygen可用于在〜/ .ssh中生成密钥对 . 生成的私钥位于文件id_rsa中,公钥位于文件id_rsa.pub中 .

    • 在Colab中,执行

    key = \ ''' paste the private key here ''' ! mkdir -p /root/.ssh with open(r'/root/.ssh/id_rsa', 'w', encoding='utf8') as fh: fh.write(key) ! chmod 600 /root/.ssh/id_rsa ! ssh-keyscan github.com >> /root/.ssh/known_hosts

    • 像往常一样使用git来拉/推 .

    同样的想法可以用于同步和同时具有微小变化的HostA的rsync:

    • 生成私钥 - 公钥对 . 将私钥复制到系统clibboard以在步骤2中使用 . 将公钥粘贴到HostA中.ssh中的authorized_keys .

    • 在Colab中,执行

    key = \ ''' paste the private key here ''' ! mkdir -p /root/.ssh with open(r'/root/.ssh/id_rsa', 'w', encoding='utf8') as fh: fh.write(key) ! chmod 600 /root/.ssh/id_rsa ! ssh -oStrictHostKeyChecking=no root@HostA hostnam # ssh-keyscan HostA >> /root/.ssh/known_hosts does not seem to work with IP.

    • 使用rsync像往常一样同步colab和HostA文件 .
  • 4

    为了保护您的帐户用户名和密码,您可以使用 getPass 并在shell命令中连接它们:

    from getpass import getpass
    import os
    
    user = getpass('BitBucket user')
    password = getpass('BitBucket password')
    os.environ['BITBUCKET_AUTH'] = user + ':' + password
    
    !git clone https://$BITBUCKET_AUTH@bitbucket.org/{user}/repository.git
    
  • 1

    使用以下方法安装驱

    from google.colab import drive
    drive.mount('/content/drive/')
    

    然后:

    %cd /content/drive/
    

    要克隆驱动器中的存储库

    !git clone <github repo url>
    

    从repo访问其他文件(例如:helper.py是repo中的另一个文件):

    import imp 
    helper = imp.new_module('helper')
    exec(open("drive/path/to/helper.py").read(), helper.__dict__)
    

相关问题