[Git/GitHub] 여러 개의 Repository 병합 (commit history 유지)

Soyeon·2021년 7월 6일
1

GitHub

목록 보기
2/2
post-thumbnail

문제

공부하면서 만들어놓은 여러 개의 Repository를 정리하려고 하나의 Repository로 병합했다.
모든 Repository에서 파일을 받고 다시 업로드하면 간편하겠지만 몇 없는 소중한 잔디를 뽑을 순 없었다.

해결 방법

  1. 하나로 합칠 새 Repository와 폴더를 생성하고, 생성한 폴더에서 우클릭으로 Git Bash를 연다.
  1. 아래의 command 명령어를 입력하여 .git 파일을 생성한다.
$ git init
  1. 병합하고자 하는 Repository에서 데이터를 가져온다.
    (아래의 과정을 Repository 수만큼 하면 됨)
# 기존 repository 연결
$ git remote add old-project https://github.com/user-name/old-project.git
# 기존 repository에서 데이터 가져오기
$ git fetch old-project
# 병합 : --allow-unrelated-histories를 입력해야 커밋 기록 유지, master-branch
$ merge --allow-unrelated-histories old-project/master -m "commit message"
# 필요한 작업이 끝났으면 안전하게 연결 해제
$ git remote remove old-project
  1. 새 Repository에 Push한다.
$ git add .
$ git remote add origin https://github.com/user-name/new-project.git
$ git push -u origin master  # origin-remote, master-branch
  1. GitHub에서 모든 데이터가 정상적으로 업로드 되었고, commit history도 유지되는 것을 확인했다.
    (기존 Repository는 삭제해도 됨)
profile
Back-end Developer Jr.

0개의 댓글