본문 바로가기
개발 관련 개념들

Github 리파지토리를 Re-fork하고 싶을 때!! (Upstream, Origin, Syncing)

by 코곰 2021. 4. 26.

그룹 프로젝트를 하면서

리파지토리를 판 분의 깃헙에서 fork를 해왔는데 (나의 repositories 목록에도 보이게 하고 싶어서!)

 

fork해온 리파지토리에서는

fork한 시점 이후로 원래 오리지널 리파지토리, 즉 upstream에 반영한 변화들이 나타나지 않아서

"Re-forking"을 해야하나 생각했었다.

 

잠깐 짚고 넘어가기

Upstream - 대체로 Clone이나 Fork를 할 때, 그 액션을 해오는 오리지널 리파지토리를 의미한다.

Origin - Clone이나 Fork를 해와서 만들어진 나의 로컬 리파지토리를 의미한다.

 

그렇기 때문에 Clone/Fork를 해온 오리지널 리파지토리의 변화를 추적 / 거기에 변화를 반영하고 싶다면, Upstream을 따로 설정해줘야 한다.

 

출처

내용은 다음의 링크들에 기반을 둔다.

stackoverflow.com/questions/7244321/how-do-i-update-or-sync-a-forked-repository-on-github

 

How do I update or sync a forked repository on GitHub?

I forked a project, applied several fixes and created a pull request which was accepted. A few days later, another change was made by another contributor. So my fork doesn't contain that change. Ho...

stackoverflow.com

docs.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork

 

Syncing a fork - GitHub Docs

Sync a fork of a repository to keep it up-to-date with the upstream repository. Before you can sync your fork with an upstream repository, you must configure a remote that points to the upstream repository in Git. Open TerminalTerminalGit Bash. Change the

docs.github.com

 

Upstream 설정하기

git remote add upstream <오리지널 리파지토리 URL>

 

이제 git remote -v를 해주면, origin 과 upstream 리파지토리 정보를 볼 수 있다.

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
upstream  https://github.com/otheruser/repo.git (fetch)
upstream  https://github.com/otheruser/repo.git (push)

 

Upstream에서 git pull하기

origin 및 upstream이 잘 설정되었는 지 위의 스텝을 통해 먼저 살핀 후 실행하자.

 

1. Fetch하기

Upstream 레포를 fetch하면 거기에 있는 branches와 commits까지 불러올 수 있다!

fetch후에는 그것들이 special branches로 내 로컬 레포 쪽에 저장된다.

 

$ git fetch upstream
# Grab the upstream remote's branches
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/otheruser/repo
* [new branch] master -> upstream/master

fetch 후에 branches를 확인해보면 upstream/master 로칼 브랜치가 생긴 것을 볼 수 있다.

 

$ git branch -va
# List all local and remote-tracking branches
* master                          a422352 My local commit
  remotes/origin/HEAD       -> origin/master
  remotes/origin/master      a422352 My local commit
  remotes/upstream/master 5fdff0f Some upstream commit

 

2. Merging

upstream 브랜치를 가져왔으니, 이제 로칼 브랜치와 합쳐주자!

 

$ git checkout master로 우리 로칼의 master branch로 이동해주고

$ git merge upstream/master로 upstream에서 가져온 브랜치를 병합해주는 것이다!

$ git checkout master
# Check out our local master branch Switched to branch 'master'

$ git merge upstream/master
# Merge upstream's master into our own
  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

 

3. 이제 upstream에서 가져온 정보들은 나의 로칼 브랜치와 sync가 되었다.

이후론 나의 remote origin에

$ git status

$ git add/rm

$ git commit 등을 이용해 상태 변화를 추적 / 커밋해주고, 필요에 따라 푸시해주면 된다! 

 

 

ㅎㅎ

 

댓글