[Git] 다른 프로젝트의 원격 repository에서 특정 branch 내용 merge 하기
강의 수강을 위해 v1 내용을 v2로 옮겨야 하는 상황이 있었는데 git 기능으로 해결했던 내용 기록하고자 하는 내용입니다.
📌 1. 원격 repository 추가 등록
project-v1의 특정 branch(beanie)에서 , project-v2의 특정 branch(beanie)로 merge해서 합병해야 하는 상황입니다.
이를 위해 먼저 옮기고자 하는 내용이 있는 project-v1 원격 branch를 가져와야 합니다.
project-v2> git remote -v origin git@github.com:[account]/project-v2.git (fetch) origin git@github.com:[account]/project-v2.git (push)
현재 remote에 origin 이름으로 project-v2 repository가 등록되어 있습니다. 여기에 project-v1 repository를 다른 remote 이름으로 등록해야 합니다. 그래야 해당 remote 이름으로 fetch를 받아 merge를 해볼 수 있을 겁니다.
project-v2> git remote add temp git@github.com:[account]/project-v1.git project-v2> git remote -v origin git@github.com:[account]/project-v2.git (fetch) origin git@github.com:[account]/project-v2.git (push) temp git@github.com:[account]/project-v1.git (fetch) temp git@github.com:[account]/project-v1.git (push)
temp라는 이름으로 remote가 추가 등록된 것을 확인할 수 있습니다.
📌 2. 전혀 관련 없는 repository merge fatal 이슈
project-v2> git fetch temp remote: Enumerating objects: 495, done. remote: Counting objects: 100% (495/495), done. remote: Compressing objects: 100% (237/237), done. remote: Total 495 (delta 171), reused 434 (delta 110), pack-reused 0 Receiving objects: 100% (495/495), 492.23 KiB | 687.00 KiB/s, done. Resolving deltas: 100% (171/171), done. From github.com:[account]/project-v1 * [new branch] beanie -> temp/beanie * [new branch] main -> temp/main * [new branch] other -> temp/other
fetch로 remote temp의 모든 원격 branch들을 가져옵니다. merge하려는 target인 beanie 브랜치가 temp/beanie로 잘 가져온 것을 확인 할 수 있습니다.
project-v2> git checkout -b beanie
merge 하기 전에 project-v2에도 beanie 브랜치를 하나 만들어줍니다.
project-v2>beanie> git merge temp/beanie fatal: refusing to merge unrelated histories
타겟으로 하고 있는 temp/beanie를 project-v2의 beanie로 merge를 시도했는데 error가 나옵니다.
내용을 확인해보면 연관이 없는 이력(unrelated histories)을 merge하는 것을 금지한다고 나오네요.
해당 내용은 구글링하면 바로 나옵니다.

The “fatal: refusing to merge unrelated histories” Git error
The “fatal: refusing to merge unrelated histories” Git error
Contributor: Educative Answers Team
www.educative.io
위의 글을 보면 전혀 관련없는 커밋 이력을 가진 repository들을 가지고 merge를 하려고하면 git은 error를 반환한다고 알려주고 있는데요. 아무래도 merge하려는 두 개의 서로 다른 repository 간에 커밋 이력 연결점을 찾을 수 없기 때문에 기본적으로 error 처리한 것 같습니다. (개인적인 생각)
You have created a new repository, added a few commits to it, and now you are trying to pull from a remote repository that already has some commits of its own. Git will also throw the error in this case, since it has no idea how the two projects are related.
저같은 경우 이 상황에 해당한다고 볼 수 있겠네요.
새로운 프로젝트(project-v2)를 만들고 beanie 브랜치 만든 상황에서 커밋을 한 이력이 있었고 다른 원격 브랜치를 pull할 때 발생한 에러라고 볼 수 있습니다.(pull은 기본적으로 fetch > merge 과정을 한 번에 수행하기에)
📌 3. 해결책
해결책은 위의 첨부해드린 글에서 나와있는대로 간단하게 해결 가능합니다.
project-v2>beanie> git merge temp/beanie --allow-unrelated-histories
--allow-unrelated-histories 해당 옵션을 넣어서 merge 해주면 됩니다.
전혀 관련 없는 repository에서 merge를 한 것이기 때문에 merge conflict가 많이 나오긴 했었는데 적절하게 잘 resolve하면 됩니다.
원하는 branch에 무조건 merge하고자 할 때 사용하시면 될 것 같고 실무에서는 사용하지 않는 것으로,,,
(실무에서는 있어서는 안될 상황이긴 합니다.)
'Git' 카테고리의 다른 글
git tag(태그) 기능 및 사용방법 정리 (0) | 2022.02.12 |
---|
댓글
댓글(Github)
다른 글
-
git tag(태그) 기능 및 사용방법 정리
git tag(태그) 기능 및 사용방법 정리
2022.02.12git에 대한 기본 개념에 대해서 공부하면서 tag를 살펴보게 됐습니다. 실무에서는 주로 production 배포할 때 태그를 사용하게 됩니다. 실무에서 자주 사용되는 tag 기능과 사용법에 대해서 직접 테스트해보면서 정리한 내용을 남겨보고자 합니다. 📌 tag 개념 tag(태그)는 말 그대로 일종의 이정표를 남기는 것이라 할 수 있습니다. 커밋으로 움직이는 git 체계에서는 커밋자체에 이정표를 남길 수 있는데 이 때 tag를 사용합니다. git에서의 tag 개념은 다음과 같습니다. tag는 커밋을 참조하기 쉽도록 커밋에 이름을 붙이는 것 (commit ← tag를 붙이는 것) 특정 커밋에 한 번 붙인 tag는 고정 tag를 붙인 커밋에 새로운 커밋을 붙여도 tag는 이동하지 않습니다. Lightweig…
댓글을 사용할 수 없습니다.