Pra*_*nth 43 google-colaboratory
有没有推荐的方法将git与colab集成?
例如,是否可以处理谷歌源存储库或类似的代码?
谷歌驱动器和云存储都不能用于git功能.
所以我想知道是否有办法继续这样做?
Bob*_*ith 28
git安装在计算机上,您可以使用它!来调用shell命令.
例如,要克隆git存储库:
!git clone https://github.com/fastai/courses.git
Run Code Online (Sandbox Code Playgroud)
这是一个完整的示例,它克隆存储库并加载存储在其中的Excel文件. https://colab.research.google.com/notebook#fileId=1v-yZk-W4YXOxLTLi7bekDw2ZWZXWW216
小智 22
如果要克隆私有存储库,最快的方法是:
!git clone https://username:password@github.com/username/repository.git
Far*_*han 12
\n\n2021 年 9 月更新 \xe2\x80\x94 出于安全原因,现在 github 使用时已弃用密码。请使用
\nPersonal Access Token\xe2\x80\x94 转至 github.com -> 设置 -> 开发人员设置 -> 个人访问令牌并生成用于所需目的的令牌。使用此密码代替您的密码来完成本教程中提到的所有任务!
有关更多详细信息,您还可以参阅我在 Medium 上的文章:https://medium.com/geekculture/using-git-github-on-google-colaboratory-7ef3b76fe61b
\n没有一个答案提供像这样直接的答案:
\n也许这就是您正在寻找的答案..
\n适用于公共和私人存储库的 colab,并且不更改/跳过任何步骤:(替换全部{vars})
TL;DR 完整流程:
\n!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git\n%cd /content/{destination_repo_username}\n\n!git config --global user.name "{your_username}"\n!git config --global user.email "{your_email_id}"\n!git config --global user.password "{your_password}"\nRun Code Online (Sandbox Code Playgroud)\n进行更改然后运行:
\n!git add .\n!git commit -m "{Message}"\n!git push\nRun Code Online (Sandbox Code Playgroud)\n!git clone https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git\nRun Code Online (Sandbox Code Playgroud)\n%cd使用jupyter 笔记本的line magic 命令将目录更改为 {destination_repo_username} 。
%cd /content/{destination_repo_username}\nRun Code Online (Sandbox Code Playgroud)\n健全性检查,看看一切是否完美!
\n!git pull\nRun Code Online (Sandbox Code Playgroud)\n如果克隆后未对远程 git 存储库进行任何更改,则显示的输出应如下:
\nAlready up to date.\nRun Code Online (Sandbox Code Playgroud)\n同样检查暂存/未暂存更改的状态。
\n!git status\nRun Code Online (Sandbox Code Playgroud)\n它应该显示此内容,并选择默认分支:
\nOn branch main\nYour branch is up to date with \'origin/main\'.\n\nnothing to commit, working tree clean\nRun Code Online (Sandbox Code Playgroud)\n检查您之前在存储库上所做的提交:
\n!git log -n 4\nRun Code Online (Sandbox Code Playgroud)\n输出 Git 提交 ID 和日志:
\ncommit 18ccf27c8b2d92b560e6eeab2629ba0c6ea422a5 (HEAD -> main, origin/main, origin/HEAD)\nAuthor: Farhan Hai Khan <njrfarhandasilva10@gmail.com>\nDate: Mon May 31 00:12:14 2021 +0530\n\n Create README.md\n\ncommit bd6ee6d4347eca0e3676e88824c8e1118cfbff6b\nAuthor: khanfarhan10 <njrfarhandasilva10@gmail.com>\nDate: Sun May 30 18:40:16 2021 +0000\n\n Add Zip COVID\n\ncommit 8a3a12863a866c9d388cbc041a26d49aedfa4245\nAuthor: khanfarhan10 <njrfarhandasilva10@gmail.com>\nDate: Sun May 30 18:03:46 2021 +0000\n\n Add COVID Data\n\ncommit 6a16dc7584ba0d800eede70a217d534a24614cad\nAuthor: khanfarhan10 <njrfarhandasilva10@gmail.com>\nDate: Sun May 30 16:04:20 2021 +0000\n\n Removed sample_data using colab (testing)\nRun Code Online (Sandbox Code Playgroud)\n从本地存储库目录进行更改。
\n这些可能包括编辑、删除、编辑。
\nfrom google.colab import drive\ndrive.mount(\'/content/gdrive\')\nRun Code Online (Sandbox Code Playgroud)\nimport shutil\n\n# For a folder:\nshutil.copytree(src_folder,des_folder)\n\n# For a file:\nshutil.copy(src_file,des_file)\n\n# Create a ZipFile\nshutil.make_archive(archive_name, \'zip\', directory_to_zip)\nRun Code Online (Sandbox Code Playgroud)\n告诉 Git 你是谁?
\n!git config --global user.name "{your_username}"\n!git config --global user.email "{your_email_id}"\n!git config --global user.password "{your_password}"\nRun Code Online (Sandbox Code Playgroud)\n检查远程 url 是否设置和配置正确:
\n!git remote -v\nRun Code Online (Sandbox Code Playgroud)\n如果配置正确,它应该输出以下内容:
\norigin https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git (fetch)\norigin https://{your_username}:{your_password}@github.com/{destination_repo_username}/{destination_repo_projectname}.git (push)\nRun Code Online (Sandbox Code Playgroud)\n你知道该做什么。
\n!git add .\n!git commit -m "{Message}"\n!git push\nRun Code Online (Sandbox Code Playgroud)\n享受!
\n小智 10
为了保护您的帐户用户名和密码,您可以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
Run Code Online (Sandbox Code Playgroud)
将私有存储库克隆到 google colab :
生成令牌:
Settings -> Developer settings -> Personal access tokens -> Generate new token
Run Code Online (Sandbox Code Playgroud)
复制令牌并克隆存储库(相应地替换用户名和令牌)
!git clone https://username:token@github.com/username/repo_name.git
Run Code Online (Sandbox Code Playgroud)
使用git同步colab与github或gitlab的三个步骤。
生成私钥-公钥对。将私钥复制到系统 clibboard 以用于步骤 2。将公钥粘贴到 github 或 gitlab 中。
在 Linux 中,ssh-keygen 可用于在 ~/.ssh 中生成密钥对。生成的私钥在文件 id_rsa 中,公钥在文件 id_rsa.pub 中。
在 Colab 中,执行
key = \
'''
paste the private key here
(your id_rsa or id_ecdsa file in the .ssh directory, e.g.
-----BEGIN EC PRIVATE KEY-----
M..............................................................9
...............................................................J
..................................==
-----END EC PRIVATE KEY-----
'''
! 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
# test setup
! ssh -T git@github.com
# if you see something like "Hi ffreemt! You've successfully
# authenticated, but GitHub does not provide shell access."
# you are all set. You can tweak .ssh/config for multiple github accounts
Run Code Online (Sandbox Code Playgroud)
像往常一样使用 git 拉/推。
相同的想法可用于 rsync(或 ssh)colab 和 HostA 之间,只需稍作更改:
生成私钥-公钥对。将私钥复制到系统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
Run Code Online (Sandbox Code Playgroud)
HostA >> /root/.ssh/known_hosts 似乎不适用于 IP。
您可以使用ssh协议将专用存储库与colab连接
在您的本地计算机上生成ssh密钥对,不要忘了将
短语保留为空,请查看本教程。
上载到colab,查看以下截图
from google.colab import files
uploaded = files.upload()
将ssh kay对移动到/ root并连接到git
! rm -rf /root/.ssh/*! mkdir /root/.ssh ! tar -xvzf ssh.tar.gz ! cp ssh/* /root/.ssh && rm -rf ssh && rm -rf ssh.tar.gz
! chmod 700 /root/.ssh ! ssh-keyscan gitlab.com >> /root/.ssh/known_hosts! chmod 644 /root/.ssh/known_hosts ! git config --global user.email "email"! git config --global user.name "username" ! ssh git@gitlab.com验证您的私有存储库,请检查此每个存储库部署密钥。
请! git@gitlab.com:{account}/{projectName}.git
注意:要使用push,您必须授予对
git服务器进行身份验证的公共ssh密钥的写入权限。
You can almost use this link: https://qiita.com/Rowing0914/items/51a770925653c7c528f9
as a summary of the above link you should do this steps:
1- connect your google colab runtime to your Google Drive using this commands:
from google.colab import drive
drive.mount('/content/drive')
Run Code Online (Sandbox Code Playgroud)
It would need a authentication process. Do whatever it needs.
2- Set current directory the path you want to clone the Git project there:
in my example:
path_clone = "drive/My Drive/projects"
!cd path_clone
Run Code Online (Sandbox Code Playgroud)
don't forget to use ! in the beginning of cd command.
3- Clone the Git project:
!git clone <Git project URL address>
Run Code Online (Sandbox Code Playgroud)
now you would have the cloned Git project in projects folder in you Google Drive (which is also connected to your Google Colab runtime machine)
4- Go to your Google Drive (using browser or etc) and then go to the "projects" folder and open the .ipynb file that you want to use in Google Colab.
5- Now you have Google Colab runtime with the .ipynb that you wanted to use which is also connected to your Google Drive and all cloned git files are in the Colab runtime's storage.
Note:
1- Check that your Colab runtime is connected to Google Drive. If it's not connected, just repeat the step #1 above.
2- Double check by using "pwd" and "cd" commands that the current directory is related to the cloned git project in google Drive (step #2 above).
The very simple and easy way to clone your private github repo in Google colab is as below.
import os
from getpass import getpass
import urllib
user = input('User name: ')
password = getpass('Password: ')
password = urllib.parse.quote(password) # your password is converted into url format
repo_name = input('Repo name: ')
cmd_string = 'git clone https://{0}:{1}@github.com/{0}/{2}.git'.format(user, password, repo_name)
os.system(cmd_string)
cmd_string, password = "", "" # removing the password from the variable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43974 次 |
| 最近记录: |