본문 바로가기

Study/IT 분야 연구

GitHub에서 특정 파일만 다운로드 받는 방법

반응형

본 페이지에서는 GitHub에서 특정 파일만 다운로드 받는 방법에 대해서 기술하고자 합니다. 

 

개요

 

많은 소프트웨어 개발자들이 형상관리를 위해 깃헙(GitHub)를 사용하고 있습니다. 저 역시 직장에서는 프로젝트 멤버들과 GitHub를 이용해서 협업하고, 퇴근후에는 개인적으로 관심이 있는 분야에 대해서 공부하면서 GitHub를 소스코드 및 웹페이지 저장 공간으로(말 그대로 repository)로 사용하고 있습니다. 

 

대규모로 협업을 진행하는 경우는 repositoy 전체를 clone해서 진행하면 되겠지만, 간혹 특정 파일만 공유가 필요한 경우가 있습니다. 특정 파일만 GitHub에서 다운로드가 가능하냐고요? 당연히 가능합니다. 

 

 

특정 파일만 다운로드 받는 방법 #1 

 

우선 GitHub 페이지에 접속해서 다운로드하고자하는 파일을 URL을 가져옵니다. 예제로 사용한 파일을 라즈베리파이의 GPO를 제어를 통해서 파워 릴레이를 제어하는 파이선 코드입니다. 

 

 

URL을 복사하고 curl 명령 혹은 wget 명령을 통해서 다운로드를 해보겠습니다. 

 

$ wget https://github.com/boyinblue/raspberry/blob/main/pwr/pwr_off.py

 

위의 명령으로 마치 해당 파일만 정상적으로 다운로드 된 것처럼 보입니다. 

 

$ wget https://github.com/boyinblue/raspberry/blob/main/pwr/pwr_off.py
--2022-03-05 05:20:34--  https://github.com/boyinblue/raspberry/blob/main/pwr/pwr_off.py
Resolving github.com (github.com)... 52.78.231.108
Connecting to github.com (github.com)|52.78.231.108|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘pwr_off.py’

pwr_off.py                              [ <=>                                                              ] 186.28K  --.-KB/s    in 0.03s   

2022-03-05 05:20:35 (5.28 MB/s) - ‘pwr_off.py’ saved [190752]

 

하지만 실제로 파일을 열어보면 파이선 스크립트 파일이 아니라, GitHub의 웹페이지가 다운로드 된 것을 확인할 수 있습니다. 

 

<!DOCTYPE html>
<html lang="en" data-color-mode="auto" data-light-theme="light" data-dark-theme="dark" >
  <head>
    <meta charset="utf-8">
  <link rel="dns-prefetch" href="https://github.githubassets.com">
  <link rel="dns-prefetch" href="https://avatars.githubusercontent.com">
  <link rel="dns-prefetch" href="https://github-cloud.s3.amazonaws.com">
  <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">
  <link rel="preconnect" href="https://github.githubassets.com" crossorigin>
  <link rel="preconnect" href="https://avatars.githubusercontent.com">

 

 

특정 파일만 다운로드 받는 방법 #2 

 

위에서 소개한 방법으로 다운로드시에 raw 파일을 받을 수 없기 때문에 이번에는 raw 버튼을 누른 후에 다운로드를 해보겠습니다. 

 

 

위와 같이 raw 버튼을 누른 후에 나오는 URL로 다운로드하면 해당 파일만 정상적으로 잘 받아집니다. 

 

$ wget https://raw.githubusercontent.com/boyinblue/raspberry/main/pwr/pwr_off.py

 

wget으로 다운로드한 이후에 해당 파일을 vi로 열어보면 파이선 스크립트가 제대로 받아진 것을 확인하실 수 있습니다. 

 

$ wget https://raw.githubusercontent.com/boyinblue/raspberry/main/pwr/pwr_off.py
--2022-03-05 05:30:21--  https://raw.githubusercontent.com/boyinblue/raspberry/main/pwr/pwr_off.py
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 132 [text/plain]
Saving to: ‘pwr_off.py’

pwr_off.py                          100%[=================================================================>]     132  --.-KB/s    in 0s      

2022-03-05 05:30:22 (991 KB/s) - ‘pwr_off.py’ saved [132/132]

$ cat pwr_off.py
import RPi.GPIO as GPIO

#GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)
GPIO.output(14, True) #Low Active

 

그럼 2개의 경로가 어떻게 차이나는지 설명을 드리겠습니다. 

 

구분 실제 경로
GitHub 웹페이지 주소 https://github.com/boyinblue/raspberry/blob/main/pwr/pwr_off.py
GitHub raw file 주소 https://raw.githubusercontent.com/boyinblue/raspberry/main/pwr/pwr_off.py

 

웹브라우저에 표시되는 html 형식의 파일을 https://github.com/boyinblue/raspberry/blob 경로로 시작됩니다. 

반면, raw 파일은 https://raw.githubusercontent.com/boyinblue/raspberry 경로로 시작됩니다. 

 

이것을 잘 응용하면 특정 user의 특정 repository 내부의 특정 파일만 다운로드 받을 수 있는 URL을 스스로 조합해낼 수 있습니다. 

 

 

[raw 파일 URL 형식]

"raw.githubusercontent.com/user명/repo명/branch명/경로"

 

예를들어, 제 GitHub 저장소의 다른 경로의 파일(pwr/pwr_on.py)을 다운로드 하려면 아래와 같은 URL을 사용하면 됩니다. 

https://raw.githubusercontent.com/boyinblue/raspberry/main/pwr/pwr_on.py 

 

위와 같은 형식으로 다운로드를 시도해봤더니, 역시나 정상적으로 잘 다운로드가 되네요. 

 

 

 

결론

 

GitHub에서 특정 파일만 다운로드가 가능합니다. 다만, GitHub 웹페이지의 주소를 그대로 다운로드하면 html 형식의 파일로 다운로드 되므로, raw 버튼을 눌러서 다운로드하시거나 "raw.githubusercontent.com/user명/repo명/branch명/경로"의 형식으로 URL을 조합해서 다운로드 하시면 됩니다. 

 

이상입니다 

 

반응형