개발입문/개발환경 세팅

Google Colab 에서 Github 로 코드 저장하기

haloaround 2022. 8. 15. 13:33

 

 


기존에는 매번 Google Colab 에서 노트북을 만들었다 지웠다 했다. 

1. 누적되는 산출물로 관리하고 싶었다.
내가 어떤 공부를 했는지, 산출물을 관리했는지 확인하기 어려웠다. 
2. 나도 블로그에서 github example code 를 제공해보고 싶었다. 

그래서 나도 Github Repository 에 저장해보기로 했다. 


 

 

1. GitHub 계정 만들기

https://github.com/

 

 

2. Github Repository 생성

저는 Halora 계정에 ds 라는 Private Repository 를 생성했습니다. 

Repository 란 일종의 작업공간입니다. Repository 프로젝트 파일과 각 파일의 수정 히스토리를 담고 있습니다.

https://github.com/halora/ds

 

 

3. Google Colab 작성

- 경로: 구글 드라이브
- 클릭: [new] > [more] > [Google Colabotory]

 

 

4. Colab 에서 Google Drive filepath 활용하기

Google Drive 를 런타임 환경에서 마운트하고, file 을 쓰고 읽는다. 

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

 

Google Drive 기준 현재 working directory 는 /content 이다.  working directory 를 변경할 수도 있다.

import os
os.chdir('/content/drive/MyDrive/DS/data_preprocessing_levelup_allinone/1. 데이터 핸들링/데이터')
!pwd

 

 

5. Copy to Github 

- 경로: File > Save a Copy in Github
Repository, Branch, File path 설정
.ipynb 확장자: jupyter notebook  (as-was: ipython notebook) 

google colab : Copy to GitHub

 

 

6. Github 에서 확인

Hello World Github!
Github Code Tab

 

 

 

7. 다시 Google Colab 편집으로

Github file 에 있는 버튼 [Open in Colab] 을 클릭하여 편집을 이어갈 수 있다.

매번 Connection 을 붙을 때 마다 Google Drive 인증을 해주어야 한다.