에코프로.AI

[GitHub] Colab에서 GoogleDrive의 파일 GitHub연동 본문

AI Tutorial

[GitHub] Colab에서 GoogleDrive의 파일 GitHub연동

AI_HitchHiker 2024. 11. 7. 18:12

https://miro.medium.com/v2/resize:fit:720/format:webp/1*mM0p_SF47QA74232hkjjNw.png

 

Colab에서 GoogleDrive의 파일을 GitHub에 연동하는 방법을 소개합니다.

이 방법을 사용하면, colab을 사용하여, 다른 환경에서 별도의 설정없이 colab으로 작업이 가능하며,

작업한 colab의 코드를 github로 연동 가능합니다.

1. 준비작업

■ github - token 생성
1. github에 접속
2. 우측의 프로필 이미지 를 클릭한다.
3. "Settings"를 선택
4. 좌측메뉴의 하단에 "Developer settings" 를 선택
5. 좌측메뉴의 "Personal access tokens"의 "Tokens (classic)" 를  선택
6. 우측의 "Generate new token"을 선택 후. "Generate new token (classic)" 을 선택
- "Note"에 적당한 이름 입력 ex) "colab commit 용 키 생성"
- "Expiration" 사용기간 을 설정
- "Select scopes" - repo 체크
7. 하단의 "Generate token" 선택
※ 생성된 Token은 다음에 확인이 안되므로, 저장해 놓는다.
 
■ github - repository path 확인
1. github에 접속
2. 해당하는 repository 선택
3. "Code"를 선택하여, repository path 확인

 

2. 구글드라이브 Mount

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

 

3. 드라이브 내에 생성해둔 폴더로 이동

cd '/gdrive/MyDrive/_github/Python_DataAnalysis'

 

4. git연동 시, 사용할 e-mail,사용자이름 설정

- 연동할 github의 이메일, 사용자계정을 설정

!git config --global user.email "github메일@gmail.com"
!git config --global user.name "github계정"

- 정상적으로 등록되었는지 확인

# 등록여부 확인
!git config --global --list

 

5. github 연동하기

5.1. 현재폴더를 기준으로 github에 연동

현재 폴더를 git으로 관리한다고 설정
 - 처음 1회만 하면 됨

!git init

# git init 취소
# !rm -r .git

 

git 저장소 연결

# github 원격저장소(repository)를 추가합니다.

!git remote add origin https://[토큰값]@github.com/[github계정]/[Repository이름].git

 

git 저장소 확인

# 저장소 확인
!git remote -v

# 저장소 삭제
# !git remote rm origin

 

5.2. github를 기준으로 git clone하여 연동

git clone 하기

!git clone https://[토큰값]@github.com/[github계정]/[Repository이름].git

 

 

6.commit 하기

전체 변경된 파일 commit 

# 현재 변경된 내용을 commit 적용항목에 포함
!git add .
# add 된 항목, commit 처리
!git commit -m "2024.11.13_first commit!"

 

commit 상태 확인

# commit 상태 확인
!git status

 

7. github push하기

# 현재 브랜치를 main으로 설정
!git branch -M main

!git push -u origin main

# 강제 push (local을 강제로 원격으로 적용)
#!git push -f origin main

 

끝~

 

[참고사이트] https://medium.com/analytics-vidhya/how-to-use-google-colab-with-github-via-google-drive-68efb23a42d