[Git] Fork한 repository 최신으로 동기화

2021. 2. 3. 15:44Dev. etc

Fork 한 repository를 최신으로 동기화시켜야 할 때가 있다. 작업 전에 꼭 최신으로 동기화 한 후 작업을 해 주어야 한다.

 

  • Open Source에 단발성이 아닌 지속적으로 contribution 하려 할 때
  • 수정해서 사용하기 위해 fork해온 원본 repository에서 업데이트 된 부분을 받아 올 때
  • 협업 프로젝트 진행 시에

 

이를 위해서는 먼저 원본 repository를 remote repository로 추가해야 한다.


Fork해온 repository의 remote repository 확인을 위해 다음 명령어를 입력한다.
하게 되면 아직은

$ git remote -v
origin  https://github.com/yuseon-Lim/hsp-narehajae.git (fetch)
origin  https://github.com/yuseon-Lim/hsp-narehajae.git (push)

이렇게만 보일 것이다.

 

여기에 동기화 해 오고 싶은 원본 repository를 upstream이라는 이름으로 추가한다.(upstream이 아닌 다른 이름도 가능하다. 하지만 보통 이렇게 쓴다.)

$ git remote add upstream <원본 repository url>

 ↓ url 확인하는 방법

더보기

repository의 주소는 github 페이지 오른쪽 위에 있다.

주소 옆에 복사 버튼을 누르면 복사된다.

 

이렇게 한 후에 upstream repository가 제대로 추가 되었는지 확인한다.

$ git remote -v
origin  https://github.com/yuseon-Lim/hsp-narehajae.git (fetch)
origin  https://github.com/yuseon-Lim/hsp-narehajae.git (push)
upstream        <원본 repository url> (fetch)
upstream        <원본 repository url> (push)

이제 fetch명령어를 통해 upstream repository의 내용을 불러온다.

$ git fetch upstream
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 3), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 1.81 KiB | 92.00 KiB/s, done.
From <원본 repository url>
   bc1ce48..8547bbd  master     -> upstream/master

나의 local master branch로 checkout한다. ( 최근의 github의 default branch가 main으로 바뀌었는데, 바뀐 후에 생성한 repository는 master대신 main으로 적어주자. )

$ git checkout master
Switched to branch 'master'
M       .eslintcache
M       src/App.js
M       src/components/Category.js
M       src/components/Products.js
M       src/components/Shop.js
M       src/home.css
Your branch is up to date with 'origin/master'.

이제 upstream repository의 master branch (혹은 원하는 branch) 로부터 나의 local master branch로 merge한다.

$ git merge upstream/master
Updating 3c867e8..8547bbd
Fast-forward
 .eslintcache | 1 -
 .gitignore   | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
 delete mode 100644 .eslintcache

여기까지만 하면 나의 local repository만 변경사항이 적용되므로 fork 한 repository에도 push 해주면 된다.

$ git push origin master

 

Reference

https://json.postype.com/post/210431
https://lelecoder.com/142

반응형