Fork 한 Repository 최신 상태로 동기화하기

김현우·2021년 9월 29일
0

Fork한 레포를 수정할 때

오픈소스에 기여하거나 타인이 올린 코드 혹은 파일을 수정하여 올려주는 등 여러가지 이유로 특정 Repository를 Fork 해야할 때가 있다.

하지만 다른사람의 Repository를 자신의 저장소로 clone한 후에 그 사람의 Repository의 변화가 생기면 나중에 Pull Request를 보냈을 때 충돌이 발생할 수 있다.

이를 방지하기 위해 Fork된 Repository를 최신 상태로 유지할 필요성이 있다.

아래의 flow를 따라 진행하여 fork한 repository를 최신 상태로 만들어보자.

1. fork 해온 repository 에서 remote repository 확인

$ git remote -v
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)

2. 동기화해오고 싶은 원본 repository 를 upstream 이라는 이름으로 추가

$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

3. upstream repository 추가 여부 확인

$ git remote -v
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

4. fetch 로 upstream repository 를 불러오기

$ git fetch upstream

5. 나의 local master branch 로 checkout

$ git checkout master

6. upstream repository 의 master branch (혹은 원하는 branch) 로부터 merge

$ git merge upstream/master

7. push 로 remote repository 에도 적용

$ git push origin master

8. upstream 삭제

$ git remote rm upstream
profile
두려워 말라, 놀라지 말라, 굳세게 하리라

0개의 댓글