はじめに
Google Colablatoryの有料版を契約したので、とりあえず記事の内容を検証するのに頻繁に使っており、Git管理したいとリポジトリを作りpushしたのですが、エラーがでたのでColabのpushの流れも含めて紹介します。
問題
test_colab
というパブリックなリポジトリをGitHubで作成して、以下のコマンドを実行しました。
!git remote add origin https://github.com/jinwatanabe/test_colab.git
!git push origin -u main
すると、以下のようなエラーが発生しました。
fatal: could not read Username for 'https://github.com': No such file or directory
解決方法
!git remote add origin
に続く、リポジトリのHTTPSを、
https://{username}:{password}@github.com/{username}/project.git
のように変更します。
usernameとpasswordはGitHubの自分のアカウントの情報になります。
ユーザー名:jinwatanabe
パスワード: password
の場合は以下のコマンドになります。
# git remote add origin https://{username}:{password}@github.com/{username}/project.git
!git clone https://jinwatanabe:password@github.com/jinwatanabe/test_colab.git
!git push -u origin main
これでうまくいきました。
Colabでgit pushまでのコマンド
pushまでのコマンドをまとめておきます。
!git init
!git config --global user.email "your email"
!git config --global user.name "your user name"
!git add .
!git commit -m "your commit message"
!git branch -M main
!git remote add origin https://{username}:{password}@github.com/{username}/project.git
# ここでfatal: remote origin already exists. とでたら以下のコマンド
# !git remote rm origin
# !git remote add origin https://{username}:{password}@github.com/{username}/project.git
!git push -u origin main
さいごに
git remote rm origin
は毎回調べてる気がしたので、そろそろ覚えたいところです。
Colab便利ですが、環境構築の手間を忘れてしまうので、実際のローカル環境で同じコードを動かそうとしたときにライブラリが入っていなかったりでエラーをたくさんはいてしまいます。便利ですが悩みどころです。