13 번째 수정본
git 저장소 두 개를 병합
저장소 A와 B가 있다.
A - /
/fileA
B - /
/fileB
A 저장소를 B에 합치면서, 기존 커밋 로그 등도 다 가져오고 싶다.
(커밋 로그 같은 거 가져올 필요가 없다면... 그냥 fileA를 복사하면 된다)
2. 디렉토리 이동이 필요없는 경우
B - /
/fileA
/fileB
이렇게 만들 경우는 쉽다.
$ cd B
$ git fetch ../A/ master:branch_A
$ git merge branch_A
뭐 그나마 기분이 나쁜 거라면, 시작 커밋이 서로 다른 두 히스토리를 합쳤으니까 한 커밋에서 시작하는 게 아니라 두 커밋에서 시작해서 나중에 합쳐진 형태의 그래프가 된다.
원한다면 한쪽 히스토리와 다른 히스토리를 연결할 수도 있다. Git의 "두 저장소를 잇기"와 "CVS에서 Git로 이전하기" 참조.
3. 한쪽 저장소를 서브디렉토리에 두고 싶은 경우
B - /
/A/fileA
/fileB
이렇게 하려니, 은근히 복잡하다.
사실 이게 맞는지 제대로 정리가 안 되었는데, 반나절 동안 직접 해보면서 발견한 내용만 적어본다.
아래 내용들은 거의 다 [merge - How to import existing GIT repository into another? - Stack Overflow] 질문글에 달린 답변들이 출처이다.
3.1. fetch로 가져오고 mv로 옮기고 merge
[B (master)]$ git remote add repoA ../A/
[B (master)]$ git fetch repoA
From ../A
* [new branch] master -> repoA/master
[B (master)]$ git checkout -b TEMP repoA/master
Branch TEMP set up to track remote branch master from repoA.
Switched to a new branch 'TEMP'
[B (TEMP)]$ mkdir A
[B (TEMP)]$ git mv fileA A/fileA
[B (TEMP)]$ git commit -m "move fileA to A/"
[TEMP d13a3ce] move fileA to A/
1 files changed, 0 insertions(+), 0 deletions(-)
rename fileA => A/fileA (100%)
[B (TEMP)]$ git checkout master
Switched to branch 'master'
[B (master)]$ git merge TEMP
Merge made by the 'recursive' strategy.
A/fileA | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 A/fileA
[B (master)]$ git remote rm repoA
[B (master)]$ git branch -d TEMP
Deleted branch TEMP (was d13a3ce).
이렇게 하면 git log를 했을 때 두 저장소의 로그도 잘 보인다. 그리고 A디렉토리로 옮기는 것을 나타내는 커밋 로그도 있어서 직관적이다.
문제는 뭐냐 하면, A 서브디렉토리 아래 있는 파일들의 개별 로그를 보는 게 귀찮다
$ git log -- A/fileA
commit d13a3ce29b1ffccc5ce1cda062883c750ddac390
Author: Geunyoung Park <gypark@gmail.com>
Date: Thu Feb 11 00:42:59 2016 +0900
move fileA to A/
$ git log --follow A/fileA
d13a3ce move fileA to A/
b010c01 A2
abcad74 init A
그런데 --follow 옵션을 붙일 때는 대상 파일을 하나밖에 지정할 수 없다. (보통은 파일 하나만 보면 충분하겠지만)
4. 기타 & comments
컴퓨터분류