[Git]fork한 repository update하기

Yeshin Lee
4 min readJan 1, 2021

--

peer-programming 방식으로 토이 프로젝트를 진행하면서, 매번 유용하게 사용했던 Git 명령어에 대해 복습한다.

  1. 현재 remote된 repository를 확인한다.
git branch -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 http://github.com/ORIGINAL_OWNER_REPOSITORY.git

3. 제대로 추가되었는지 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)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

4. 원본 repository에서 변경된 commit을 가져온다. (이는 fork된 repository의 upstream/main에 저장된다.)

git fetch upstream
> remote: Counting objects: 75, done.
> remote: Compressing objects: 100% (53/53), done.
> remote: Total 62 (delta 27), reused 44 (delta 9)
> Unpacking objects: 100% (62/62), done.
> From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
> * [new branch] main -> upstream/main

5. 변경 사항을 저장할 main 브랜치로 변경한다.

git checkout main
> Switched to branch 'main'

※ 만약 다음과 같은 메세지가 나온다면, 이미 main 브랜치에서 작업 중이었으니 넘어가도 된다

Already on 'main'
Your branch is up to date with 'origin/main'.

6. upstream/main 브랜치의 변경사항을 병합한다.

$ git merge upstream/main
> Updating a422352..5fdff0f
> Fast-forward
> README | 9 -------
> README.md | 7 ++++++
> 2 files changed, 7 insertions(+), 9 deletions(-)
> delete mode 100644 README
> create mode 100644 README.md

7. 나의 원격 저장소에 push한다.

git push

그동안 git status-add-commit-push(혹은 pull)만 줄곧 사용해온 내게 Git에 대해 좀 더 자세하게 알 수 있었던 기회였다.

[Reference]

--

--

Yeshin Lee
Yeshin Lee

No responses yet