여러가지 / / 2024. 2. 27. 13:44

[git] 특정 repository의 모듈을 다른 repository로 옮기기 (이력 유지)

git에서 특정 repository에 있는 특정 모듈을 다른 repository로 이동하려고 할 때 이력과 함께 옮기는 방법이다.

일단 아래의 그림을 한번 보자.

component 하위에 특정 message 모듈을 project-new의 message로 이동할 때 기본적으로 move나 복사를 하면 git 이력이 유지되지 않는다. 이력을 유지하면서 이동하는 방법을 알아보자.

1. 별도의 디렉토리에 작업환경 구성

refactoring 디렉토리를 생성하여 각 repo에서 소스를 내려받자.

각 작업은 develop 브랜치를 가져다가 merge-branch 브랜치에서 작업을 수행한다.

mkdir refactoring
cd refactoring

# git clone으로 내려받는다.
git clone https://github.com/company/project-curr.git
git clone https://github.com/company/project-new.git

# project-new에서 develop을 checkout한다.
cd project-new
git checkout develop
# merge-branch로 checkout
git checkout -b merge-branch 

# project-curr에서 develop을 checkout한다.
cd ../project-curr
git checkout develop
# merge-branch로 checkout
git checkout -b merge-branch 

2. 이전할 모듈([project-curr] message)을 기준으로 재구성한다.

  • repo에서 component 하위만 대상으로 설정
  • project-curr에서 message을 제외하고 모두 삭제 (git rm)
cd ../project-curr
git filter-branch --subdirectory-filter component -- --all 

# 해당 경로에 message 모듈을 제외하고는 모두 git에서 삭제
find . -not \( -name "message" -o -name "*.git" \) -maxdepth 1 -not -path . -exec git rm -r {} \;
# git commit을 한다.
git commit -m "message commit";

3. 목적지 repo에서 이전 대상 repo 추가

project-new repo에서 project-curr를 가져올 수 있도록 git remote add를 한다.

cd ../project-new
git remote add project-curr ../project-curr
git fetch project-curr

4. git merge & message에 반영

project-curr와 project-new 모두 merge-branch 브랜치에서 작업을 진행

git merge --allow-unrelated-histories -s ours --no-commit project-curr/merge-branch
git read-tree --prefix=component -u project-curr/merge-branch 

# message가 잘 merge 되었는지 확인
cd component && ll

5. 커밋 & push

# 커밋
git commit -m "message migration from project-curr" 
# project-curr 브랜치 삭제
git remote rm project-curr
# merge-branch 브랜치를 git push
git push origin merge-branch 
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유