Search code examples
pythongitgithubgoogle-colaboratory

How can I copy a file from colab to github repo directly? (It is possible to save the notebook in the Github repo)


How can I save a file generated by colab notebook directly to github repo? It can be assumed that the notebook was opened from the github repo and can be (the same notebook) saved to the same github repo.


Solution

  • Google Colaboratory's integrating with github tends to be lacking, however you can run bash commands from inside the notebook. These allow you to access and modify any data generated.

    You'll need to generate a token on github to allow access to the repository you want to save data to. See here for how to create a personal access token.

    Once you have that token, you run git commands from inside the notebook to clone the repository, add whatever files you need to, and then upload them. This post here provides an overview of how to do it in depth.

    That being said, this approach is kind of cumbersome, and it might be preferable to configure colab to work over an SSH connection. Once you do that, you can mount a folder on the colab instance to a folder on your local machine using sshfs. This will allow you to access the colab as though it were any other folder on your machine, including opening it in your IDE, viewing files in a file browser, and cloning or updating git repositories. This goes more in depth on that.

    These are the best options I was able to identify, and I hope one of them can be made to work for you.